Wednesday, April 13, 2022

Predictive Model with Linear Regression

 This demo program will show how to create a predictive model using Linear Regression.

The Ouput:


Predicted value when x = 100:


The code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
import joblib
import warnings
warnings.filterwarnings('ignore')
X = np.array([5, 10, 20, 30, 40, 50, 10, 25, 30, 45, 50, 15, 20, 35, 40, 50, 50, 45, 30, 25, 20, 10, 66])
Y = np.array([50, 95, 185, 280, 370, 490, 100, 230, 290, 410, 500, 135, 200, 295, 395, 495, 480, 430, 305, 205, 175, 110, 600])

data = pd.DataFrame({'X':X, 'Y':Y})
data.head()
sns.jointplot(X, Y, kind='reg')
plt.show()
sns.regplot(x='X', y='Y', data=data)
plt.title("Regression Plot XY")
model = LinearRegression().fit(X.reshape(-1, 1), Y)
filename = "model.sav"
joblib.dump(model, filename)
loaded_model = joblib.load(filename)
loaded_model.predict([[100]])

No comments:

Post a Comment