SKlearn学习（持续更新）

2018-05-22 15:45

### 回归

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

# 读数据，并进行处理
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])

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]]

{{panelTitle}}

{{item.creationTime | formatDateTime}}
{{ritem.creationTime | formatDateTime}}

{{ritem.contents?ritem.contents:'没有填写内容'}}

{{item.creationTime | formatDateTime}}
{{ritem.creationTime | formatDateTime}}

{{ritem.contents?ritem.contents:'没有填写内容'}}