[机器学习python实践(5)]Sklearn实现集成

匿名 (未验证) 提交于 2019-12-02 22:56:40

1,集成

集成(Ensemble)分类模型是综合考量多个分类器的预测结果,从而做出决策。一般分为两种方式:
1)利用相同的训练数据同时搭建多个独立的分类模型,然后通过投票的方式,以少数服从多数的原则做出最终的分类决策。如随即森林分类器的思想是在相同的训练数据上同时搭建多棵决策树。随机森林分类器在构建每一棵决策树会随机选择特征,而不是根据每维特征对预测结果的影响程度进行排序。

2)按照一定次序搭建多个分类模型。这些模型之间彼此存在依赖关系。一般而言,每一个后续模型的加入都需要对现有集成模型的综合性能有所贡献,进而不断提升更新过后的集成模型的性能,并最终期望借助整合多个分类能力较弱的分类器,搭建出具有更强分类能力的模型。如梯度提升决策树:它生成每一棵决策树的过程中都会尽可能降低整体集成模型在训练集上的拟合误差。

2.例子

数据集:同上一篇文章

代码:

#coding=utf-8 import pandas as pd from sklearn.model_selection import train_test_split from sklearn.feature_extraction import DictVectorizer from sklearn.tree import DecisionTreeClassifier from sklearn.ensemble import RandomForestClassifier from sklearn.ensemble import GradientBoostingClassifier from sklearn.metrics import classification_report  #1.数据获取 titanic = pd.read_csv(http://biostat.mc.vanderbilt.edu/wiki/pub/Main/DataSets/titanic.txt) X = titanic[[pclass, age, sex]] y = titanic[survived]  #2.数据预处理:训练集测试集分割,数据标准化 X[age].fillna(X[age].mean(),inplace=True)   #age只有633个,需补充,使用平均数或者中位数都是对模型偏离造成最小的策略 X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.25,random_state=33)  # 将数据进行分割 vec = DictVectorizer(sparse=False) X_train = vec.fit_transform(X_train.to_dict(orient=record))   #对训练数据的特征进行提取 X_test = vec.transform(X_test.to_dict(orient=record))         #对测试数据的特征进行提取  #3.集成模型训练 # 使用单一决策树进行模型训练以及预测分析 dtc = DecisionTreeClassifier() dtc.fit(X_train, y_train) dtc_y_pred = dtc.predict(X_test)  #使用随机森林分类器进行集成模型的训练以及预测分析。 rfc = RandomForestClassifier() rfc.fit(X_train, y_train) rfc_y_pred = rfc.predict(X_test)  #使用梯度提升决策树进行集成模型的训练以及预测分析。 gbc = GradientBoostingClassifier() gbc.fit(X_train, y_train) gbc_y_pred = gbc.predict(X_test)  #4.获取结果报告 # 输出单一决策树在测试集上的分类准确性,以及更加详细的精确率、召回率、F1指标。 print The accuracy of decision tree is, dtc.score(X_test, y_test) print classification_report(dtc_y_pred, y_test)  # 输出随机森林分类器在测试集上的分类准确性,以及更加详细的精确率、召回率、F1指标。 print The accuracy of random forest classifier is, rfc.score(X_test, y_test) print classification_report(rfc_y_pred, y_test)  # 输出梯度提升决策树在测试集上的分类准确性,以及更加详细的精确率、召回率、F1指标。 print The accuracy of gradient tree boosting is, gbc.score(X_test, y_test) print classification_report(gbc_y_pred, y_test)

运行结果:单个决策树,随机森林,梯度提升决策树的表现如下:

The accuracy of decision tree is 0.781155015198              precision    recall  f1-score   support            0       0.91      0.78      0.84       236           1       0.58      0.80      0.67        93  avg / total       0.81      0.78      0.79       329 

The accuracy of random forest classifier is 0.784194528875              precision    recall  f1-score   support            0       0.92      0.77      0.84       239           1       0.57      0.81      0.67        90  avg / total       0.82      0.78      0.79       329
The accuracy of gradient tree boosting is 0.790273556231              precision    recall  f1-score   support            0       0.92      0.78      0.84       239           1       0.58      0.82      0.68        90  avg / total       0.83      0.79      0.80       329

结论:
预测性能:梯度上升决策树大于随机森林分类器大于单一决策树。工业界经常使用随机森林分类模型作为基线系统。

原文:https://www.cnblogs.com/youngsea/p/9330695.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!