1. 程式人生 > >SKlearn迴歸模型調包練習

SKlearn迴歸模型調包練習

摘自 一隻鹹狗https://blog.csdn.net/u013982164/article/details/80364500

看了錄播後照著程式碼敲了一遍 sklearn常用分類迴歸演算法簡介 對能瞭解SKlearn常規套路,但模型具體的引數需要進一步瞭解。

# 引入必要的第三方包
from sklearn.cross_validation import train_test_split
from sklearn import metrics
import pandas as pd 
import time

# 讀資料,並進行處理
data = pd.read_csv(’/home/whn/Downloads/all_window.csv’).fillna(0,axis=1)
X = data.drop(‘label’,axis=1)
# min_max_scale = StandardScaler()
# X = min_max_scale.fit_transform(X)
y = data[‘label’]
history = []
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=

0)

# 線性迴歸:LR、Rigde(L2) 和 Lasso(L1)
from sklearn import linear_model

start = time.time()
reg = linear_model.LinearRegression()
reg.fit(X_train, y_train)
end = time.time()
y_pred = reg.predict(X_test)
loss = metrics.mean_squared_error(y_test, y_pred)
name = ‘LinearRegression’
history.append([name,loss,

end-start])

start = time.time()
reg = linear_model.Ridge()
reg.fit(X_train, y_train)
end = time.time()
y_pred = reg.predict(X_test)
loss = metrics.mean_squared_error(y_test, y_pred)
name = ‘Rigde’
history.append([name,loss,end-start])

start = time.time()
reg = linear_model.Ridge(alpha=0.5)
reg.fit(X_train, y_train)
end = time.time()
y_pred = reg.predict(X_test)
loss = metrics.mean_squared_error(y_test, y_pred)
name = ‘Ridge_alpha=0.5’
history.append([name,loss,end-start])

start = time.time()
reg = linear_model.Lasso()
reg.fit(X_train, y_train)
end = time.time()
y_pred = reg.predict(X_test)
loss = metrics.mean_squared_error(y_test, y_pred)
name = ‘Lasso’
history.append([name,loss,end-start])

start = time.time()
reg = linear_model.Lasso(alpha=2)
reg.fit(X_train, y_train)
end = time.time()
y_pred = reg.predict(X_test)
loss = metrics.mean_squared_error(y_test, y_pred)
name = ‘Lasso_alpha=2’
history.append([name,loss,end-start])

start = time.time()
reg = linear_model.Lasso(alpha=2,max_iter=10)
reg.fit(X_train, y_train)
end = time.time()
y_pred = reg.predict(X_test)
loss = metrics.mean_squared_error(y_test, y_pred)
name = ‘Lasso_alpha=2_max_iter=10’
history.append([name,loss,end-start])

# 整合模型:RF
from sklearn.ensemble import RandomForestRegressor

start = time.time()
reg = RandomForestRegressor()
reg.fit(X_train, y_train)
end = time.time()
y_pred = reg.predict(X_test)
loss = metrics.mean_squared_error(y_test, y_pred)
name = ‘RandomForestRegressor’
history.append([name,loss,end-start])

start = time.time()
reg = RandomForestRegressor(n_estimators=200,random_state=0)
reg.fit(X_train, y_train)
end = time.time()
y_pred = reg.predict(X_test)
loss = metrics.mean_squared_error(y_test, y_pred)
name = ‘RandomForestRegressor_n_estimators=200’
history.append([name,loss,end-start])

from sklearn.ensemble.forest import ExtraTreeRegressor

start = time.time()
reg = ExtraTreeRegressor()
reg.fit(X_train, y_train)
end = time.time()
y_pred = reg.predict(X_test)
loss = metrics.mean_squared_error(y_test, y_pred)
name = ‘ExtraTreeRegressor’
history.append([name,loss,end-start])

start = time.time()
reg = ExtraTreeRegressor(n_estimators=100,max_depth=7,min_samples_leaf=10)
reg.fit(X_train, y_train)
end = time.time()
y_pred = reg.predict(X_test)
loss = metrics.mean_squared_error(y_test, y_pred)
name = ‘ExtraTreeRegressor_s’
history.append([name,loss,end-start])

# 神經網路:MLP
from sklearn.neural_network import MLPRegressor

start = time.time()
reg = MLPRegressor()
reg.fit(X_train, y_train)
end = time.time()
y_pred = reg.predict(X_test)
loss = metrics.mean_squared_error(y_test, y_pred)
name = ‘MLPRegressor’
history.append([name,loss,end-start])

start = time.time()
reg = MLPRegressor(batch_size=50, hidden_layer_sizes=20, learning_rate_init=0.1,
max_iter=300,random_state=0,early_stopping=True)
reg.fit(X_train, y_train)
end = time.time()
y_pred = reg.predict(X_test)
loss = metrics.mean_squared_error(y_test, y_pred)
name = ‘MLPRegressor_s’
history.append([name,loss,end-start])

# SVM
from sklearn.svm import SVR, LinearSVR

start = time.time()
reg = SVR()
reg.fit(X_train, y_train)
end = time.time()
y_pred = reg.predict(X_test)
loss = metrics.mean_squared_error(y_test, y_pred)
name = ‘SVR’
history.append([name,loss,end-start])

