码迷,mamicode.com
首页 > 编程语言 > 详细

Python机器学习(基础篇---监督学习(集成模型))

时间:2019-03-17 00:54:09      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:方式   bsp   user   互联网   pre   机器   使用   min   epo   

集成模型

集成分类模型是综合考量多个分类器的预测结果,从而做出决策。

综合考量的方式大体分为两种:

1.利用相同的训练数据同时搭建多个独立的分类模型,然后通过投票的方式,以少数服从多数的原则作出最终的分类决策。(随机森林分类器)

2.按照一定次序搭建多个分类模型。这些模型之间彼此存在依赖关系。一般而言,每一个后续模型的加入都要对现有集成模型的综合性能有所贡献,进而不断提升更新过后的集成模型的性能。(梯度提升决策树)

代码1:

 #集成模型对泰坦尼克号乘客是否生还的预测

#导入pandas,并且重命名为pd

import pandas as pd

#通过互联网读取泰坦尼克乘客档案,并存储在变量titanic中

titanic=pd.read_csv(‘技术图片http://biostat.mc.vanderbilt.edu/wiki/pub/Main/DataSets/titanic.txt‘)

#观察前几行数据

print(titanic.head())

#查看数据统计特性

技术图片titanic.info()



X=titanic[[‘pclass‘,‘age‘,‘sex‘]]

# print(X)

#对当前选择的特征进行探查

技术图片X.info()

y=titanic[[‘survived‘]]

# print(y)

#对于缺失的年龄信息,我们使用全体乘客的平均年龄代替,

#填充age缺失值,使用平均数或中位数

X[‘age‘].fillna(X[‘age‘].mean(),inplace=True)

#查看数据特征

技术图片X.info()

from sklearn.cross_validation import train_test_split

#随机采样25%的数据用于测试,剩下的75%用于构建训练集合

X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.25,random_state=33)

#对类别型特征进行转换,成为特征向量

from sklearn.feature_extraction import DictVectorizer

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‘))

#使用单一决策树

from sklearn.tree import DecisionTreeClassifier

dtc=DecisionTreeClassifier()

dtc.fit(X_train,y_train)

dtc_y_pred=dtc.predict(X_test)

#使用随机森林

from sklearn.ensemble import RandomForestClassifier

rfc=RandomForestClassifier()

rfc.fit(X_train,y_train)

rfc_y_pred=rfc.predict(X_test)

#使用梯度提升决策树进行集成模型的训练以及预测分析

from sklearn.ensemble import GradientBoostingClassifier

gbc=GradientBoostingClassifier()

gbc.fit(X_train,y_train)

gbc_y_pred=gbc.predict(X_test)

#集成模型对泰坦尼克号乘客是否生还的预测性能

#使用模型自带的评估函数进行准确性测评

print(‘The Accuracy of decision tree is‘,dtc.score(X_test,y_test))

#从sklearn.metrics里导入classification_report模块

from sklearn.metrics import classification_report

print(classification_report(dtc_y_pred,y_test))



print(‘The Accuracy of random forest classifier is‘,rfc.score(X_test,y_test))

print(classification_report(rfc_y_pred,y_test))



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.7811550151975684

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.7811550151975684

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 gradient tree boosting is 0.790273556231003

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

Python机器学习(基础篇---监督学习(集成模型))

标签:方式   bsp   user   互联网   pre   机器   使用   min   epo   

原文地址:https://www.cnblogs.com/yxllfl/p/10544925.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!