0% found this document useful (0 votes)
6 views

Linear Regression.py

The document outlines a process for performing linear regression analysis on a house price dataset using Python in Google Colab. It includes steps for data preparation, model training, and evaluation metrics such as Mean Squared Error (MSE) and R squared value. Additionally, it provides a visualization of the dataset and predictions for house prices based on size.

Uploaded by

Daniyal Shahbaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Linear Regression.py

The document outlines a process for performing linear regression analysis on a house price dataset using Python in Google Colab. It includes steps for data preparation, model training, and evaluation metrics such as Mean Squared Error (MSE) and R squared value. Additionally, it provides a visualization of the dataset and predictions for house prices based on size.

Uploaded by

Daniyal Shahbaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 2

#Step1.

Remove blank row and columns


#Step2. File save as CSV
#Step3. Upload it on colab file upload
#Step4. Run the command
import os
print(os.listdir()) # Lists all files in current directory
import os
print(os.getcwd()) # Prints current working directory
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
import math
from google.colab import files
uploaded = files.upload() # Use the UI to select your file
dataset = pd.read_csv('Dataset_House_Price.csv')

#read .csv into a DataFrame


#dataset = pd.read_csv('C:\\Users\\User\\Desktop\\house_prices.csv')
dataset = pd.read_csv('Dataset_House_Price.csv')
size=dataset['sqft_living']
price=dataset['price']

#machine learing handle arrays not dataframes


x = np.array(size).reshape(-1,1)
y = np.array(price).reshape(-1,1)

#we use Linear Regression + fit() is the training


model = LinearRegression()
model.fit(x, y)

#MSE and R value


regression_model_mse = mean_squared_error(x, y)
print("MSE: ", math.sqrt(regression_model_mse))
print("R squared value:", model.score(x,y))

#we can get the b values after the model fit


#this is the b0
print(model.coef_[0])
#this is b1 in our model
print(model.intercept_[0])

#visualize the dataset with the fitted model


plt.scatter(x, y, color= 'green')
plt.plot(x, model.predict(x), color = 'black')
plt.title ("Linear Regression")
plt.xlabel("Size")
plt.ylabel("Price")
plt.show()

#Predicting the prices


print("Prediction by the model:" , model.predict([[2000]]))

You might also like