<jupyter_start><jupyter_text>**Step 1: Reading Data**<jupyter_code>#read the data

df= pd.read_csv(r'/kaggle/input/usa-housing/USA_Housing.csv')
#check data
df.head()
#check shape of data
df.shape
#info of dataframe
df.info()
df.describe()<jupyter_output><empty_output><jupyter_text>Step 2: Exploratory Data Analysis (EDA)<jupyter_code>#plotting a pairplot

sns.pairplot(df, diag_kind='kde')
# plotting heat map for correlations

sns.heatmap(df.corr(), annot=True)
plt.show()
#dist plot for dependent variable
sns.distplot(df['Price'])<jupyter_output><empty_output><jupyter_text>Step 3: Splitting the Data into Training and Testing Sets<jupyter_code>#selecting X and y for Model
X = df.drop(['Address', 'Price'], axis=1)
y = df['Price']
#dividing data into train, test
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.70, random_state=0)
# train data
X_train.head()
#train data shape
X_train.shape
#test data shape
X_test.shape<jupyter_output><empty_output><jupyter_text>Step 4: Building the Model<jupyter_code>#build linear model

#add a constant
X_train_sm = sm.add_constant(X_train)

#model creation
lr= sm.OLS(y_train, X_train_sm)
#fit the model
lr_model= lr.fit()
#params
lr_model.params
#summary
lr_model.summary()<jupyter_output><empty_output><jupyter_text>* Here p value of 'Avg. Area Number of Bedrooms' is very high, therefore we are dropping it.<jupyter_code>#dropping 'Avg. Area Number of Bedrooms' as it has high p value. 
X_train= X_train.drop(columns=['Avg. Area Number of Bedrooms'])
#build linear model

#add a constant
X_train_sm = sm.add_constant(X_train)

#model creation
lr= sm.OLS(y_train, X_train_sm)
#fit the model
lr_model= lr.fit()
#params
lr_model.params
#summary
lr_model.summary()
#building model using sklearn

lm= LinearRegression()
lm.fit(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Step 5: Residual analysis<jupyter_code>#predict y_train
y_train_cnt = lm.predict(X_train)
# Plot the histogram of the error terms
fig = plt.figure()
sns.distplot((y_train - y_train_cnt), bins = 20)
# Plot heading 
fig.suptitle('Error Terms', fontsize = 20) 
# X-label
plt.xlabel('Errors', fontsize = 18)    
plt.show()<jupyter_output><empty_output><jupyter_text>Step 6: Making Predictions on Test Set<jupyter_code>#preparing X_test data

X_test= X_test.drop(columns=['Avg. Area Number of Bedrooms'])
#X_test data

X_test.head()
# Making predictions using the model

y_pred = lm.predict(X_test)<jupyter_output><empty_output><jupyter_text>Step 7: Model Evaluation<jupyter_code># Plotting y_test and y_pred to understand the spread.
fig = plt.figure(figsize=(8, 6))
plt.scatter(y_test,y_pred)
# Plot heading 
fig.suptitle('y_test vs y_pred', fontsize=20)
# X-label
plt.xlabel('y_test', fontsize=18)
# Y-label
plt.ylabel('y_pred', fontsize=16)
plt.show()
#r-square train
r2_score(y_true= y_train, y_pred= y_train_cnt)
#r-square test
r2_score(y_true= y_test, y_pred= y_pred)
#'MAE', 'MSE', 'RMSE' of the model.

print('MAE:', metrics.mean_absolute_error(y_test, y_pred))
print('MSE:', metrics.mean_squared_error(y_test, y_pred))
print('RMSE:', np.sqrt(metrics.mean_squared_error(y_test, y_pred)))<jupyter_output>MAE: 81517.55975242118
MSE: 10339659571.685184
RMSE: 101684.11661456859
StarcoderdataJupyterStructuredCleanDedup