image_output_11.png image_output_14.png image_output_13.png image_output_5.png image_output_7.png image_output_4.png image_output_8.png image_output_6.png image_output_12.png image_output_3.png image_output_2.png image_output_1.png image_output_10.png image_output_15.png image_output_9.png
from scipy import stats

from sklearn import preprocessing

from sklearn.linear_model import LinearRegression

from sklearn.metrics import accuracy_score, jaccard_score, mean_absolute_error, mean_squared_error, r2_score

from sklearn.model_selection import train_test_split

import matplotlib.pyplot as plt   #visualisation

import numpy as np

import pandas as pd

import seaborn as sns

data = pd.read_csv('/kaggle/input/housesalesprediction/kc_house_data.csv')

data.shape

data.dtypes

data.drop(['id', 'date', 'lat', 'long'], axis=1, inplace=True)

column_names = ['bedrooms', 'bathrooms', 'sqft_living', 'sqft_lot', 'floors', 'waterfront', 'view', 'condition', 'grade', 'sqft_above', 'sqft_basement', 'yr_built', 'yr_renovated', 'sqft_living15', 'sqft_lot15']

Q1 = data.quantile(0.25)
Q3 = data.quantile(0.75)
IQR = Q3 - Q1
upper = data[~(data > Q3 + 1.5 * IQR)].max()
lower = data[~(data < Q1 - 1.5 * IQR)].min()
df = np.where(data > upper, data.mean(), np.where(data < lower, data.mean(), data))
data = pd.DataFrame(df, columns=data.columns)

pearson_coef_values = []
p_values = []
for name in column_names:
    pearson_coef, p_value = stats.pearsonr(data[name], data['price'])
    pearson_coef_values.append(pearson_coef)
    p_values.append(p_value)
pearson_corelation = pd.DataFrame({'Feature': column_names, 'Pearson Coefficient Values': pearson_coef_values, 'P Values': p_values})
pearson_corelation.sort_values('Pearson Coefficient Values')

c = data.corr()
c

X = data[['bedrooms', 'bathrooms', 'sqft_living', 'sqft_lot', 'floors', 'waterfront', 'view', 'condition', 'grade', 'sqft_above', 'sqft_basement', 'yr_built', 'yr_renovated', 'sqft_living15', 'sqft_lot15']]
X = preprocessing.StandardScaler().fit(X).transform(X)
Y = data['price']

from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.3, random_state=1)

from sklearn.linear_model import LinearRegression
linearReg = LinearRegression().fit(x_train, y_train)

predictions = linearReg.predict(x_test)

from sklearn.metrics import accuracy_score, jaccard_score, mean_absolute_error, mean_squared_error, r2_score
LinearRegression_MAE = mean_absolute_error(y_test, predictions)
LinearRegression_MSE = mean_squared_error(y_test, predictions)
LinearRegression_R2 = r2_score(y_test, predictions)

Report = pd.DataFrame({'Mean Absolute Error': LinearRegression_MAE, 'Mean Squared Error': LinearRegression_MSE, 'R Squared': LinearRegression_R2}, index=[0])
Report.head()