-
Notifications
You must be signed in to change notification settings - Fork 0
/
prophet_predict.py
325 lines (163 loc) · 7.18 KB
/
prophet_predict.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
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
#!/usr/bin/env python
# coding: utf-8
# In[518]:
import pandas as pd
import numpy as np
from prophet import Prophet
from prophet.plot import plot_plotly, plot_components_plotly
import plotly.offline as py
import matplotlib.pyplot as plt
py.init_notebook_mode()
get_ipython().run_line_magic('matplotlib', 'inline')
plt.style.use('fivethirtyeight')
# Load the data
# In[519]:
input_file = 'data/processed_data.csv'
# Read the CSV file
# In[520]:
df = pd.read_csv(input_file)
# In[521]:
df.head()
# In[522]:
df.info()
# Rename the columns for better readability
# In[523]:
df.columns = ['DateTime', 'Year', 'Month', 'Date', 'Time', 'Minute', 'Temperature', 'Previous Day Average', 'Two Days Before Average', 'Three Days Before average', 'Last 7 Days Average', 'Previous Day Wind Speed', 'Previous Day Rainfall']
# Convert the 'Date' and 'Time' columns to integers
# In[524]:
df['Date'] = df['Date'].astype(int)
df['Time'] = df['Time'].astype(int)
# Fill leading zeros for the 'Time' column
# In[525]:
df['Time'] = df['Time'].apply(lambda x: str(x).zfill(4))
# Combine the 'Date' and 'Time' columns into a single 'DateTime' column<br>
# df['DateTime'] = pd.to_datetime(df['Date'].astype(str) + df['Time'], format='%Y%m%d%H%M')
# Remove rows with a specific value (e.g., 32767) in 'Temperature' column
# In[526]:
df = df[df['Temperature'] != 32767]
# Calculate the mean and standard deviation of Y
# In[527]:
threshold = 5
mean_Y = np.mean(df['Temperature'])
std_Y = np.std(df['Temperature'])
# Define the range of acceptable Y values
# In[528]:
lower_bound = mean_Y - threshold * std_Y
upper_bound = mean_Y + threshold * std_Y
# Filter out rows with Y values outside the acceptable range
# In[529]:
df = df[(df['Temperature'] >= lower_bound) & (df['Temperature'] <= upper_bound)]
# Divide the 'Temperature' column by 10 to convert it to degrees Celsius
# In[530]:
# df['Temperature'] = df['Temperature'] / 10
# Prepare the data for Prophet
# In[531]:
df['DateTime'] = pd.to_datetime(df['DateTime'].astype(int).astype(str) + df['Time'].astype(int).astype(str) + df['Minute'].astype(str), format='%Y%m%d%H%M')
prophet_df = df.copy()
prophet_df.rename(columns={'DateTime': 'ds', 'Temperature': 'y'}, inplace=True)
prophet_df.dropna(inplace=True)
prophet_df = prophet_df[prophet_df['Month'] == 7]
# In[532]:
prophet_df.head()
# In[533]:
prophet_df.info()
# In[534]:
ax = prophet_df[["ds", "y"]].set_index('ds').plot(figsize=(12, 8))
ax.set_ylabel('Temperature')
ax.set_xlabel('Date')
plt.show()
# Split the dataset into training and validation sets (80% for training, 20% for validation)
# In[535]:
train_df = prophet_df # .loc[prophet_df['ds'] < '2022-07-01']
validation_df = prophet_df.loc[(prophet_df['ds'] >= '2022-07-01') & (prophet_df['ds'] < '2022-08-01')]
# In[536]:
validation_df.reset_index(drop=True, inplace=True)
# Initialize and fit the Prophet model
# In[537]:
model = Prophet(
interval_width=0.95, # Increase the interval width to capture more uncertainty
n_changepoints=250, # Increase the number of changepoints for more flexibility
yearly_seasonality=True, # Keep yearly seasonality
weekly_seasonality=False, # Add weekly seasonality
daily_seasonality=True, # Add daily seasonality
changepoint_prior_scale=20, # Reduce the changepoint prior scale for smoother trends
seasonality_mode='additive', # Use multiplicative seasonality for better handling of varying scales
# changepoint_range=0.8, # Restrict changepoints to the first 80% of the data
seasonality_prior_scale=10,
# growth='logistic'
)
model.add_regressor('Previous Day Average')
model.add_regressor('Two Days Before Average')
model.add_regressor('Three Days Before average')
model.add_regressor('Last 7 Days Average')
model.add_regressor('Previous Day Wind Speed')
model.add_regressor('Previous Day Rainfall')
model.add_regressor('Time')
model.add_seasonality(name='daily', period=1, fourier_order=10)
model.add_seasonality(name='hourly', period=1/24, fourier_order=10)
model.fit(train_df)
# Make predictions for the validation dataset
# In[ ]:
validation_predictions = model.predict(validation_df)
# Calculate the mean squared error (MSE) for the validation set
# In[ ]:
mse = np.mean((validation_df['y'] - validation_predictions['yhat']) ** 2)
print("Mean Squared Error (MSE) for the validation set:", mse)
mas = np.mean(abs(validation_df['y'] - validation_predictions['yhat']))
print("Mean Absolute Error (MAE) for the validation set:", mas)
# accuracy =
# Generate future date times for prediction
# In[ ]:
future_dates = pd.date_range(start='2022-07-01', periods=30000, freq='1min') # Adjust the start date and number of periods as needed
# Create a dataframe with the future dates
# In[ ]:
future_df = pd.DataFrame({'ds': future_dates})
# Use the trained model to make predictions
# In[ ]:
predictions = model.predict(validation_df)
predictions[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].head()
# In[ ]:
all_predictions = model.predict(prophet_df.dropna(inplace=True))
# In[ ]:
model.plot(all_predictions, uncertainty=True)
# Print the predicted temperatures for the future dates
# In[ ]:
print(predictions[['ds', 'yhat']].tail(10)) # Adjust the number of rows to display as needed
# Print the actual and predicted temperatures for the future dates
# In[ ]:
# actual_values = df.loc[df['DateTime'].isin(future_dates)]
# predicted_values = predictions.loc[predictions['ds'].isin(actual_values['DateTime'])]['yhat'].values
# comparison_df = pd.DataFrame({'DateTime': actual_values['DateTime'].values, 'Actual': actual_values["Temperature"].values, 'Predicted': predicted_values})
# print(comparison_df)
# Calculate accuracy (optional, depending on the desired accuracy metric)
# In[ ]:
# accuracy = np.mean(np.abs(actual_values['Temperature'].values - predicted_values) / actual_values['Temperature'].values)
# print("Accuracy:", (1 - accuracy) * 100, "%")
# Visualize the actual vs. predicted temperatures for the validation set
# In[ ]:
plt.figure(figsize=(12, 6))
plt.plot(validation_df['ds'], validation_df['y'], label='Actual')
plt.plot(validation_predictions['ds'], validation_predictions['yhat'], label='Predicted')
plt.xlabel('Date')
plt.ylabel('Temperature (°C)')
plt.title('Actual vs. Predicted Temperatures (Validation Set)')
plt.legend()
plt.grid(True)
plt.show()
# In[ ]:
# In[ ]:
plt.figure(figsize=(12, 6))
# condition = '2021-12-31' < prophet_df['ds'] < '2023-01-01'
plt.plot(prophet_df[(prophet_df['ds'] < '2023-01-01') & (prophet_df['ds'] > '2021-12-31')]['ds'], prophet_df[(prophet_df['ds'] < '2023-01-01') & (prophet_df['ds'] > '2021-12-31')]['y'], label='Actual')
plt.plot(all_predictions[(all_predictions['ds'] < '2023-01-01') & (all_predictions['ds'] > '2021-12-31')]['ds'], all_predictions[(all_predictions['ds'] < '2023-01-01') & (all_predictions['ds'] > '2021-12-31')]['yhat'], label='Predicted')
plt.xlabel('Date')
plt.ylabel('Temperature (°C)')
plt.title('Actual vs. Predicted Temperatures (Validation Set)')
plt.legend()
plt.grid(True)
plt.show()
# In[ ]:
import pickle
# Assuming you have trained the Prophet model and stored it in a variable called 'model'
with open('prophet_model.pkl', 'wb') as f:
pickle.dump(model, f)