import graphlab

"""
Explanation: Fire up graphlab create
End of explanation
"""


sales = graphlab.SFrame('home_data.gl/')

sales

"""
Explanation: Load some house sales data
Dataset is from house sales in King County, the region where the city of Seattle, WA is located.
End of explanation
"""


graphlab.canvas.set_target('ipynb')
sales.show(view="Scatter Plot", x="sqft_living", y="price")

"""
Explanation: Exploring the data for housing sales
The house price is correlated with the number of square feet of living space.
End of explanation
"""


train_data,test_data = sales.random_split(.8,seed=0)

"""
Explanation: Create a simple regression model of sqft_living to price
Split data into training and testing.
We use seed=0 so that everyone running this notebook gets the same results.  In practice, you may set a random seed (or let GraphLab Create pick a random seed for you).
End of explanation
"""


sqft_model = graphlab.linear_regression.create(train_data, target='price', features=['sqft_living'])

"""
Explanation: Build the regression model using only sqft_living as a feature
End of explanation
"""


print test_data['price'].mean()

print sqft_model.evaluate(test_data)

"""
Explanation: Evaluate the simple model
End of explanation
"""


import matplotlib.pyplot as plt
%matplotlib inline

plt.plot(test_data['sqft_living'],test_data['price'],'.',
        test_data['sqft_living'],sqft_model.predict(test_data),'-')

"""
Explanation: RMSE of about \$255,170!
Let's show what our predictions look like
Matplotlib is a Python plotting library that is also useful for plotting.  You can install it with:
'pip install matplotlib'
End of explanation
"""


sqft_model.get('coefficients')

"""
Explanation: Above:  blue dots are original data, green line is the prediction from the simple regression.
Below: we can view the learned regression coefficients.
End of explanation
"""


my_features = ['bedrooms', 'bathrooms', 'sqft_living', 'sqft_lot', 'floors', 'zipcode']

sales[my_features].show()

sales.show(view='BoxWhisker Plot', x='zipcode', y='price')

"""
Explanation: Explore other features in the data
To build a more elaborate model, we will explore using more features.
End of explanation
"""


my_features_model = graphlab.linear_regression.create(train_data,target='price',features=my_features)

print my_features

"""
Explanation: Pull the bar at the bottom to view more of the data.  
98039 is the most expensive zip code.
Build a regression model with more features
End of explanation
"""


print sqft_model.evaluate(test_data)
print my_features_model.evaluate(test_data)

"""
Explanation: Comparing the results of the simple model with adding more features
End of explanation
"""


house1 = sales[sales['id']=='5309101200']

house1

"""
Explanation: The RMSE goes down from \$255,170 to \$179,508 with more features.
Apply learned models to predict prices of 3 houses
The first house we will use is considered an "average" house in Seattle.
End of explanation
"""


print house1['price']

print sqft_model.predict(house1)

print my_features_model.predict(house1)

"""
Explanation: <img src="house-5309101200.jpg">
End of explanation
"""


house2 = sales[sales['id']=='1925069082']

house2

"""
Explanation: In this case, the model with more features provides a worse prediction than the simpler model with only 1 feature.  However, on average, the model with more features is better.
Prediction for a second, fancier house
We will now examine the predictions for a fancier house.
End of explanation
"""


print sqft_model.predict(house2)

print my_features_model.predict(house2)

"""
Explanation: <img src="house-1925069082.jpg">
End of explanation
"""


bill_gates = {'bedrooms':[8], 
              'bathrooms':[25], 
              'sqft_living':[50000], 
              'sqft_lot':[225000],
              'floors':[4], 
              'zipcode':['98039'], 
              'condition':[10], 
              'grade':[10],
              'waterfront':[1],
              'view':[4],
              'sqft_above':[37500],
              'sqft_basement':[12500],
              'yr_built':[1994],
              'yr_renovated':[2010],
              'lat':[47.627606],
              'long':[-122.242054],
              'sqft_living15':[5000],
              'sqft_lot15':[40000]}

"""
Explanation: In this case, the model with more features provides a better prediction.  This behavior is expected here, because this house is more differentiated by features that go beyond its square feet of living space, especially the fact that it's a waterfront house. 
Last house, super fancy
Our last house is a very large one owned by a famous Seattleite.
End of explanation
"""


print my_features_model.predict(graphlab.SFrame(bill_gates))

"""
Explanation: <img src="house-bill-gates.jpg">
End of explanation
"""


sales[sales['zipcode'] == 98039]

sales_1 = sales[sales['zipcode'] == '98039']
print sales_1['price'].mean()

"""
Explanation: The model predicts a price of over $13M for this house! But we expect the house to cost much more.  (There are very few samples in the dataset of houses that are this fancy, so we don't expect the model to capture a perfect prediction here.)
Exercise
1. Selection and summary statistics: In the notebook we covered in the module, we discovered which neighborhood (zip code) of Seattle had the highest average house sale price. Now, take the sales data, select only the houses with this zip code, and compute the average price. Save this result to answer the quiz at the end.
End of explanation
"""


house_3 = sales[(sales['sqft_living'] > 2000) & (sales['sqft_living'] < 4000 )]

print sales.shape
print house_3.shape
print 9111.0/21613.0

"""
Explanation: 2. Filtering data: One of the key features we used in our model was the number of square feet of living space (‘sqft_living’) in the house. For this part, we are going to use the idea of filtering (selecting) data.

In particular, we are going to use logical filters to select rows of an SFrame. You can find more info in the Logical Filter section of this documentation.
Using such filters, first select the houses that have ‘sqft_living’ higher than 2000 sqft but no larger than 4000 sqft.
What fraction of the all houses have ‘sqft_living’ in this range? Save this result to answer the quiz at the end.
End of explanation
"""


advanced_features = [
'bedrooms', 'bathrooms', 'sqft_living', 'sqft_lot', 'floors', 'zipcode',
'condition', # condition of house
'grade', # measure of quality of construction
'waterfront', # waterfront property
'view', # type of view
'sqft_above', # square feet above ground
'sqft_basement', # square feet in basement
'yr_built', # the year built
'yr_renovated', # the year renovated
'lat', 'long', # the lat-long of the parcel
'sqft_living15', # average sq.ft. of 15 nearest neighbors
'sqft_lot15', # average lot size of 15 nearest neighbors 
]

advanced_features_model = graphlab.linear_regression.create(train_data, target='price', features=advanced_features)

print my_features_model.evaluate(test_data)
print advanced_features_model.evaluate(test_data)

print 178861.74561891545 - 156757.5481062501

"""
Explanation: 3. Building a regression model with several more features: In the sample notebook, we built two regression models to predict house prices, one using just ‘sqft_living’ and the other one using a few more features, we called this set
End of explanation
"""


