-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathChapter20BostonHousePrice.py
More file actions
255 lines (218 loc) · 7.15 KB
/
Chapter20BostonHousePrice.py
File metadata and controls
255 lines (218 loc) · 7.15 KB
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
# -*- coding: utf-8 -*-
"""
Created on Wed Aug 15 10:36:53 2018
@author: Administrator
"""
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Lasso
from sklearn.linear_model import ElasticNet
from sklearn.neighbors import KNeighborsRegressor
from sklearn.svm import SVR
from sklearn.pipeline import Pipeline
from sklearn.model_selection import KFold
from sklearn.tree import DecisionTreeRegressor
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import AdaBoostRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.ensemble import ExtraTreesRegressor
from sklearn.metrics import mean_squared_error
#导入数据
datasets=load_boston()
X=datasets.data
y=datasets.target
#划分训练、测试集
X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=0,test_size=0.2)
print(X.shape)
print(y.shape)
#(506, 13)
#(506,)
#使用评估算法
#10折交叉验证
num_folds=10
seed=7
#均方差误差
scoring='neg_mean_squared_error'
'''
使用3个线性算法和3个非线性算法
LR LASSO EN
CART SVM KNN
'''
#评估算法
models={}
models['LR']=LinearRegression()
models['LASSO']=Lasso()
models['EN']=ElasticNet()
models['KNN']=KNeighborsRegressor()
models['SVR']=SVR()
models['CART']=DecisionTreeRegressor()
#获取算法准确度
results=[]
for key in models:
#K折交叉验证
kfold=KFold(n_splits=num_folds,random_state=seed)
#验证结果
cv_result=cross_val_score(models[key],X_train,y_train,cv=kfold,scoring=scoring)
results.append(cv_result)
print('%s:%f(%f)'%(key,cv_result.mean(),cv_result.std()))
'''
查看10这交叉分离验证的结果
箱线图
'''
fig=plt.figure()
fig.suptitle('Algorithm Comparison')
ax=fig.add_subplot(111)
plt.boxplot(results)
ax.set_xticklabels(models.keys())
plt.show()
'''
正态化数据
'''
#使用pipeline
pipelines={}
pipelines['ScalerLR']=Pipeline([('Scaler',StandardScaler()),('LR',LinearRegression())])
pipelines['ScalerLasso']=Pipeline([('Scaler',StandardScaler()),('LASSO',Lasso())])
pipelines['ScalerEN']=Pipeline([('Scaler',StandardScaler()),('EN',ElasticNet())])
pipelines['ScalerSVR']=Pipeline([('Scaler',StandardScaler()),('SVR',SVR())])
pipelines['ScalerKNN']=Pipeline([('Scaler',StandardScaler()),('KNN',KNeighborsRegressor())])
pipelines['ScalerCART']=Pipeline([('Scaler',StandardScaler()),('CART',DecisionTreeRegressor())])
results=[]
for key in pipelines:
kfold=KFold(random_state=seed,n_splits=num_folds)
cv_result=cross_val_score(pipelines[key],X_train,y_train,cv=kfold,scoring=scoring)
results.append(cv_result)
print('机器学习单一算法%s:%f(%f)'%(key,cv_result.mean(),cv_result.std()))
'''
查看10这交叉分离验证的结果
箱线图
'''
fig=plt.figure()
fig.suptitle('Algorithm Comparison')
ax=fig.add_subplot(111)
plt.boxplot(results)
ax.set_xticklabels(models.keys())
plt.show()
'''
从上观察
KNN是最好的算法
因此主要对KNN进行参数调整
'''
#正态化数据
scaler=StandardScaler().fit(X_train)
rescaledX=scaler.transform(X_train)
#调整参数
param_grid={'n_neighbors':[1,3,5,7,9,11,13,15,17,19,21]}
#构建模型
model=KNeighborsRegressor()
#10折交叉验证
kfold=KFold(n_splits=num_folds,random_state=seed)
#网格搜索
grid=GridSearchCV(estimator=model,param_grid=param_grid,scoring=scoring,cv=kfold)
#获取结果
grid_result=grid.fit(X=rescaledX,y=y_train)
print('最优参数:%s,获取分数:%s'%(grid_result.best_params_,grid_result.best_score_))
#zip合并矩阵
cv_results=zip(grid_result.cv_results_['mean_test_score'],
grid_result.cv_results_['std_test_score'],
grid_result.cv_results_['params'])
for mean,std,param in cv_results:
print('%f (%f) with %r'%(mean,std,param))
'''
接下来使用集成算法
装袋算法:
RF ET
提升算法:
AB GBM
'''
ensembles={}
ensembles['ScaledAB']=Pipeline([('Scaler',StandardScaler()),('AB',AdaBoostRegressor())])
ensembles['ScaledAB-KNN']=Pipeline([('Scaler',StandardScaler()),('ABKNN',AdaBoostRegressor(base_estimator=KNeighborsRegressor(n_neighbors=3)))])
ensembles['ScaledAB-LR']=Pipeline([('Scaler',StandardScaler()),('ABLR',AdaBoostRegressor(LinearRegression()))])
ensembles['ScaledRFR']=Pipeline([('Scaler',StandardScaler()),('RFR',RandomForestRegressor())])
ensembles['ScaledETR']=Pipeline([('Scaler',StandardScaler()),('ETR',ExtraTreesRegressor())])
ensembles['ScaledGBR']=Pipeline([('Scaler',StandardScaler()),('GBR',GradientBoostingRegressor())])
results=[]
for key in ensembles:
kfold=KFold(random_state=seed,n_splits=num_folds)
cv_result=cross_val_score(ensembles[key],X_train,y_train,cv=kfold,scoring=scoring)
results.append(cv_result)
print('集成算法%s:%f(%f)'%(key,cv_result.mean(),cv_result.std()))
'''
查看10这交叉分离验证的结果
箱线图
'''
fig=plt.figure()
fig.suptitle('Algorithm Comparison')
ax=fig.add_subplot(111)
plt.boxplot(results)
ax.set_xticklabels(ensembles.keys())
plt.show()
'''
对GBM和ET算法进行调参
'''
'''
GBM
'''
#正态化数据
scaler=StandardScaler().fit(X_train)
rescaledX=scaler.transform(X_train)
#调整参数
param_grid={'n_estimators':[10,50,100,200,300,400,500,600,700,800,900]}
#构建模型
model=GradientBoostingRegressor()
#10折交叉验证
kfold=KFold(n_splits=num_folds,random_state=seed)
#网格搜索
grid=GridSearchCV(estimator=model,param_grid=param_grid,scoring=scoring,cv=kfold)
#获取结果
grid_result=grid.fit(X=rescaledX,y=y_train)
print('最优参数:%s,获取分数:%s'%(grid_result.best_params_,grid_result.best_score_))
#zip合并矩阵
cv_results=zip(grid_result.cv_results_['mean_test_score'],
grid_result.cv_results_['std_test_score'],
grid_result.cv_results_['params'])
for mean,std,param in cv_results:
print('GBM%f (%f) with %r'%(mean,std,param))
'''
ET
'''
#正态化数据
scaler=StandardScaler().fit(X_train)
rescaledX=scaler.transform(X_train)
#调整参数
param_grid={'n_estimators':[10,50,100,200,300,400,500,600,700,800,900]}
#构建模型
model=ExtraTreesRegressor()
#10折交叉验证
kfold=KFold(n_splits=num_folds,random_state=seed)
#网格搜索
grid=GridSearchCV(estimator=model,param_grid=param_grid,scoring=scoring,cv=kfold)
#获取结果
grid_result=grid.fit(X=rescaledX,y=y_train)
print('最优参数:%s,获取分数:%s'%(grid_result.best_params_,grid_result.best_score_))
#zip合并矩阵
cv_results=zip(grid_result.cv_results_['mean_test_score'],
grid_result.cv_results_['std_test_score'],
grid_result.cv_results_['params'])
for mean,std,param in cv_results:
print('ET%f (%f) with %r'%(mean,std,param))
'''
确定最终模型
ET
'''
#正态化数据
scaler=StandardScaler().fit(X_train)
rescaledX=scaler.transform(X_train)
gbr=ExtraTreesRegressor(n_estimators=80)
gbr.fit(X=rescaledX,y=y_train)
#评估算法模型
rescaledX_validation=scaler.transform(X_test)
preditions=gbr.predict(X_test)
print('准确度:',mean_squared_error(preditions,y_test))