<jupyter_start><jupyter_text>Multiple Linear Regression   1 - Load Packages and Data<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from MLR_utils import computeCost

%matplotlib inline
df = pd.read_pickle('./Data/train_final')
m = df.shape[0]
df.head()<jupyter_output><empty_output><jupyter_text>2 - Split into Training and Validation SetsWe will split the training data into a smaller training set (70%) and a validation set (30%).  **Note that the validation set is denoted here as X_test and y_test.**<jupyter_code>from sklearn.model_selection import train_test_split
X = df.drop('logSalePrice',axis=1)
y = df['logSalePrice']

X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.3, random_state=101)<jupyter_output><empty_output><jupyter_text>3 - Fit Model<jupyter_code>from sklearn.linear_model import LinearRegression
lm = LinearRegression()
lm.fit(X_train,y_train)

theta_hat = np.r_[lm.intercept_,lm.coef_]
X_train_1 = np.c_[np.ones((X_train.shape[0],1)),X_train]
theta_hat = theta_hat.reshape((len(theta_hat),1))

y_tr = y_train.values.reshape((len(y_train),1))
X_val_1 = np.c_[np.ones((X_val.shape[0],1)), X_val]
y_te = y_val.values.reshape((len(y_val),1))

error_train = computeCost(theta_hat,X_train_1,y_tr)
error_val = computeCost(theta_hat,X_val_1,y_te)

print("Training Error: " + str(error_train))
print("Validation Error: " + str(error_val))<jupyter_output>Training Error: 0.007568033439315971
Validation Error: 0.011185798456694027<jupyter_text>3 - Prediction and Residual Plots (Validation Set)Make sure to use the **not scaled** outputs in this section!<jupyter_code>predictions = lm.predict(X_val)
fig, axes = plt.subplots(1, 2, figsize=(12, 4))

axes[0].scatter(y_val,predictions)
axes[0].set_title("Predictions vs Actual logSalePrice")

sns.distplot((y_val-predictions),bins=50);
axes[1].set_title("Distribution Plot");<jupyter_output><empty_output><jupyter_text>4 - Performance Evaluation Metrics (Validation Set)<jupyter_code>from sklearn import metrics
print('log-scale MSE:', round(np.sqrt(metrics.mean_squared_error(y_val, predictions)),10))
preds = pd.Series(data=predictions)
y_val_ri = y_val.reset_index()
y_val_ri.drop('index',axis=1,inplace=True)
compare = pd.concat([y_val_ri['logSalePrice'],preds],axis=1)
compare.rename(columns={0: 'prediction'}, inplace=True)
compare = compare.apply(lambda x: round(np.exp(x),1))
compare['SalePrice'] = compare['logSalePrice']
compare.drop('logSalePrice', axis=1, inplace=True)
compare['difference'] = compare['SalePrice']-compare['prediction']
#print(compare.head())
from sklearn import metrics
mse = round(metrics.mean_squared_error(np.exp(y_val), np.exp(predictions)),1)
print('Validation MSE:', "{:,}".format(mse))
pd.options.display.float_format = '{:,.2f}'.format
print(compare.head(10))<jupyter_output>prediction  SalePrice  difference
0  251,229.50 255,000.00    3,770.50
1  137,790.70 145,000.00    7,209.30
2  141,322.40 150,500.00    9,177.60
3  401,586.30 412,500.00   10,913.70
4  331,098.20 402,861.00   71,762.80
5  106,560.10 113,000.00    6,439.90
6  137,950.30 136,000.00   -1,950.30
7  150,397.40 144,152.00   -6,245.40
8  151,361.10 145,250.00   -6,111.10
9  146,118.30 135,000.00  -11,118.30<jupyter_text>5 - Test Predictions<jupyter_code>#test set features
X_test = pd.read_pickle('./Data/test_final')
test_prediction = lm.predict(X_test)
test_price = np.exp(test_prediction)
#get ID's
id_test = pd.read_csv('./Data/test')
id_test['SalePrice'] = test_price
id_test[['Id','SalePrice']].head()
test_submit = id_test[['Id','SalePrice']]
test_submit.to_csv('./Data/testsaleprice.txt', header = True, index = False, sep=',')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14958299704062417, 'fraction_numerical': 0.08609093354856066, 'mean_word_length': 6.944444444444445, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '323554', 'license': 'MIT', 'n_tokens_mistral': 1640, 'n_tokens_neox': 1439, 'n_words': 264, 'path': 'multiple_linear_regression.ipynb', 'repo_name': 'JohnYap/house-prices', 'size': '3717'}