from sklearn.ensemble import VotingClassifier 少数服从多数投票方法的集成分类器
【j集成学习】
# 导入数据 iris鸢尾花数据
import numpy as np
import warnings ##提醒函数变动的,所以在视频演示中过滤掉
from sklearn import datasets
warnings.filterwarnings("ignore")
iris = datasets.load_iris()
X, y = iris.data[:, 1:3], iris.target
from sklearn import cross_validation
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.ensemble import RandomForestClassifier
np.random.seed(21)
clf1 = LogisticRegression()
clf2 = RandomForestClassifier()
clf3 = GaussianNB()
print('5折交叉验证:\n')
##zip函数中将对象中元素打包成元祖
for clf, label in zip([clf1, clf2, clf3], ['逻辑回归', '随机森林', '朴素贝叶斯']):
scores = cross_validation.cross_val_score(clf, X, y, cv=5, scoring='accuracy')
print("准确率: %0.2f (+/- %0.2f) [%s]" % (scores.mean(), scores.std(), label))
from sklearn.ensemble import VotingClassifier
np.random.seed(21)
eclf1 = VotingClassifier(
estimators=[('逻辑回归', clf1), ('随机森林', clf2), ('朴素贝叶斯', clf3)], voting='soft')
scores = cross_validation.cross_val_score(eclf1, X, y, cv=5, scoring='accuracy')
print("准确率: %0.2f (+/- %0.2f) [%s]" % (scores.mean(), scores.std(), label))