VotingClassifier 模型聚合——投票
来源:互联网 发布:仓库数据采集器 软件 编辑:程序博客网 时间:2024/05/20 20:02
将多种模型的结果通过投票的方式进行聚合。选择的模型的结果要有好有坏,这样聚合的结果最佳。
PLAN1 Majority Class Labels (Majority/Hard Voting)
事先准备三个模型,xgb和rf模型都已经通过cross_validation找出了较好的参数
>>> from xgboost.sklearn import XGBClassifier>>> from sklearn.ensemble import RandomForestClassifier>>> from sklearn import svm>>> from sklearn.svm import SVC>>> clf1 = XGBClassifier(learning_rate =0.1, n_estimators=140, max_depth=1,min_child_weight=2, gamma=0, subsample=0.7, colsample_bytree=0.6,objective= 'binary:logistic', nthread=4, scale_pos_weight=1)>>> clf2 = RandomForestClassifier(n_estimators=50,max_depth=1,min_samples_split=4,min_samples_leaf=54,oob_score=True)>>> clf3 = SVC(C=0.1,probability=True)
投票聚合,结果如下
>>> from sklearn.ensemble import VotingClassifier>>> from sklearn.model_selection import cross_val_score>>> eclf = VotingClassifier(estimators=[('xgb', clf1), ('rf', clf2), ('svc', clf3)], voting='hard')>>> for clf, label in zip([clf1, clf2, clf3, eclf], ['XGBBoosting', 'Random Forest', 'SVM', 'Ensemble']): scores = cross_val_score(clf, x, y, cv=5, scoring='accuracy') print("Accuracy: %0.2f (+/- %0.2f) [%s]" % (scores.mean(), scores.std(), label))Accuracy: 0.93 (+/- 0.00) [XGBBoosting]Accuracy: 0.93 (+/- 0.00) [Random Forest]Accuracy: 0.93 (+/- 0.00) [SVM]Accuracy: 0.93 (+/- 0.00) [Ensemble]
懵比,这三个怎么一样啊…
回到hard vote 的原理,在这种少数服从多数的硬投票中,投出的结果,表明大多数模型认同的结果。比如,如果三个分类器对一个给定的样本的预测是这样的:
- 分类器 1 -> 类型 1
- 分类器 2 -> 类型 1
- 分类器 3 -> 类型 2
最终的投票结果会把这个样本归类到类型一,基于少数服从多数的原则。
在投票相同的情况下,投票结果会按照分类器的排序选择排在第一个的分类器结果。在下面这个例子中:
- 分类器 1 -> 类型 1
- 分类器 2 -> 类型 2
这个样本的投票结果是类型1。
所以很重要的一点,硬投票不能计算预测概率。
PLAN2 Weighted Average Probabilities (Soft Voting)
软投票和硬投票不同之处在于,它返回的结果是一组概率的加权平均数。可是这个权重是随便赋予的吗?貌似是的,看谁顺眼,给谁高点(未解之谜,待我再查查)。。。
>>> eclf = VotingClassifier(estimators=[('xgb', clf1), ('rf', clf2), ('svc', clf3)], voting='soft',weights=[2,1,1])>>> clf1.fit(x,y)XGBClassifier(base_score=0.5, colsample_bylevel=1, colsample_bytree=0.6, gamma=0, learning_rate=0.1, max_delta_step=0, max_depth=1, min_child_weight=2, missing=None, n_estimators=140, nthread=4, objective='binary:logistic', reg_alpha=0, reg_lambda=1, scale_pos_weight=1, seed=0, silent=True, subsample=0.7)>>> clf2.fit(x,y)RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini', max_depth=1, max_features='auto', max_leaf_nodes=None, min_impurity_split=1e-07, min_samples_leaf=54, min_samples_split=4, min_weight_fraction_leaf=0.0, n_estimators=50, n_jobs=1, oob_score=True, random_state=None, verbose=0, warm_start=False)>>> clf3.fit(x,y)SVC(C=0.1, cache_size=200, class_weight=None, coef0=0.0, decision_function_shape=None, degree=3, gamma='auto', kernel='rbf', max_iter=-1, probability=True, random_state=None, shrinking=True, tol=0.001, verbose=False)>>> eclf.fit(x,y)VotingClassifier(estimators=[('xgb', XGBClassifier(base_score=0.5, colsample_bylevel=1, colsample_bytree=0.6, gamma=0, learning_rate=0.1, max_delta_step=0, max_depth=1, min_child_weight=2, missing=None, n_estimators=140, nthread=4, objective='binary:logistic', reg_alpha=0, reg_lambda=1, s...', max_iter=-1, probability=True, random_state=None, shrinking=True, tol=0.001, verbose=False))], n_jobs=1, voting='soft', weights=[2, 1, 1])>>> pre = eclf.predict_proba(test[predictors_test])>>> np.savetxt("D:\Python\\1\\result_vote_xgb_rf_svm.csv", pre, delimiter=',')
结果比三个中最好的xgboost高了0.01,可喜可贺!
PLAN3 Using the VotingClassifier with GridSearch
用GridSearch,可以在投票方法进行模型聚合的同时,调整每个单独模型的参数。由于三个模型的参数我已经在聚合之前调整好了,所以,我就不做这个了。
0 0
- VotingClassifier 模型聚合——投票
- 工作流模型分析(3)——流程发散聚合模型
- 工作流模型分析(3)——流程发散聚合模型(有图)
- 简易在线投票系统(php)——发布投票页面
- 简易在线投票系统(php)——投票页面
- 简易在线投票系统(php)——投票结果页面
- 投票统计—投票过滤器类的设计与实现
- ACM投票——二分法
- 工作流模型分析---聚合模型
- 工作流模型分析---聚合模型
- 工作流模型分析---聚合模型
- 工作流模型分析---聚合模型
- 工作流模型分析---聚合模型
- 查询—聚合函数
- 1003—IP聚合
- 并查集——电影投票
- ZendFramework小例子——投票
- Android自定义View——投票进度条
- Android之ConstraintLayout(依赖约束)布局
- vue2.x响应式原理,vue与react响应式简单对比
- Oracle数据库学习
- 汇编伪指令的学习
- Centos7 安装 PHP7最新版
- VotingClassifier 模型聚合——投票
- vbs脚本操纵系统文件
- POJ2385【DP】
- Scala Case类
- linux下nginx命令方法总结
- SQL中,无法连接到本机服务器,error:40-无法打开到SQL Server的连接(Microsoft SQL Server,错误:2)
- linux无法连接网络
- java面试——mybatis 面试题
- hibernate用like进行模糊查询时不能写单引号!!!