<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Machine Learning Foundations - A Case Study Approach
Week 2 - Demonstration Linear Regression
Donny van der Meer
Load data
Step1:  The import into pandas looks correct, but the date is not formatted correctly and the zipcode is interpreted as a number rather than a category. Let's fix this.
Step2:  Exploring the data for housing sales
Step3:  Create simple regression model of size_sqft to price
Step4:  Build the regression model
Step5:  Evaluate the simple model
Step6:  Let's show what our predictions look like
Step7:  Explore other features in the data
Step8:  Build a regression model with more features
Step9:  Apply learned models to predict prices of 3 houses
Step10:  Prediction for a second, fancier house
Step11:  Last house, super fancy
<ASSISTANT_TASK:>
Python Code:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.cross_validation import train_test_split
from sklearn import linear_model
from math import sqrt
plt.style.use('ggplot')
%matplotlib inline
sales = pd.read_csv("../data/home_data.csv")
sales.head(10)
sales.dtypes
Explanation: Machine Learning Foundations - A Case Study Approach
Week 2 - Demonstration Linear Regression
Donny van der Meer
Load data
End of explanation
sales['date'] = pd.to_datetime(sales['date'])
sales['zipcode'] = sales['zipcode'].astype('category')
sales.head(10)
len(sales)
Explanation: The import into pandas looks correct, but the date is not formatted correctly and the zipcode is interpreted as a number rather than a category. Let's fix this.
End of explanation
sales.plot(kind = 'scatter', x='sqft_living', y='price')
Explanation: Exploring the data for housing sales
End of explanation
# Use the train_test_split function from scikit-learn (imported at the top)
# Here we have to give the size of the test (instead of the size of 
# the train data in the lecture). random_state is the seed
train_data, test_data = train_test_split(sales, test_size=0.2, random_state=42)
print(len(train_data), len(test_data))
Explanation: Create simple regression model of size_sqft to price
End of explanation
sqft_model = linear_model.LinearRegression()
train_X = np.reshape(train_data['sqft_living'], (-1, 1)) # reshape required for sklearn
train_Y = train_data['price']
sqft_model.fit(X = train_X, y = train_Y)
Explanation: Build the regression model
End of explanation
print(test_data['price'].mean())
# Define a helper function to assess model performance
def evaluate(model, test_x, test_y):
    errors = np.absolute(model.predict(test_x) - test_y)
    return {'rmse': sqrt(np.mean(errors ** 2)), 'max_error' : errors.max()}
test_X = np.reshape(test_data['sqft_living'], (-1, 1))
test_Y = test_data['price']
evaluate(sqft_model, test_X, test_Y)
Explanation: Evaluate the simple model
End of explanation
plt.plot(test_data['sqft_living'], test_data['price'], '.',
        test_data['sqft_living'], sqft_model.predict(test_X), '-')
print(sqft_model.coef_[0], sqft_model.intercept_)
Explanation: Let's show what our predictions look like
End of explanation
sales.columns
my_features = ['bedrooms', 'bathrooms', 'sqft_living', 'sqft_lot', 'floors', 'zipcode']
sales[my_features].describe(include = 'all')
sales[my_features].hist()
sales.boxplot(column='price', by='zipcode', rot=-45)
Explanation: Explore other features in the data
End of explanation
train_with_dummies = pd.get_dummies(train_data[my_features])
test_with_dummies = pd.get_dummies(test_data[my_features])
train_with_dummies.head()
my_features_model = linear_model.LinearRegression()
my_features_model.fit(X = train_with_dummies, y = train_Y)
print(evaluate(sqft_model, test_X, test_Y))
print(evaluate(my_features_model, test_with_dummies, test_Y))
Explanation: Build a regression model with more features
End of explanation
house1 = sales.loc[sales['id'] == 5309101200]
house1
print(house1.price)
# Prediction from the sqft_model (with some reshaping to prevent errors)
sqft_model.predict(np.reshape(house1['sqft_living'],(-1,1)))
# Prediction from my_features_model (with zipcode converted to dummy)
my_features_model.predict(pd.get_dummies(house1[my_features]))
Explanation: Apply learned models to predict prices of 3 houses
End of explanation
house2 = sales.loc[sales['id'] == 1925069082]
house2
sqft_model.predict(np.reshape(house2['sqft_living'],(-1,1)))
my_features_model.predict(pd.get_dummies(house2[my_features]))
Explanation: Prediction for a second, fancier house
End of explanation
bill_gates = house2.copy()
bill_gates['bedrooms'] = 8
bill_gates['bathrooms'] = 25
bill_gates['sqft_living'] = 50000
bill_gates['sqft_lot'] = 225000
bill_gates['floors'] = 4
bill_gates.loc[:,'zipcode'] = 98039
bill_gates['zipcode']
sqft_model.predict(np.reshape(bill_gates['sqft_living'], (-1,1)))
my_features_model.predict(pd.get_dummies(bill_gates[my_features]))
Explanation: Last house, super fancy
End of explanation
<END_TASK>