image_output_1.png
from sklearn.linear_model import LinearRegression

from sklearn.metrics import r2_score, mean_absolute_error, mean_squared_error

from sklearn.model_selection import train_test_split, cross_val_score

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import seaborn as sns

data = pd.read_csv('../input/house-prices-advanced-regression-techniques/train.csv')

important = list(data.corr()['SalePrice'][(data.corr()['SalePrice'] > 0.5) | (data.corr()['SalePrice'] < -0.5)].index)
cat_columns = ['MSZoning', 'Utilities', 'BldgType', 'Heating', 'KitchenQual', 'SaleCondition', 'LandSlope']
important_columns = important + cat_columns
data = data[important_columns]

X = data.drop('SalePrice', axis=1)
y = data['SalePrice']

X = pd.get_dummies(X, columns=cat_columns)

def rmse_cv(model):
    rmse = np.sqrt(-cross_val_score(model, X, y, scoring='neg_mean_squared_error', cv=5)).mean()
    return rmse
def evaluation(y, predictions):
    mae = mean_absolute_error(y, predictions)
    mse = mean_squared_error(y, predictions)
    rmse = np.sqrt(mean_squared_error(y, predictions))
    r_squared = r2_score(y, predictions)
    return (mae, mse, rmse, r_squared)

models = pd.DataFrame(columns=['Model', 'MAE', 'MSE', 'RMSE', 'R2 Score', 'RMSE (Cross-Validation)'])

lin_reg = LinearRegression()
lin_reg.fit(X_train, y_train)
prediction = lin_reg.predict(X_test)
mae, mse, rmse, r_squared = evaluation(y_test, prediction)
print('MAE:', mae)
print('MSE:', mse)
print('RMSE:', rmse)
print('R2 Score:', r_squared)
print('-' * 30)
rmse_cross_val = rmse_cv(lin_reg)
print('RMSE Cross-Validation:', rmse_cross_val)
new_row = {'Model': 'LinearRegression', 'MAE': mae, 'MSE': mse, 'RMSE': rmse, 'R2 Score': r_squared, 'RMSE (Cross-Validation)': rmse_cross_val}
models = models.append(new_row, ignore_index=True)