欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

第四部分:模型融合

程序员文章站 2022-06-30 10:51:39
...

第三部分中的建模阶段,在不调参的情况下模型精度已经很高了,
第四部分:模型融合
但是在使用5折交叉验证进行模型性能评估的时候出现报错:

KeyError: "None of [Int64Index([    0,     1,     2,     3,     4,     6,     7,     9,    10,\n               12,\n            ...\n            34928, 34929, 34930, 34931, 34932, 34933, 34934, 34935, 34936,\n            34938],\n           dtype='int64', length=27951)] are in the [columns]"

这个问题暂时还没有解决;
因为模型精度已经很高的,所以后面的调参程序可以先搁置。

这里先考虑一下模型融合的方法:
简单的平均和加权平均是常用的两种比赛中模型融合的方式,其优点是快速、简单。

简单加权

from xgboost import XGBClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier, VotingClassifier
clf1 = LogisticRegression(random_state=1)
clf2 = RandomForestClassifier(random_state=1)
clf3 = XGBClassifier(learning_rate=0.1, n_estimators=150, max_depth=4, min_child_weight=2, subsample=0.7,objective='binary:logistic')
 
vclf = VotingClassifier(estimators=[('lr', clf1), ('rf', clf2), ('xgb', clf3)])
vclf = vclf .fit(x_train,y_train)
print(vclf .predict(x_test))

加权投票

在VotingClassifier中加入参数 voting=‘soft’, weights=[2, 1, 1],weights用于调节基模型的权重

from xgboost import XGBClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier, VotingClassifier
clf1 = LogisticRegression(random_state=1)
clf2 = RandomForestClassifier(random_state=1)
clf3 = XGBClassifier(learning_rate=0.1, n_estimators=150, max_depth=4, min_child_weight=2, subsample=0.7,objective='binary:logistic')
 
vclf = VotingClassifier(estimators=[('lr', clf1), ('rf', clf2), ('xgb', clf3)], voting='soft', weights=[2, 1, 1])
vclf = vclf .fit(x_train,y_train)
print(vclf .predict(x_test))

融合后结果目前还没有办法对比,因为还在安装包,但基本可以确定的是由于之前的精度已经很高了,再提升的幅度已经很小了,后面改如何改善呢?第四部分:模型融合

相关标签: 日常学习