Name : Aditya Bonnerjee
Reg No. : 21BEC0384
Course Name : IoT Domain Analytics Lab
Course Code : BECE352E
Faculty Name : Pradheep T
Slot : L49+L50
Lab task 4
Task
1. Objective:
• Build a linear regression model to predict housing prices based
on relevant features.
2. Dataset:
• Use the given dataset
3. Steps:
• Data Exploration and Preprocessing:
1.] Load the dataset.
2.] Explore features and target variable (housing price index).
3.] Handle missing values.
4.] Encode categorical variables if needed.
• Feature Selection:
1.] Choose relevant features impacting housing prices.
2.] Remove unnecessary features.
• Data Splitting:
1.] Split data into training and testing sets.
• Model Building:
1.] Create a simple linear regression model.
2.] Train the model using the training data.
• Model Evaluation:
1.] Evaluate model performance (MAE, MSE,) on testing data
Components Required
Google Collaborator
Code
import pandas as pd
import numpy as np
import [Link] as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from [Link] import mean_squared_error
from [Link] import mean_absolute_error
import [Link] as plt
from [Link] import r2_score
df=pd.read_csv('/content/drive/MyDrive/[Link]')
[Link]
print([Link]())
x=df['price']
y=df['area']
x=[Link](-1,1)
x_train, x_test, y_train, y_test = train_test_split(x, y,
test_size=0.2, random_state=42)
print(x_train.shape)
print(x_test.shape)
model = LinearRegression()
[Link](x_train,y_train)
predictions = [Link](x_test)
print(predictions)
[Link](y_test, predictions, color='blue')
[Link]([min(y_test),max(y_test)],[min(y_test),max(y_test)],
color='red', linestyle='--')
[Link]('Actual values')
[Link]('Predicted values')
[Link]('Actual vs predicted values')
[Link]()
from [Link] import PolynomialFeatures
poly = PolynomialFeatures(degree=4)
X_poly = poly.fit_transform(x)
[Link](X_poly, y)
lin2 = LinearRegression()
[Link](X_poly, y)
predictions = [Link](x_test)
print(predictions)
[Link](x, y, color='blue')
[Link](x, [Link](poly.fit_transform(x)),
color='red')
[Link]('Polynomial Regression')
[Link]('Price')
[Link]('Area')
[Link]()
pred2 = 78895
pred2array = [Link]([[pred2]])
[Link](poly.fit_transform(pred2array))
mse = mean_squared_error(y_test, predictions)
print("Mean Squared Error: ", mse)
mae = mean_absolute_error(y_test, predictions)
print("Mean Absolute Error: ", mae)
from [Link] import r2_score
r_squared = r2_score(y_test,predictions)
print('R-squared: ', r_squared)
x = df[['price', 'area']].values
y = df[['parking']].values
new_data = [Link]([[12250000,8960]])
new_data_poly = [Link](new_data)
predicted_price = [Link](new_data_poly)
print(predicted_price)
Outputs
Verification
Reference
Housing Price Prediction ( Linear Regression ) ([Link])