forked from lihaifeng215/Thunderstorm-Gale-Identification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class6_1_regrassion_model.py
179 lines (148 loc) · 6.13 KB
/
class6_1_regrassion_model.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
import numpy as np
import time
from sklearn import metrics
from sklearn.preprocessing import MinMaxScaler
from sklearn.externals import joblib
class classification_model:
def __init__(self):
pass
'''
Bayesian Ridge Regression
'''
def bayesian_ridge_regressor(self, train_x, train_y):
from sklearn.linear_model import BayesianRidge
model = BayesianRidge()
model.fit(train_x, train_y)
return model
'''
Decision Tree Regressor
'''
def decision_tree_regressor(self, train_x, train_y):
from sklearn.tree import DecisionTreeRegressor
model = DecisionTreeRegressor()
model.fit(train_x, train_y)
return model
'''
Linear Regression
'''
def linear_regression(self, train_x, train_y):
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(train_x, train_y)
return model
'''
Ridge Regression
'''
def ridge_regression(self, train_x, train_y):
from sklearn.linear_model import Ridge
model = Ridge(alpha=0.1)
model.fit(train_x, train_y)
return model
'''
Lasso Regression
'''
def lasso_regression(self, train_x, train_y):
from sklearn.linear_model import Lasso
model = Lasso(alpha=0.1)
model.fit(train_x, train_y)
return model
'''
Support Vector Regressor
'''
def support_vector_regressor(self, train_x, train_y):
from sklearn.svm import SVR
model = SVR()
model.fit(train_x, train_y)
return model
'''
K Nearest Neighbor Regressor
'''
def k_nearest_neighbor_regressor(self, train_x, train_y):
from sklearn.neighbors import KNeighborsRegressor
model = KNeighborsRegressor(n_neighbors=5)
model.fit(train_x, train_y)
return model
'''
Random Forest Regressor
'''
def random_forest_regressor(self, train_x, train_y):
from sklearn.ensemble import RandomForestRegressor
model = RandomForestRegressor(n_estimators=10)
model.fit(train_x, train_y)
return model
'''
AdaBoost Regressor
'''
def adaboost_regressor(self, train_x, train_y):
from sklearn.ensemble import AdaBoostRegressor
model = AdaBoostRegressor(n_estimators=50)
model.fit(train_x, train_y)
return model
'''
Gradient Boosting Regressor
'''
def gradient_boosting_regressor(self, train_x, train_y):
from sklearn.ensemble import GradientBoostingRegressor
model = GradientBoostingRegressor(n_estimators=100)
model.fit(train_x, train_y)
return model
# Normalize dataset
def Normalize_dataset(self, dataset):
return MinMaxScaler().fit_transform(dataset)
# save model
def save_model(self,model, model_name):
joblib.dump(model, "file/regression_models/" + str(model_name) + '_model.pkl')
# save error
def save_error(self, error, error_name):
np.savetxt("file/wind_error_classification/reg_error_%s_model.csv" % error_name, error, delimiter=',', fmt='%f')
# use all classification models
def use_regressors(self, train_x, train_y, test_x, test_y):
classifiers = {'DTR': self.decision_tree_regressor,
'LR': self.linear_regression,
'Ridge': self.ridge_regression,
'Lasso': self.lasso_regression,
'BR': self.bayesian_ridge_regressor,
'SVR': self.support_vector_regressor,
'KNNR': self.k_nearest_neighbor_regressor,
'RFR': self.random_forest_regressor,
'AR': self.adaboost_regressor,
'GBR': self.gradient_boosting_regressor
}
test_classifiers = ['DTR', 'LR', 'Ridge', 'Lasso', 'BR', 'SVR', 'KNNR', 'RFR', 'AR', 'GBR']
for classifier in test_classifiers:
print('******************* %s ********************' % classifier)
start_time = time.time()
model = classifiers[classifier](train_x, train_y)
print('training took %fs!' % (time.time() - start_time))
# 保存模型
self.save_model(model, classifier)
# 使用保存的模型预测
model = joblib.load("file/regression_models/" + str(classifier) + '_model.pkl')
test_time = time.time()
predict = model.predict(test_x)
score = model.score(test_x, test_y)
mae = metrics.mean_absolute_error(test_y, predict)
rmse = np.sqrt(metrics.mean_squared_error(test_y, predict))
print('testing took %fs!' % (time.time() - test_time))
print('score: %.2f, MAE: %.2f, RMSE: %.2f' % (score, mae, rmse))
error = np.column_stack((test_y, predict))
# 保存误差
self.save_error(error, classifier)
print('use all time: %fs!' % (time.time() - start_time))
if __name__ == '__main__':
train_dataset = np.loadtxt("file/train_test/train_dataset_17y_Radar_denoised_13x13_append_features_reg_labeled_sub_7x7.csv", delimiter=',')[:,5:]
print("train_dataset.shape:", train_dataset.shape)
test_dataset = np.loadtxt("file/train_test/test_dataset_17y_Radar_denoised_13x13_append_features_reg_labeled_sub_7x7.csv", delimiter=',')[:, 5:]
print("test_dataset.shape:", test_dataset.shape)
train_x = train_dataset[:, 49*18:-1]
print("train_x.shape:",train_x.shape)
train_y = train_dataset[:, -1]
train_y = np.array(train_y,dtype=int)
test_x = test_dataset[:, 49*18:-1]
test_y = test_dataset[:, -1]
test_y = np.array(test_y,dtype=int)
print("测试集中的正例比例:",len(test_y[test_y >= 15]) / len(test_y))
cls_model = classification_model()
train_x = cls_model.Normalize_dataset(train_x)
test_x = cls_model.Normalize_dataset(test_x)
cls_model.use_regressors(train_x, train_y, test_x, test_y)