start = time.time()
reg = SVR(kernel=‘rbf’,C=0.1, epsilon=0.1,max_iter=100)
reg.fit(X_train, y_train)
end = time.time()
y_pred = reg.predict(X_test)
loss = metrics.mean_squared_error(y_test, y_pred)
name = ‘SVR_s’
history.append([name,loss,end-start])

# XGBOOST
from xgboost.sklearn import XGBRegressor

start = time.time()
reg = XGBRegressor()
reg.fit(X_train, y_train)
end = time.time()
y_pred = reg.predict(X_test)
loss = metrics.mean_squared_error(y_test, y_pred)
name = ‘XGBRegressor’
history.append([name,loss,end-start])

start = time.time()
reg = XGBRegressor(max_depth=4, n_estimators=500, min_child_weight=10,
subsample=0.7, colsample_bytree=0.7, reg_alpha=0, reg_lambda=0.5)
reg.fit(X_train, y_train)
end = time.time()
y_pred = reg.predict(X_test)
loss = metrics.mean_squared_error(y_test, y_pred)
name = ‘XGBRegressor_s’
history.append([name,loss,end-start])

# lightGBM
from lightgbm import LGBMRegressor

start = time.time()
reg = LGBMRegressor()
reg.fit(X_train, y_train)
end = time.time()
y_pred = reg.predict(X_test)
loss = metrics.mean_squared_error(y_test, y_pred)
name = ‘LGBMRegressor’
history.append([name,loss,end-start])

start = time.time()
reg = LGBMRegressor(num_leaves=40,max_depth=7,n_estimators=200,min_child_weight=10,
subsample=0.7, colsample_bytree=0.7,reg_alpha=0, reg_lambda=0.5)
reg.fit(X_train, y_train)
end = time.time()
y_pred = reg.predict(X_test)
loss = metrics.mean_squared_error(y_test, y_pred)
name = ‘LGBMRegressor_s’
history.append([name,loss,end-start])

from sklearn.neighbors.regression import KNeighborsRegressor

start = time.time()
reg = KNeighborsRegressor(n_neighbors=4,algorithm=‘kd_tree’)
reg.fit(X_train, y_train)
end = time.time()
y_pred = reg.predict(X_test)
loss = metrics.mean_squared_error(y_test, y_pred)
name = ‘KNeighborsRegressor_s’
history.append([name,loss,end-start])

# 融合
start = time.time()
reg = LGBMRegressor()
reg.fit(X_train, y_train)
y_pred_lgb = reg.predict(X_test)
reg = linear_model.Lasso(alpha=2,max_iter=10)
reg.fit(X_train, y_train)
y_pred_lr = reg.predict(X_test)
y_pred = (y_pred_lgb + y_pred_lr) / 2
end = time.time()
loss = metrics.mean_squared_error(y_test, y_pred)
name = ‘LGBMRegressor+Lasso’
history.append([name,loss,end-start])

  • 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

結果如下:

In [216]: history
Out[216]:[['LinearRegression', 5616.27782832045, 0.23373723030090332],
		 ['Rigde', 5625.959154564378, 0.18334627151489258],
		 ['Ridge_alpha=0.5', 5622.655065407929, 0.18333888053894043],
		 ['Lasso', 4957.000025059516, 0.516655683517456],
		 ['Lasso_alpha=2', 4741.355578668198, 0.5424296855926514],
		 ['Lasso_alpha=2_max_iter=10', 4233.225156683616, 0.20163559913635254],
		 ['DecisionTreeRegressor', 14380.923876533841, 0.3124210834503174],
		 ['RandomForestRegressor', 8305.730287150229, 2.3668367862701416],
		 ['RandomForestRegressor_n_estimators=200', 5410.864295873489, 43.549724102020264],
		 ['MLPRegressor', 3629898.708004297, 0.3255181312561035],
		 ['MLPRegressor_s', 30620.511077612777, 0.2955296039581299],
		 ['SVR', 57030.14962918571, 2.7514443397521973],
		 ['SVR_s', 77562.5821631834, 0.3735840320587158],
		 ['XGBRegressor', 7435.130163942215, 1.4277334213256836],
		 ['XGBRegressor_s', 6465.628256746952, 6.708096981048584],
		 ['LGBMRegressor', 6386.790622782424, 2.187742233276367],
		 ['LGBMRegressor_s', 6874.105186792729, 1.4423110485076904],
		 ['ExtraTreeRegressor', 7343.10613304338, 0.2813429832458496],
		 ['ExtraTreeRegressor_s', 24685.212690420427, 0.28301167488098145],
		 ['KNeighborsRegressor_s', 43321.059426229505, 0.21740508079528809],
		 ['LGBMRegressor+Lasso', 4589.754232812634, 2.578683376312256]]

					

看了錄播後照著程式碼敲了一遍 sklearn常用分類迴歸演算法簡介 對能瞭解SKlearn常規套路,但模型具體的引數需要進一步瞭解。

# 引入必要的第三方包
from sklearn.cross_validation import train_test_split
from sklearn import metrics
import pandas as pd 
import time