-
Notifications
You must be signed in to change notification settings - Fork 0
/
classical_weather.py
173 lines (138 loc) · 6.25 KB
/
classical_weather.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
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import mean_squared_error, r2_score
# Pre-processing of the data
df_raw = pd.read_csv('assets/hourly_load&weather_data.csv', header=None, skiprows=1) # loading raw data from the CSV
y_test = df_raw[1].values/1000 # numpy array
# print("y_test: ", y_test.shape, "\n", y_test, "\n")
def simple_moving_average(n, y_test):
# Getting the predicted values for SMA
y_pred = pd.Series(y_test).rolling(window=n).mean().iloc[n - 1:].values
# print("Predicted values: ", y_pred, "\n")
mse_sma = mean_squared_error(y_test[n - 1:], y_pred)
# Plotting the results
fig = plt.figure(figsize=(60, 8))
plt.plot(y_test, label='Actual')
plt.plot(y_pred, label='Predicted')
plt.legend(loc='upper right')
plt.title("Simple Moving Average", fontsize=14)
plt.xlabel('Hour')
plt.ylabel('Electricity load')
plt.show()
fig.savefig('results/SMA_weather/final_output.jpg', bbox_inches='tight')
# Storing the result in a file: 'load_forecasting_result.txt'
predicted_test_result = y_pred
np.savetxt('results/SMA_weather/predicted_values.txt', predicted_test_result)
actual_test_result = y_test
np.savetxt('results/SMA_weather/test_values.txt', actual_test_result)
return mse_sma, y_pred
def weighted_moving_average(n, y_test):
y_pred = []
for i in range(len(y_test) - 4):
total = np.arange(1, n + 1, 1) # Weight Matrix for current being the heaviest
temp = y_test[i:i + n]
temp = total * temp
wma = (temp.sum()) / (total.sum())
y_pred = np.append(y_pred, wma)
# print("Predicted values: ", y_pred, "\n")
mse_wma = mean_squared_error(y_test[n - 1:], y_pred)
# Plotting the results
fig = plt.figure(figsize=(60, 8))
plt.plot(y_test, label='Actual')
plt.plot(y_pred, label='Predicted')
plt.legend(loc='upper right')
plt.title("Weighted Moving Average", fontsize=14)
plt.xlabel('Hour')
plt.ylabel('Electricity load')
plt.show()
fig.savefig('results/WMA_weather/final_output.jpg', bbox_inches='tight')
# Storing the result in a file: 'load_forecasting_result.txt'
predicted_test_result = y_pred
np.savetxt('results/WMA_weather/predicted_values.txt', predicted_test_result)
actual_test_result = y_test
np.savetxt('results/WMA_weather/test_values.txt', actual_test_result)
return mse_wma, y_pred
def cumulative_moving_average(y_test):
df = pd.DataFrame(y_test)
y_pred = df.expanding().mean()
# print("Predicted values: ", y_pred, "\n")
mse_cma = mean_squared_error(y_test, y_pred)
# Plotting the results
fig = plt.figure(figsize=(60, 8))
plt.plot(y_test, label='Actual')
plt.plot(y_pred, label='Predicted')
plt.legend(loc='upper right')
plt.title("Cumulative Moving Average", fontsize=14)
plt.xlabel('Hour')
plt.ylabel('Electricity load')
plt.show()
fig.savefig('results/CMA_weather/final_output.jpg', bbox_inches='tight')
# Storing the result in a file: 'load_forecasting_result.txt'
predicted_test_result = y_pred
np.savetxt('results/CMA_weather/predicted_values.txt', predicted_test_result)
actual_test_result = y_test
np.savetxt('results/CMA_weather/test_values.txt', actual_test_result)
return mse_cma, y_pred
def exponential_moving_average(y_test):
df = pd.DataFrame(y_test)
smoothing_factor = 0.5
y_pred = df.ewm(alpha=smoothing_factor, adjust=False).mean()
# print("Predicted values: ", y_pred, "\n")
mse_ema = mean_squared_error(y_test, y_pred)
# Plotting the results
fig = plt.figure(figsize=(60, 8))
plt.plot(y_test, label='Actual')
plt.plot(y_pred, label='Predicted')
plt.legend(loc='upper right')
plt.title("Exponential Moving Average", fontsize=14)
plt.xlabel('Hour')
plt.ylabel('Electricity load')
plt.show()
fig.savefig('results/EMA_weather/final_output.jpg', bbox_inches='tight')
# Storing the result in a file: 'load_forecasting_result.txt'
predicted_test_result = y_pred
np.savetxt('results/EMA_weather/predicted_values.txt', predicted_test_result)
actual_test_result = y_test
np.savetxt('results/EMA_weather/test_values.txt', actual_test_result)
return mse_ema, y_pred
print("---------------------------------------------------------")
n = 5 # Window size
mse_sma, y_sma = simple_moving_average(n, y_test)
print("MSE for SMA: ", mse_sma)
print('RMSE for SMA:', mean_squared_error(y_sma * 1000, y_test[n - 1:] * 1000, squared=False))
print('R-squared for SMA:', r2_score(y_sma, y_test[n - 1:]))
print('MAPE for SMA:', np.mean(np.abs((y_test[n-1:] - y_sma) / y_test[n-1:])) * 100)
print("---------------------------------------------------------")
mse_wma, y_wma = weighted_moving_average(n, y_test)
print("MSE for WMA: ", mse_wma)
print('RMSE for WMA:', mean_squared_error(y_wma * 1000, y_test[n - 1:] * 1000, squared=False))
print('R-squared for WMA:', r2_score(y_wma, y_test[n - 1:]))
print('MAPE for WMA:', np.mean(np.abs((y_test[n-1:] - y_wma) / y_test[n-1:])) * 100)
print("---------------------------------------------------------")
y = np.reshape(y_test, (364,1))
mse_cma, y_cma = cumulative_moving_average(y_test)
print("MSE for CMA: ", mse_cma)
print('RMSE for CMA:', mean_squared_error(y_cma * 1000, y * 1000, squared=False))
print('R-squared for CMA:', r2_score(y_cma, y_test))
print('MAPE for CMA:', np.mean(np.abs((y - y_cma) / y)) * 100)
print("---------------------------------------------------------")
mse_ema, y_ema = exponential_moving_average(y_test)
print("MSE for EMA: ", mse_ema)
print('RMSE for EMA:', mean_squared_error(y_ema * 1000, y_test * 1000, squared=False))
print('R-squared for EMA:', r2_score(y_ema, y_test))
print('MAPE for EMA:', np.mean(np.abs((y - y_ema) / y)) * 100)
print("---------------------------------------------------------")
# Plotting the results
fig = plt.figure(figsize=(60, 8))
plt.plot(y_sma, label='SMA')
plt.plot(y_cma, label='CMA')
plt.plot(y_ema, label='EMA')
plt.plot(y_wma, label='WMA')
plt.plot(y_test, label='Actual Values')
plt.legend(loc='upper right')
plt.xlabel('Hour')
plt.ylabel('Electricity load')
plt.title("Predicted Values of various classical methods", fontsize=14)
plt.show()
fig.savefig('results/ClassicalWeather_final_output.jpg', bbox_inches='tight')