<jupyter_start><jupyter_text>Description and Details1. Load house sales data2. Add 4 new variables in training set and test set3. Estimate model 1, 2, 3 using three different sets of features4. Calculate RSS of three models on both training set and test set<jupyter_code>import pandas as pd
import numpy as np
import math
from sklearn import linear_model
sales = pd.read_csv('kc_house_data.csv')
train_data = pd.read_csv('kc_house_train_data.csv')
test_data = pd.read_csv('kc_house_test_data.csv')
sales.dtypes<jupyter_output><empty_output><jupyter_text>Create some new features<jupyter_code>train_data['bedrooms_squared'] = train_data['bedrooms'].apply(lambda x: x**2)
train_data['bed_bath_rooms'] = train_data['bedrooms'] * train_data['bathrooms']

train_data['log_sqft_living'] = train_data['sqft_living'].apply(lambda x: math.log(x))
train_data['lat_plus_long'] = train_data['lat'] + train_data['long']

test_data['bedrooms_squared'] = test_data['bedrooms'].apply(lambda x: x**2)
test_data['bed_bath_rooms'] = test_data['bedrooms'] * test_data['bathrooms']


test_data['log_sqft_living'] = test_data['sqft_living'].apply(lambda x: math.log(x))
test_data['lat_plus_long'] = test_data['lat'] + test_data['long']<jupyter_output><empty_output><jupyter_text>Learning Multiple Models<jupyter_code>feature_1 = ['sqft_living', 'bedrooms', 'bathrooms', 'lat', 'long']
feature_2 = feature_1 + ['bed_bath_rooms']
feature_3 = feature_2 + ['bedrooms_squared', 'log_sqft_living', 'lat_plus_long']
lm = linear_model.LinearRegression()
model_1 = lm.fit(train_data[feature_1], train_data['price'])
lm = linear_model.LinearRegression()
model_2 = lm.fit(train_data[feature_2], train_data['price'])
lm = linear_model.LinearRegression()
model_3 = lm.fit(train_data[feature_3], train_data['price'])
print(model_1.coef_, model_1.intercept_)
print(model_2.coef_, model_2.intercept_)
print(model_3.coef_, model_3.intercept_)<jupyter_output>[ 3.12258646e+02 -5.95865332e+04  1.57067421e+04  6.58619264e+05
 -3.09374351e+05] -69075726.79256983
[ 3.06610053e+02 -1.13446368e+05 -7.14613083e+04  6.54844630e+05
 -2.94298969e+05  2.55796520e+04] -66867968.8710789
[ 5.29422820e+02  3.45142296e+04  6.70607813e+04  5.34085611e+05
 -4.06750711e+05 -8.57050439e+03 -6.78858667e+03 -5.61831484e+05
  1.27334900e+05] -62036084.98609828<jupyter_text>Comparing multiple modelsFirst use your functions from earlier to compute the RSS on TRAINING Data for each of the three models.<jupyter_code>#predict on training data
pred_model_1 = model_1.predict(train_data[feature_1])
pred_model_2 = model_2.predict(train_data[feature_2])
pred_model_3 = model_3.predict(train_data[feature_3])
#predict on test data
pred_model_test_1 = model_1.predict(test_data[feature_1])
pred_model_test_2 = model_2.predict(test_data[feature_2])
pred_model_test_3 = model_3.predict(test_data[feature_3])
#compute RSS for training data
RSS_train_1 = ((train_data['price'] - pred_model_1)**2).sum()
RSS_train_2 = ((train_data['price'] - pred_model_2)**2).sum()
RSS_train_3 = ((train_data['price'] - pred_model_3)**2).sum()
print('RSS_train_1 is:', RSS_train_1, '\n'  
      'RSS_train_2 is:', RSS_train_2, '\n'
      'RSS_train_3 is:', RSS_train_3)
#compute RSS for training data
RSS_test_1 = ((test_data['price'] - pred_model_test_1)**2).sum()
RSS_test_2 = ((test_data['price'] - pred_model_test_2)**2).sum()
RSS_test_3 = ((test_data['price'] - pred_model_test_3)**2).sum()
print('RSS_test_1 is:', RSS_test_1, '\n'  
      'RSS_test_2 is:', RSS_test_2, '\n'
      'RSS_test_3 is:', RSS_test_3)<jupyter_output>RSS_test_1 is: 225500469795489.97 
RSS_test_2 is: 223377462976467.06 
RSS_test_3 is: 259236319207179.94
{'fraction_non_alphanumeric': 0.14050944946589974, 'fraction_numerical': 0.10846343467543139, 'mean_word_length': 7.5327102803738315, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '422398', 'license': 'MIT', 'n_tokens_mistral': 1662, 'n_tokens_neox': 1446, 'n_words': 246, 'path': 'week2/HW2_1.ipynb', 'repo_name': 'jessie-sun927/UW-Machine-Learning-Regression', 'size': '3651'}