1. 回归
训练了两个回归器,GBDT和Xgboost,用这两个回归器做stacking
使用之前已经调好参的训练器
gbdt_nxf = GradientBoostingRegressor(learning_rate=0.06,n_estimators=250,
min_samples_split=700,min_samples_leaf=70,max_depth=6,
max_features='sqrt',subsample=0.8,random_state=75)
xgb_nxf = XGBRegressor(learning_rate=0.06,max_depth=6,n_estimators=200,random_state=75)
事先建好stacking要用到的矩阵
from sklearn.model_selection import KFold,StratifiedKFold
kf = StratifiedKFold(n_splits=5,random_state=75,shuffle=True)
from sklearn.metrics import r2_score
train_proba = np.zeros((len(gbdt_train_data),2))
train_proba = pd.DataFrame(train_proba)
train_proba.columns = ['gbdt_nxf','xgb_nxf']
test_proba = np.zeros((len(gbdt_test_data),2))
test_proba = pd.DataFrame(test_proba)
test_proba.columns = ['gbdt_nxf','xgb_nxf']
reg_names = ['gbdt_nxf','xgb_nxf']
for i,reg in enumerate([gbdt_nxf,xgb_nxf]):
pred_list = []
col = reg_names[i]
for train_index,val_index in kf.split(gbdt_train_data,gbdt_train_label):
x_train = gbdt_train_data.loc[train_index,:].values
y_train = gbdt_train_label[train_index]
x_val = gbdt_train_data.loc[val_index,:].values
y_val = gbdt_train_label[val_index]
reg.fit(x_t