集成模型-泰坦尼克号生还预测

集成模型(分类)(Ensemble)
集成分类模型综合考量多个分类器的预测结果,进而做出决策。
综合考量的方式大体分两种:
1.利用相同的训练数据同时搭建多个独立的分类模型,然后通过投票的方式,以少数服从多数的原则作为最终的分类决策。代表性的模型有:随机森林分类器(Random Forest Classifier),在相同的训练数据上同时搭建多棵决策树(Decision Tree)。一株标准的决策树会根据每维特征对预测结果的影响程度进行排序,进而决定不同特征从上至下构建分裂节点的顺序;这样,所有在随机森林中的决策树都会受这一策略影响
而构建得完全一致,丧失了多样性。随机森林分类器在构建过程中,每一棵决策树都会放弃这一固定的排序算法,转而随机选取特征。
2.另一种是按照一定次序搭建多个分类模型。这些模型彼此之间存在依赖关系。一般而言,每一个后续模型的加入都需要对现有集成模型的综合性能有所贡献,进而不断提升更新过后的集成模型的性能,并最终期望借助整合多个分类能力较弱的分类器,搭建出具有更强分类能力的模型。具有代表性的是梯度提升决策树(Gradient Tree Boosting)。与构建随机森林分类器模型不同,这里每一棵决策树在生成的过程中都会尽可能降低整体集成模型在训练集上的拟合误差。
为了对比单一决策树(Decision Tree)与集成模型中随机森林分类器(Random Forest Classifier)以及梯度提升决策树(Gradient Tree Boosting)的性能差异,本文依旧使用泰坦尼克号的乘客数据。

Python 源码:

#coding=utf-8
import pandas as pd
#-------------data split
from sklearn.cross_validation import train_test_split
#-------------feature transfer
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

#-------------download data
titanic=pd.read_csv('http://biostat.mc.vanderbilt.edu/wiki/pub/Main/DataSets/titanic.txt')
print titanic.head()
#transfer to dataFrame format by pandas,use info() to show statistics of data
print titanic.info()
#-------------feature selection
X=titanic[['pclass','age','sex']]
y=titanic['survived']

print 'bf processing\n',X.info()
#-------------feature processing
X['age'].fillna(X['age'].mean(),inplace=True)
print 'af processing\n',X.info
#-------------data split
#75% training set,25% testing set
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.25,random_state=33)
#-------------feature transfer  from String to int
vec=DictVectorizer(sparse=False)
X_train=vec.fit_transform(X_train.to_dict(orient='record'))
print vec.get_feature_names()
X_test=vec.transform(X_test.to_dict(orient='record'))
#-------------training  DT
#initialize
dtc=DecisionTreeClassifier()
dtc.fit(X_train,y_train)
dtc_y_predict=dtc.predict(X_test)
#-------------training  RFC
rfc=RandomForestClassifier()
rfc.fit(X_train,y_train)
rfc_y_predict=rfc.predict(X_test)
#-------------training  GBC
gbc=GradientBoostingClassifier()
gbc.fit(X_train,y_train)
gbc_y_predict=gbc.predict(X_test)
#-------------performance  DT
print 'The Accuracy DT is',dtc.score(X_test,y_test)
print classification_report(y_test,dtc_y_predict,target_names=['died','survived'])
#-------------performance  RFC
print 'The Accuracy RFC is',rfc.score(X_test,y_test)
print classification_report(y_test,rfc_y_predict,target_names=['died','survived'])
#-------------performance  GBC
print 'The Accuracy GBC is',gbc.score(X_test,y_test)
print classification_report(y_test,gbc_y_predict,target_names=['died','survived'])

Result:

The Accuracy DT is 0.781155015198
             precision    recall  f1-score   support
       died       0.78      0.91      0.84       202
   survived       0.80      0.58      0.67       127
avg / total       0.78      0.78      0.77       329


The Accuracy RFC is 0.784194528875
             precision    recall  f1-score   support
       died       0.78      0.91      0.84       202
   survived       0.80      0.58      0.68       127
avg / total       0.79      0.78      0.78       329


The Accuracy GBC is 0.790273556231
             precision    recall  f1-score   support
       died       0.78      0.92      0.84       202
   survived       0.82      0.58      0.68       127
avg / total       0.80      0.79      0.78       329


输出表明,仅仅使用模型的默认配置,梯度上升决策树具有最佳的预测性能,其次是随机森林分类器,最后是单一决策树。大量的其它数据上面的模型实践也证明了上述结论的普适性。一般工业界为了追求更好的预测性能,经常使用随机森林分类模型作为基线系统(Baseline System)。

基线系统通常指使用经典模型搭建的机器学习系统,研发人员每提出一个新模型,都需要和基线系统在多个具有代表性的数据集上进行性能比较的测试。随机森林分类模型就经常以基线系统的身份出现在科研论文,甚至公开的数据竞赛中。

模型特点:集成模型是实战应用中最为常见的,相比于其他单一的学习模型,集成模型可以整合多种模型,或者多次就一种类型的模型进行建模。由于模型估计参数的过程同样受到概率的影响,具有一定的不确定性。因此集成模型虽然在训练过程中耗费更多的时间,但是得到的综合模型往往具有更高的表现性能和更好的稳定性。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值