-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain_Prediction.py
More file actions
393 lines (278 loc) · 12.3 KB
/
Main_Prediction.py
File metadata and controls
393 lines (278 loc) · 12.3 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
import wx
import os
import time
import sys
# In[ ]:
input_max_temp = input("Please input maximum of temperature: ")
input_min_temp = input("Please input minimum of temperature: ")
input_meandew = input("Please input mean dew point: ")
input_meanhum = input("Please input mean humidity: ")
input_pressure = input("Please input mean pressure: ")
input_meancloud = input("Please input mean cloud: ")
input_rainfall = input("Please input mean rainfall: ")
input_population = input("Please input population density: ")
input_sunshine = input("Please input mean number of sunshine hour: ")
input_wind_dir = input("Please input mean wind direction: ")
input_wind_speed = input("Please input mean wind speed: ")
input_air_quality = input("Please input mean air health quality: ")
# In[ ]:
if (True):
# !/usr/bin/env python
# coding: utf-8
# In[1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as seabornInstance
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor
from sklearn.neighbors import KNeighborsRegressor
from sklearn import metrics
# In[2]:
dataset = pd.read_csv('data_2.csv')
# In[3]:
dataset.shape
# In[4]:
dataset.describe()
# In[5]:
dataset.isnull().any()
# In[6]:
dataset = dataset.fillna(method='ffill')
# In[7]:
dataset.plot(x='pressure', y='mean_temp', style='o')
plt.title('Pressure vs Mean Temperature')
plt.xlabel('pressure')
plt.ylabel('mean_temp')
plt.savefig("statistics/pressure.png")
plt.show()
dataset.plot(x='max_temp', y='mean_temp', style='o')
plt.title('Maximum Temperature vs Mean Temperature')
plt.xlabel('max_temp')
plt.ylabel('mean_temp')
plt.savefig("statistics/max_temp.png")
plt.show()
dataset.plot(x='min_temp', y='mean_temp', style='o')
plt.title('Minimum Temperature vs Mean Temperature')
plt.xlabel('min_temp')
plt.ylabel('mean_temp')
plt.savefig("statistics/min_temp.png")
plt.show()
dataset.plot(x='meandew', y='mean_temp', style='o')
plt.title('Mean Dew Point vs Mean Temperature')
plt.xlabel('meandew')
plt.ylabel('mean_temp')
plt.savefig("statistics/meandew.png")
plt.show()
dataset.plot(x='meanhum', y='mean_temp', style='o')
plt.title('Mean Humidity vs Mean Temperature')
plt.xlabel('meanhum')
plt.ylabel('mean_temp')
plt.savefig("statistics/meanhum.png")
plt.show()
dataset.plot(x='meancloud', y='mean_temp', style='o')
plt.title('Mean Cloud vs Mean Temperature')
plt.xlabel('meancloud')
plt.ylabel('mean_temp')
plt.savefig("statistics/meancloud.png")
plt.show()
dataset.plot(x='rainfall', y='mean_temp', style='o')
plt.title('Rainfall vs Mean Temperature')
plt.xlabel('rainfall')
plt.ylabel('mean_temp')
plt.savefig("statistics/rainfall.png")
plt.show()
dataset.plot(x='population', y='mean_temp', style='o')
plt.title('Population vs Mean Temperature')
plt.xlabel('Population')
plt.ylabel('mean_temp')
plt.savefig("statistics/population.png")
plt.show()
dataset.plot(x='sunshine_hour', y='mean_temp', style='o')
plt.title('Bright Sunshine vs Mean Temperature')
plt.xlabel('sunshine_hour')
plt.ylabel('mean_temp')
plt.savefig("statistics/sunshine.png")
plt.show()
dataset.plot(x='wind_direction', y='mean_temp', style='o')
plt.title('Wind Direction vs Mean Temperature')
plt.xlabel('wind_direction')
plt.ylabel('mean_temp')
plt.savefig("statistics/wind_direction.png")
plt.show()
dataset.plot(x='wind_speed', y='mean_temp', style='o')
plt.title('Wind Speed vs Mean Temperature')
plt.xlabel('wind_speed')
plt.ylabel('mean_temp')
plt.savefig("statistics/wind_speed.png")
plt.show()
dataset.plot(x='air_health_quality', y='mean_temp', style='o')
plt.title('Air Health Quality vs Mean Temperature')
plt.xlabel('air_health_quality')
plt.ylabel('mean_temp')
plt.savefig("statistics/air_quality.png")
plt.show()
# In[8]:
X = dataset[['pressure', 'max_temp', 'min_temp', 'meandew', 'meanhum', 'meancloud', 'rainfall', 'population',
'sunshine_hour', 'wind_direction', 'wind_speed', 'air_health_quality']]
y = dataset['mean_temp']
# In[9]:
plt.figure(figsize=(15, 10))
plt.tight_layout()
seabornInstance.distplot(dataset['mean_temp'])
# In[10]:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
# In[11]:
print("Linear Regression Prediction: ")
regressor = LinearRegression()
regressor.fit(X_train, y_train)
# In[12]:
coeff_df = pd.DataFrame(regressor.coef_, X.columns, columns=['Coefficient'])
coeff_df.sort_values(by='Coefficient', ascending=False)
# In[13]:
pos_coeffs_df = coeff_df[(coeff_df['Coefficient'] >= 0)].sort_values(by='Coefficient', ascending=False)
# pos_coeffs_df.sort_values(by='Estimated_Coefficients', ascending=False)
pos_coeffs_df
# In[14]:
pos_coeffs_df = coeff_df[(coeff_df['Coefficient'] < 0)].sort_values(by='Coefficient', ascending=True)
# pos_coeffs_df.sort_values(by='Estimated_Coefficients', ascending=False)
pos_coeffs_df
# In[15]:
y_pred = regressor.predict(X_test)
# In[16]:
import seaborn as sns
g = sns.regplot(y_pred, y=y_test, fit_reg=True)
g.set(xlabel='Predicted Mean Temperature', ylabel='Actual Mean Temperature', title='Model Predictions')
plt.title('Regression Plot for Actual vs Predicted Values')
# In[17]:
df = pd.DataFrame({'Actual': y_test, 'Predicted': y_pred})
df1 = df.head(25)
df1
# In[18]:
df1.plot(kind='bar', figsize=(10, 8))
plt.grid(which='major', linestyle='-', linewidth='0.5', color='green')
plt.grid(which='minor', linestyle=':', linewidth='0.5', color='black')
plt.savefig("statistics/linear_regression_comparison.png")
plt.show()
# In[19]:
# R2 for train and test data
R2_reg_train = regressor.score(X_train, y_train)
R2_reg_test = regressor.score(X_test, y_test)
print('R squared for train data is: %.3f' % (R2_reg_train))
print('R squared for test data is: %.3f' % (R2_reg_test))
# In[20]:
from math import sqrt
RMSE_reg_train = sqrt(np.mean((y_train - regressor.predict(X_train)) ** 2))
RMSE_reg_test = sqrt(np.mean((y_test - regressor.predict(X_test)) ** 2))
print('Root mean squared error for train data is: %.3f' % (RMSE_reg_train))
print('Root mean sqaured error for test data is: %.3f' % (RMSE_reg_test))
# In[21]:
print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, y_pred))
print('Mean Squared Error:', metrics.mean_squared_error(y_test, y_pred))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
# In[27]:
# input_pressure = 1000
# input_max_temp = 30
# input_min_temp = 25
# input_meandew = 25
# input_meanhum = 80
estimated_temp = regressor.predict([[float(input_pressure),float(input_max_temp),float(input_min_temp),float(input_meandew),float(input_meanhum),float(input_meancloud),float(input_rainfall),int(input_population),float(input_sunshine),float(input_wind_dir),float(input_wind_speed),float(input_air_quality)]])
print ("The expected mean of temperature is", estimated_temp)
print(" ")
print("K-Nearest Neighbors Prediction: ")
knn = KNeighborsRegressor(n_neighbors=3)
knn.fit(X_train, y_train)
# In[12]:
pred_knn = knn.predict(X_test)
pred_knn
y_pred = knn.predict(X_test)
# In[16]:
import seaborn as sns
g = sns.regplot(y_pred, y=y_test, fit_reg=True)
g.set(xlabel='Predicted Mean Temperature', ylabel='Actual Mean Temperature', title='Model Predictions')
plt.title('Regression Plot for Actual vs Predicted Values')
# In[17]:
df = pd.DataFrame({'Actual': y_test, 'Predicted': y_pred})
df1 = df.head(25)
df1
# In[18]:
df1.plot(kind='bar', figsize=(10, 8))
plt.grid(which='major', linestyle='-', linewidth='0.5', color='green')
plt.grid(which='minor', linestyle=':', linewidth='0.5', color='black')
plt.savefig("statistics/KNN_comparison.png")
plt.show()
# In[19]:
# R2 for train and test data
# R2 for train and test data
R2_reg_train = knn.score(X_train, y_train)
R2_reg_test = knn.score(X_test, y_test)
print('R squared for train data is: %.3f' % (R2_reg_train))
print('R squared for test data is: %.3f' % (R2_reg_test))
# In[20]:
from math import sqrt
RMSE_reg_train = sqrt(np.mean((y_train - knn.predict(X_train)) ** 2))
RMSE_reg_test = sqrt(np.mean((y_test - knn.predict(X_test)) ** 2))
print('Root mean squared error for train data is: %.3f' % (RMSE_reg_train))
print('Root mean sqaured error for test data is: %.3f' % (RMSE_reg_test))
# In[21]:
print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, y_pred))
print('Mean Squared Error:', metrics.mean_squared_error(y_test, y_pred))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
# In[27]:
# input_pressure = 1000
# input_max_temp = 30
# input_min_temp = 25
# input_meandew = 25
# input_meanhum = 80
estimated_temp = knn.predict([[float(input_pressure),float(input_max_temp),float(input_min_temp),float(input_meandew),float(input_meanhum),float(input_meancloud),float(input_rainfall),int(input_population),float(input_sunshine),float(input_wind_dir),float(input_wind_speed),float(input_air_quality)]])
print ("The expected mean of temperature is", estimated_temp)
# In[ ]:
print(" ")
print("Random Forest Regression Prediction: ")
rf = RandomForestRegressor(random_state=5, n_estimators=20)
rf.fit(X_train, y_train)
# In[12]:
pred_rf = rf.predict(X_test)
pred_rf
y_pred = rf.predict(X_test)
# In[16]:
import seaborn as sns
g = sns.regplot(y_pred, y=y_test, fit_reg=True)
g.set(xlabel='Predicted Mean Temperature', ylabel='Actual Mean Temperature', title='Model Predictions')
plt.title('Regression Plot for Actual vs Predicted Values')
# In[17]:
df = pd.DataFrame({'Actual': y_test, 'Predicted': y_pred})
df1 = df.head(25)
df1
# In[18]:
df1.plot(kind='bar', figsize=(10, 8))
plt.grid(which='major', linestyle='-', linewidth='0.5', color='green')
plt.grid(which='minor', linestyle=':', linewidth='0.5', color='black')
plt.savefig("statistics/random_forest_comparison.png")
plt.show()
# In[19]:
# R2 for train and test data
# R2 for train and test data
# R2 for train and test data
R2_reg_train = rf.score(X_train, y_train)
R2_reg_test = rf.score(X_test, y_test)
print('R squared for train data is: %.3f' % (R2_reg_train))
print('R squared for test data is: %.3f' % (R2_reg_test))
# In[20]:
from math import sqrt
RMSE_reg_train = sqrt(np.mean((y_train - rf.predict(X_train)) ** 2))
RMSE_reg_test = sqrt(np.mean((y_test - rf.predict(X_test)) ** 2))
print('Root mean squared error for train data is: %.3f' % (RMSE_reg_train))
print('Root mean sqaured error for test data is: %.3f' % (RMSE_reg_test))
# In[21]:
print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, y_pred))
print('Mean Squared Error:', metrics.mean_squared_error(y_test, y_pred))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
# In[27]:
estimated_temp = rf.predict([[float(input_pressure),float(input_max_temp),float(input_min_temp),float(input_meandew),float(input_meanhum),float(input_meancloud),float(input_rainfall),int(input_population),float(input_sunshine),float(input_wind_dir),float(input_wind_speed),float(input_air_quality)]])
print ("The expected mean of temperature is", estimated_temp)
# In[ ]:
# In[ ]: