-
Notifications
You must be signed in to change notification settings - Fork 0
/
Delivery_Time model.py
167 lines (123 loc) · 3.61 KB
/
Delivery_Time model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# Step -1 Import data files
import numpy as np
import pandas as pd
df = pd.read_csv("D:\\DS\\books\\ASSIGNMENTS\\Simple Linear Regression\\delivery_time.csv")
df
df.head()
df.shape
df.isnull().sum()
df.describe()
# Step - 2 Split the variables in X and Y
Y = df[["Delivery Time"]]
X = df[["Sorting Time"]]
#EDA
# Scatter Plot
import matplotlib.pyplot as plt
plt.scatter (X.iloc[:,0],Y,color = 'red')
plt.xlabel("Sorting Time")
plt.ylabel("Delivery Time")
plt.show()
# Boxplot
plt.boxplot(df["Delivery Time"])
plt.boxplot(df["Sorting Time"])
# Histogram
plt.hist(df["Delivery Time"],bins=5)
plt.hist(df["Sorting Time"],bins=5)
df.corr()
# Model Fitting
from sklearn.linear_model import LinearRegression
LR = LinearRegression()
LR.fit(X,Y)
LR.intercept_ #Bo
LR.coef_ #B1
# Predict the value
Y_pred = LR.predict(X)
Y_pred
# Scatter Plot with Plot
plt.scatter (X.iloc[:,0],Y,color = 'red')
plt.plot (X.iloc[:,0],Y_pred,color = 'blue')
plt.xlabel("Sorting Time")
plt.ylabel("Delivery Time")
plt.show()
# Then Finding Error
from sklearn.metrics import mean_squared_error,r2_score
mse = mean_squared_error(Y, Y_pred)
RMSE = np.sqrt(mse)
print("Root mean square error: ", RMSE.round(3))
print("Rsquare: ", r2_score(Y, Y_pred).round(3)*100)
"""
There is a Difference of RMSE is 2.792 and the R2 is 0.682
"""
# Transformation
# Model 2
from sklearn.linear_model import LinearRegression
LR=LinearRegression()
LR.fit(np.log(X),Y)
y1 = LR.predict(np.log(X))
mse= mean_squared_error(Y, y1)
RMSE=np.sqrt(mse).round(3)
print("Root mean square error: ", RMSE)
print("Rsquare: ", r2_score(Y, y1).round(3)*100)
plt.scatter (X.iloc[:,0],Y,color = 'red')
plt.plot (X.iloc[:,0],y1,color = 'blue')
plt.xlabel("Sorting Time")
plt.ylabel("Delivery Time")
plt.show()
"""
There is a Difference of RMSE is 2.733 and the R2 is 0.695
"""
# Model 3
from sklearn.linear_model import LinearRegression
LR=LinearRegression()
LR.fit(np.sqrt(X),Y)
y1 = LR.predict(np.sqrt(X))
mse= mean_squared_error(Y, y1)
RMSE=np.sqrt(mse).round(3)
print("Root mean square error: ", RMSE)
print("Rsquare: ", r2_score(Y, y1).round(3)*100)
plt.scatter (X.iloc[:,0],Y,color = 'red')
plt.plot (X.iloc[:,0],y1,color = 'blue')
plt.xlabel("Sorting Time")
plt.ylabel("Delivery Time")
plt.show()
"""
There is a Difference of RMSE is 2.732 and the R2 is 0.696
"""
# Model 4
from sklearn.linear_model import LinearRegression
LR=LinearRegression()
LR.fit(X**2,Y)
y1 = LR.predict(X**2)
mse= mean_squared_error(Y, y1)
RMSE=np.sqrt(mse).round(3)
print("Root mean square error: ", RMSE)
print("Rsquare: ", r2_score(Y, y1).round(3)*100)
plt.scatter (X.iloc[:,0],Y,color = 'red')
plt.plot (X.iloc[:,0],y1,color = 'blue')
plt.xlabel("Sorting Time")
plt.ylabel("Delivery Time")
plt.show()
"""
There is a Difference of RMSE is 3.011 and the R2 is 0.630
"""
# Model 5
from sklearn.linear_model import LinearRegression
LR=LinearRegression()
LR.fit(X**3,Y)
y1 = LR.predict(X**3)
mse= mean_squared_error(Y, y1)
RMSE=np.sqrt(mse).round(3)
print("Root mean square error: ", RMSE)
print("Rsquare: ", r2_score(Y, y1).round(3)*100)
plt.scatter (X.iloc[:,0],Y,color = 'red')
plt.plot (X.iloc[:,0],y1,color = 'blue')
plt.xlabel("Sorting Time")
plt.ylabel("Delivery Time")
plt.show()
"""
There is a Difference of RMSE is 3.253 and the R2 is 0.5689
"""
"""
Inference : Delivery time is predicted using sorting time and the best model selected is model 3
which is transformed using squared tranformation because the graph is bell shaped and its rscore is 0.69580.
"""