-
Notifications
You must be signed in to change notification settings - Fork 11
/
ghi_model_fig.py
171 lines (116 loc) · 4.28 KB
/
ghi_model_fig.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
# coding: utf-8
# In[1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import linear_model, ensemble, svm, tree, neural_network
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import MinMaxScaler, StandardScaler
from sklearn.metrics import mean_squared_error, mean_absolute_error, make_scorer
from sklearn.model_selection import cross_val_score, train_test_split, GridSearchCV
import warnings
warnings.filterwarnings("ignore")
# In[2]:
result = {}
# In[3]:
hhids=[26, 59, 77, 86, 93, 94, 101, 114, 171, 187]
for hhid in hhids:
result[hhid] = []
print('Start :: Process on household {}...'.format(hhid))
df = pd.read_csv('data_added2/added_hhdata_{}_2.csv'.format(hhid), index_col=0)
st = []
ct = 0
for idx, row in df.iterrows():
if row.GH < 2000 and row.GH > -1000:
st.append(row)
else:
ct += 1
# print(ct)
df = pd.DataFrame(data=st, columns=df.columns)
features = ['use', 'temperature', 'cloud_cover','wind_speed','is_weekday','ac_hour','ac_week','month','hour']
Y = list(df.ac)[1:]
Y.append(df.ac.iloc[0])
Y = np.array(Y)
X = df[features]
X = np.array(X)
X.shape
# temp_df = pd.DataFrame(data=X, columns=features)
# temp_df['y_GH'] = Y
# values = temp_df.values
# # normalize features
# scaler = MinMaxScaler()
# y_gt = values[:,-1:]
# scaled = scaler.fit_transform(values)
# values = scaled
X_train, X_test, Y_train, Y_test = train_test_split(X, Y,
test_size=0.10,
random_state=666)
classifiers = [
linear_model.Ridge(alpha=1.0, random_state=0),
linear_model.Lasso(alpha=0.55, random_state=0),
linear_model.BayesianRidge(alpha_1=1e-06, alpha_2=1e-06),
linear_model.LassoLars(alpha=0.55),
linear_model.LinearRegression(),
ensemble.RandomForestRegressor(bootstrap=True, criterion='mse', max_depth=None,
max_features='sqrt', max_leaf_nodes=None,
min_impurity_decrease=0.0, min_impurity_split=None,
min_samples_leaf=1, min_samples_split=2,
min_weight_fraction_leaf=0.0, n_estimators=75, n_jobs=1,
oob_score=False, random_state=0, verbose=0, warm_start=False),
tree.DecisionTreeRegressor(),
neural_network.MLPRegressor(activation='relu', alpha=0.0001, batch_size='auto', beta_1=0.9,
beta_2=0.999, early_stopping=False, epsilon=1e-08,
hidden_layer_sizes=(50, 50), learning_rate='constant',
learning_rate_init=0.001, max_iter=200, momentum=0.9,
nesterovs_momentum=True, power_t=0.5, random_state=0, shuffle=True,
solver='adam', tol=0.0001, validation_fraction=0.1, verbose=False,
warm_start=False)
]
print('Start :: Find the best model for this household...')
for clf in classifiers:
clf.fit(X_train, Y_train)
print(clf)
yhat = clf.predict(X_test)
scores = cross_val_score(clf, X_train, Y_train)
rmse = np.sqrt(mean_squared_error(Y_test, yhat))
mae = mean_absolute_error(Y_test, yhat)
print('RMSE =>', rmse)
print('MAE =>', mae)
print('CV Score =>', scores)
model_dict = {
'name': clf.__class__.__name__,
'rmse': rmse,
'mae': mae,
}
result[hhid].append(model_dict)
print('')
# In[4]:
final = []
for k, v in result.items():
for i in result[k]:
final.append([str(k), i['name'], i['rmse'], i['mae']])
col = ['household_id', 'alg', 'RMSE', 'MAE']
final = pd.DataFrame(data=final, columns=col)
final.to_csv('GHI.csv')
# In[5]:
final
# In[6]:
gb = final.groupby('alg')
# In[7]:
N = 9
ind = range(N)
mean = list(gb['RMSE'].describe()['mean'])
mean.append()
std = list(gb['RMSE'].describe()['std'])
std.append()
# In[8]:
print(mean, std)
# In[9]:
p1 = plt.bar(ind, mean, 0.4, yerr=std)
plt.ylabel('GHI ($W/m^2$)')
plt.title('Prediction RMSE of different models')
plt.xlabel('Model')
plt.xticks(ind, ('Ridge', 'Lasso','BR' ,'LasLar', 'LR', 'RF', 'DTR', 'MLP','GPR'))
# plt.yticks(np.arange(50, 2))
plt.axhline(y=gb['RMSE'].describe()['mean']['RandomForestRegressor'], linewidth=0.15)
plt.savefig('ghi.png')