Showing Relationship between X and Y Values using Linear Regression Line in Python - Machine Learning

Importing Libraries for this project

import numpy as np 

import pandas as pd from sklearn.linear_

model import LinearRegression 

import matplotlib.pyplot as plt 


Importing Data file into python code

 filepath = 'PythonRegressionPracticeWorkbook.csv' 

data = pd.read_csv(filepath) 

print(data) 


Assigning Input / Independent Column to X and assigning dependent / Output Column to Y variables

 x = data[['Square Feet']] 

y = data['Price'] 


Training the X and Y variables using Linear Regression Model to find how X affects Y

model = LinearRegression() 

model.fit(x,y) 


Checking Slope, Intercept values from the learned Model

slope = model.coef_[0] 

intercept = model.intercept_ 

print("Slope:", slope) 

print("Intercept:",intercept) 


Predicting the output value / Y value for each X value / input value

 y_pred = model.predict(x) 


Plotting Scatter plot with Regression Line to show both Original X and Y values, Predicted Y values

 plt.figure(figsize=(12,8)) 

plt.scatter(x,y, color='blue', label='Actual Data') plt.plot(x,y_pred, color='red', label='Regression Line')

 for i in range(len(x)): 

 plt.text(x.iloc[i,0],y.iloc[i]+10,f"({x.iloc[i,0]},{y.iloc[i]})",color='blue') 

for i in range(len(x)): 

 plt.text(x.iloc[i,0], y_pred[i] - 10, f"({x.iloc[i,0]},{round(y_pred[i],1)})", color='red') 

plt.xlabel('Square feet') 

plt.ylabel('Price') 

plt.legend() 

plt.show()


R Squared value tells how well the regression line fits the data, 1 - good fit, 0 - poor fit

 r2 = model.score(x,y) 

print("r_squared:",r2)




Comments

Popular posts from this blog

What is Artificial Intelligence? What is Machine Learning? What is Data Science? how they are related to each other?

Linear Algebra - What is it?

What is a Python Library?