0% found this document useful (0 votes)
11 views9 pages

IoT Task4 21BEC0384

This document describes building a linear regression model to predict housing prices based on features like area and price. It includes data exploration, feature selection, splitting data, building a linear regression model, evaluating model performance, and making predictions on new data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views9 pages

IoT Task4 21BEC0384

This document describes building a linear regression model to predict housing prices based on features like area and price. It includes data exploration, feature selection, splitting data, building a linear regression model, evaluating model performance, and making predictions on new data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

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 matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_error
import matplotlib.pyplot as plt
from sklearn.metrics import r2_score

df=pd.read_csv('/content/drive/MyDrive/Housing.csv')

df.columns

print(df.head())

x=df['price']
y=df['area']

x=x.values.reshape(-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()
model.fit(x_train,y_train)

predictions = model.predict(x_test)

print(predictions)

plt.scatter(y_test, predictions, color='blue')


plt.plot([min(y_test),max(y_test)],[min(y_test),max(y_test)],
color='red', linestyle='--')
plt.xlabel('Actual values')
plt.ylabel('Predicted values')
plt.title('Actual vs predicted values')
plt.show()
from sklearn.preprocessing import PolynomialFeatures

poly = PolynomialFeatures(degree=4)
X_poly = poly.fit_transform(x)

poly.fit(X_poly, y)
lin2 = LinearRegression()
lin2.fit(X_poly, y)

predictions = model.predict(x_test)

print(predictions)

plt.scatter(x, y, color='blue')
plt.plot(x, lin2.predict(poly.fit_transform(x)),
color='red')
plt.title('Polynomial Regression')
plt.xlabel('Price')
plt.ylabel('Area')

plt.show()

pred2 = 78895
pred2array = np.array([[pred2]])
lin2.predict(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 sklearn.metrics 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 = np.array([[12250000,8960]])
new_data_poly = poly.transform(new_data)

predicted_price = model.predict(new_data_poly)
print(predicted_price)
Outputs
Verification
Reference
Housing Price Prediction ( Linear Regression ) (kaggle.com)

You might also like