构建随机森林、GBDT、XGBoost和LightGBM这4个模型,并对每一个模型进行评分
# install the package in jupyter notebook
!pip install xgboost
!pip install lightgbm
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import GradientBoostingRegressor
import xgboost as xgb
import lightgbm as lgb
df = pd.read_csv('data_all.csv')
df.head()
y = df['status']
x = df.drop('status', axis=1)
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=2018)
print(x_train.shape,y_train.shape)
print(x_test.shape,y_test.shape)
gbdt=GradientBoostingRegressor()
gbdt.fit(x_train,y_train)
rfc=RandomForestClassifier()
rfc.fit(x_train,y_train)
gbm=lgb.LGBMRegressor()
gbm.fit(x_train, y_train)
xgb=xgb.XGBClassifier()
xgb.fit(x_train, y_train)
score_gbdt = gbdt.score(x_test,y_test)
score_rfc = rfc.score(x_test,y_test)
score_gbm = gbm.score(x_test,y_test)
score_xgb = xgb.score(x_test,y_test)
print("GradientBoostingRegressor: ", score_gbdt)
print("RandomForestClassifier: ", score_rfc)
print("LGBMRegressor: ", score_gbm)
print("XGBClassifier: ", score_xgb)
#GradientBoostingRegressor: 0.18155175882376617
#RandomForestClassifier: 0.7680448493342676
#LGBMRegressor: 0.14678879008364842
#XGBClassifier: 0.7855641205325858