利用各类回归模型,对数据集进行建模
模型的名字
names = ['LinerRegression',
'Ridge',
'Lasso',
'Random Forrest',
'GBDT',
'Support Vector Regression',
'ElasticNet',
'XgBoost']
定义模型
cv在这里是交叉验证的思想
models = [LinearRegression(),
RidgeCV(alphas=(0.001,0.1,1),cv=3),
LassoCV(alphas=(0.001,0.1,1),cv=5),
RandomForestRegressor(n_estimators=10),
GradientBoostingRegressor(n_estimators=30),
SVR(),
ElasticNet(alpha=0.001,max_iter=10000),
XGBRegressor()]
输出所有回归模型的R2评分
定义R2评分的函数
def R2(model,x_train, x_test, y_train, y_test):
model_fitted = model.fit(x_train,y_train)
y_pred = model_fitted.predict(x_test)
score = r2_score(y_test, y_pred)
return score
遍历所有模型进行评分
for name,model in zip(names,models):
score = R2(model,x_train, x_test, y_train, y_test)
print("{}: {:.6f}, {:.4f}".format(name,score.mean(),score.std()))
输出:
LinerRegression: 0.564144, 0.0000
Ridge: 0.563700, 0.0000
Lasso: 0.564078, 0.0000
Random Forrest: 0.646657, 0.0000
GBDT: 0.725883, 0.0000
Support Vector Regression: 0.517310, 0.0000
ElasticNet: 0.564021, 0.0000
XgBoost: 0.765266, 0.0000