/
ANN_Wind.py
178 lines (141 loc) · 10.7 KB
/
ANN_Wind.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
from sqlalchemy import create_engine
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn import datasets, linear_model
from sklearn.svm import SVR
from sklearn.cross_validation import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn import metrics
from sklearn.model_selection import cross_val_predict
from sklearn import preprocessing
from sklearn.neural_network import MLPRegressor
#-----------------------------------------------------------------------------------------------------------------------
acc='abc'
pwd=1234
dbName='data_analysis' #資料庫
tbName_1='codis'
tbName_2='wind_turbines' #資料表
ip='140.120.54.129'
port=3306
folderPath='C:\CK_WIND\CK_4' #文件夾路徑
#-----------------------------------------------------------------------------------------------------------------------
db = create_engine('mysql://'+acc+':'+str(pwd)+'@'+ip+':'+str(port)+'/'+dbName) #connect mysql
#qTrain_Weather='SELECT DewPointTemperature,RelativeHumidity,WindSpeed,WindDirection,WSGust FROM codis where year(DataTime) <= 2016 ' #SFS
#qTrain_Weather='SELECT Temperature,DewPointTemperature,RelativeHumidity,WindSpeed,WSGust FROM codis where year(DataTime) <= 2016 ' #RandomForest
#qTrain_Weather='SELECT StnPres,SeaPres,Temperature,DewPointTemperature,RelativeHumidity,WindSpeed,WindDirection,WSGust,WDGust,Precipitation,PrecpHour FROM codis where year(DataTime) <= 2016 '
qTrain_Weather='SELECT StnPres,SeaPres,Temperature,DewPointTemperature,RelativeHumidity,WindSpeed,WindDirection,WSGust,WDGust FROM codis where year(DataTime) <= 2016 '
#qTrain_Weather='SELECT StnPres,SeaPres,Temperature,DewPointTemperature,RelativeHumidity,WindSpeed,WindDirection,WSGust,WDGust FROM codis where year(DataTime) <= 2016 and month(DataTime) not between 5 and 9 and hour(DataTime) between 11 and 15'
#qTrain_Weather='SELECT StnPres,SeaPres,Temperature,DewPointTemperature,RelativeHumidity,WindSpeed,WindDirection,WSGust,WDGust FROM codis where year(DataTime) <= 2016 and month(DataTime) not between 5 and 9'
#qTrain_Weather='SELECT StnPres,SeaPres,Temperature,DewPointTemperature,RelativeHumidity,WindSpeed,WindDirection,WSGust,WDGust,Precipitation,PrecpHour FROM codis where year(DataTime) <= 2016 and hour(DataTime) between 11 and 15'
#qTrain_Weather='SELECT StnPres,SeaPres,Temperature,DewPointTemperature,RelativeHumidity,WindSpeed,WindDirection,WSGust,WDGust,Precipitation,PrecpHour FROM codis where year(DataTime) <= 2016 and hour(DataTime) not between 5 and 22'
qTrain_ROTO_ROTORRPM='SELECT ROTO_ROTORRPM FROM wind_turbines where year(DATE_TIME) <= 2016 and CK_NUMBER= 5'
#qTrain_ROTO_ROTORRPM='SELECT ROTO_ROTORRPM FROM wind_turbines where year(DATE_TIME) <= 2016 and CK_NUMBER= 3 and month(DATE_TIME) not between 5 and 9 and hour(DATE_TIME) between 11 and 15'
#qTrain_ROTO_ROTORRPM='SELECT ROTO_ROTORRPM FROM wind_turbines where year(DATE_TIME) <= 2016 and CK_NUMBER= 3 and month(DATE_TIME) not between 5 and 9'
#qTrain_ROTO_ROTORRPM='SELECT ROTO_ROTORRPM FROM wind_turbines where year(DATE_TIME) <= 2016 and CK_NUMBER= 3 and month(DATE_TIME) and hour(DATE_TIME) between 11 and 15'
#qTrain_ROTO_ROTORRPM='SELECT ROTO_ROTORRPM FROM wind_turbines where year(DATE_TIME) <= 2016 and CK_NUMBER= 3 and month(DATE_TIME) and hour(DATE_TIME) not between 5 and 22'
#qTest_Weather='SELECT DewPointTemperature,RelativeHumidity,WindSpeed,WindDirection,WSGust FROM codis where year(DataTime) = 2017' #SFS
#qTest_Weather='SELECT Temperature,DewPointTemperature,RelativeHumidity,WindSpeed,WSGust FROM codis where year(DataTime) = 2017' #RandomForest
#qTest_Weather='SELECT StnPres,SeaPres,Temperature,DewPointTemperature,RelativeHumidity,WindSpeed,WindDirection,WSGust,WDGust,Precipitation,PrecpHour FROM codis where year(DataTime) = 2017'
qTest_Weather='SELECT StnPres,SeaPres,Temperature,DewPointTemperature,RelativeHumidity,WindSpeed,WindDirection,WSGust,WDGust FROM codis where year(DataTime) = 2017'
#qTest_Weather='SELECT StnPres,SeaPres,Temperature,DewPointTemperature,RelativeHumidity,WindSpeed,WindDirection,WSGust,WDGust FROM codis where year(DataTime) = 2017 and month(DataTime) not between 5 and 9 and hour(DataTime) between 11 and 15'
#qTest_Weather='SELECT StnPres,SeaPres,Temperature,DewPointTemperature,RelativeHumidity,WindSpeed,WindDirection,WSGust,WDGust FROM codis where year(DataTime) = 2017 and month(DataTime) not between 5 and 9'
#qTest_Weather='SELECT StnPres,SeaPres,Temperature,DewPointTemperature,RelativeHumidity,WindSpeed,WindDirection,WSGust,WDGust,Precipitation,PrecpHour FROM codis where year(DataTime) = 2017 and hour(DataTime) between 11 and 15'
#qTest_Weather='SELECT StnPres,SeaPres,Temperature,DewPointTemperature,RelativeHumidity,WindSpeed,WindDirection,WSGust,WDGust,Precipitation,PrecpHour FROM codis where year(DataTime) = 2017 and hour(DataTime) not between 5 and 22'
qTest_ROTO_ROTORRPM='SELECT ROTO_ROTORRPM FROM wind_turbines where year(DATE_TIME) = 2017 and CK_NUMBER= 5'
#qTest_ROTO_ROTORRPM='SELECT ROTO_ROTORRPM FROM wind_turbines where year(DATE_TIME) = 2017 and CK_NUMBER= 3 and month(DATE_TIME) not between 5 and 9 and hour(DATE_TIME) between 11 and 15'
#qTest_ROTO_ROTORRPM='SELECT ROTO_ROTORRPM FROM wind_turbines where year(DATE_TIME) = 2017 and CK_NUMBER= 3 and month(DATE_TIME) not between 5 and 9'
#qTest_ROTO_ROTORRPM='SELECT ROTO_ROTORRPM FROM wind_turbines where year(DATE_TIME) = 2017 and CK_NUMBER= 3 and hour(DATE_TIME) between 11 and 15'
#qTest_ROTO_ROTORRPM='SELECT ROTO_ROTORRPM FROM wind_turbines where year(DATE_TIME) = 2017 and CK_NUMBER= 3 and hour(DATE_TIME) not between 5 and 22'
qTrain_Power='SELECT Grid_Power FROM wind_turbines where year(DATE_TIME) <= 2016 and CK_NUMBER= 5'
#qTrain_Power='SELECT Grid_Power FROM wind_turbines where year(DATE_TIME) <= 2016 and CK_NUMBER= 3 and month(DATE_TIME) not between 5 and 9 and hour(DATE_TIME) between 11 and 15'
#qTrain_Power='SELECT Grid_Power FROM wind_turbines where year(DATE_TIME) <= 2016 and CK_NUMBER= 3 and month(DATE_TIME) not between 5 and 9;'
#qTrain_Power='SELECT Grid_Power FROM wind_turbines where year(DATE_TIME) <= 2016 and CK_NUMBER= 3 and hour(DATE_TIME) between 11 and 15;'
#qTrain_Power='SELECT Grid_Power FROM wind_turbines where year(DATE_TIME) <= 2016 and CK_NUMBER= 3 and hour(DATE_TIME) not between 5 and 22'
qTest_Power='SELECT Grid_Power FROM wind_turbines where year(DATE_TIME) = 2017 and CK_NUMBER= 5'
#qTest_Power='SELECT Grid_Power FROM wind_turbines where year(DATE_TIME) = 2017 and CK_NUMBER= 3 and month(DATE_TIME) not between 5 and 9 and hour(DATE_TIME) between 11 and 15'
#qTest_Power='SELECT Grid_Power FROM wind_turbines where year(DATE_TIME) = 2017 and CK_NUMBER= 3 and month(DATE_TIME) not between 5 and 9;'
#qTest_Power='SELECT Grid_Power FROM wind_turbines where year(DATE_TIME) = 2017 and CK_NUMBER= 3 and hour(DATE_TIME) between 11 and 15;'
#qTest_Power='SELECT Grid_Power FROM wind_turbines where year(DATE_TIME) = 2017 and CK_NUMBER= 3 and hour(DATE_TIME) not between 5 and 22;'
df_Train_Weather=pd.read_sql(qTrain_Weather,con=db)
df_Train_ROTO_ROTORRPM=pd.read_sql(qTrain_ROTO_ROTORRPM,con=db)
df_Train_Combine=df_Train_Weather.join(df_Train_ROTO_ROTORRPM)
df_Test_Weather=pd.read_sql(qTest_Weather,con=db)
df_Test_ROTO_ROTORRPM=pd.read_sql(qTest_ROTO_ROTORRPM,con=db)
df_Test_Combine=df_Test_Weather.join(df_Test_ROTO_ROTORRPM)
#DataTime=pd.read_sql(queryTime,con=db_1)
df_Train_Power=pd.read_sql(qTrain_Power,con=db)
df_Test_Power=pd.read_sql(qTest_Power,con=db)
#X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42,test_size=0.3) # parse train 70% , test 30% ,random_state 固定隨機數種子
X_train=df_Train_Combine
X_test=df_Test_Combine
y_train=df_Train_Power
y_test=df_Test_Power
imp_1=preprocessing.Imputer(missing_values='NaN',strategy='mean',axis=0) #預處理,空值改均值 axis=0
imp_1.fit(X_train)
X_train=imp_1.transform(X_train)
X_test=imp_1.transform(X_test)
imp_2=preprocessing.Imputer(missing_values='NaN',strategy='mean',axis=0) #預處理,空值改均值 axis=0
imp_2.fit(y_train)
y_train=imp_2.transform(y_train)
y_test=imp_2.transform(y_test)
y_train=y_train.reshape(-1)
y_test=y_test.reshape(-1)
#特徵縮放
#-----------------------------------------------------------------------------------------------------------------------
sc=preprocessing.StandardScaler()
sc.fit(X_train)
X_train=sc.transform(X_train)
X_test=sc.transform(X_test)
#-----------------------------------------------------------------------------------------------------------------------
mlp_reg=MLPRegressor(hidden_layer_sizes=[12,12,12],
max_iter=100,
activation='relu', #‘identity’, ‘logistic’, ‘tanh’, ‘relu’
learning_rate_init=0.001,
solver='lbfgs', #lbfgs, sgd, adam
random_state=6
)
model_nn=mlp_reg.fit(X_train, y_train)
y_pred=model_nn.predict(X_test)
MSE=metrics.mean_squared_error(y_test, y_pred)
RMSE=np.sqrt(metrics.mean_squared_error(y_test, y_pred))
MAE=metrics.mean_absolute_error(y_test, y_pred)
ACC=mlp_reg.score(X_test,y_test)
# MAPE
#-----------------------------------------------------------------------------------------------------------------------
'''
def MAPE(y_true, y_pred): #0值替換成陣列前一個數
for Check_Zero in range(y_true.shape[0]):
if y_true[Check_Zero]==0:
y_true[Check_Zero]=y_true[Check_Zero +1]
y_true, y_pred = np.array(y_true), np.array(y_pred)
return np.mean(np.abs((y_true - y_pred) / y_true))
'''
'''
def MAPE(y_true, y_pred): #0值替換為y_true 的平均
mean=np.mean(y_true)
for Check_Zero in range(y_true.shape[0]):
if y_true[Check_Zero]==0:
y_true[Check_Zero]=mean
y_true, y_pred = np.array(y_true), np.array(y_pred)
return np.mean(np.abs((y_true - y_pred) / y_true))
'''
def MAPE(y_true, y_pred): #0值刪除
actual=[]
predict=[]
for Check_Zero in range(y_true.shape[0]):
if y_true[Check_Zero]!=0:
actual.append(y_true[Check_Zero])
predict.append(y_pred[Check_Zero])
actual, predict = np.array(actual), np.array(predict)
return np.mean(np.abs((actual - predict) / actual))
#-----------------------------------------------------------------------------------------------------------------------
#MAPE_rbf=mean_absolute_percentage_error(y_test,y_pred_svr_rbf)
print('*'*100)
print('天氣數據+風機轉子轉速')
print('MSE :',MSE)
print('RMSE :',RMSE)
print('MAPE :',MAPE(y_test,y_pred))
print('Accuracy :' , ACC)
print('*'*100)