scores : array of float, shape=(len(list(cv)),) Array of scores of the estimator for each run of the cross validation.

关于scores:http://scikit-learn.org/stable/modules/cross_validation.html#cross-validation

第一个方法:

# -*- coding: utf-8 -*-
"""
Created on Tue Aug 09 22:12:13 2016 @author: Administrator
""" from sklearn import datasets
from sklearn import cross_validation
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import VotingClassifier iris = datasets.load_iris()
X, y = iris.data[:, 1:3], iris.target clf1 = LogisticRegression(random_state=1)
clf2 = RandomForestClassifier(random_state=1)
clf3 = GaussianNB() eclf = VotingClassifier(estimators=[('lr', clf1), ('rf', clf2), ('gnb', clf3)], voting='hard', weights=[2,1,2]) for clf, label in zip([clf1, clf2, clf3, eclf], ['Logistic Regression', 'Random Forest', 'naive Bayes', 'Ensemble']):
print clf
print label
scores = cross_validation.cross_val_score(clf, X, y, cv=5, scoring='accuracy')
print("Accuracy: %0.2f (+/- %0.2f) [%s]" % (scores.mean(), scores.std(), label))

第二个方法:

# -*- coding: utf-8 -*-
"""
Created on Tue Aug 09 22:06:31 2016 @author: Administrator
""" import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.ensemble import RandomForestClassifier, VotingClassifier clf1 = LogisticRegression(random_state=1)
clf2 = RandomForestClassifier(random_state=1)
clf3 = GaussianNB()
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
y = np.array([1, 1, 1, 2, 2, 2])
eclf1 = VotingClassifier(estimators=[('lr', clf1), ('rf', clf2), ('gnb', clf3)], voting='hard')
eclf1 = eclf1.fit(X, y)
print(eclf1.predict(X)) eclf2 = VotingClassifier(estimators=[('lr', clf1), ('rf', clf2), ('gnb', clf3)],voting='soft')
eclf2 = eclf2.fit(X, y)
print(eclf2.predict(X)) eclf3 = VotingClassifier(estimators=[('lr', clf1), ('rf', clf2), ('gnb', clf3)],voting='soft', weights=[2,1,1])
eclf3 = eclf3.fit(X, y)
print(eclf3.predict(X))

Parameters:

estimators : list of (string, estimator) tuples

Invoking the fit method on the VotingClassifier will fit clones of those original estimators that will be stored in the class attribute self.estimators_.

voting : str, {‘hard’, ‘soft’} (default=’hard’)

If ‘hard’, uses predicted class labels for majority rule voting. Else if ‘soft’, predicts the class label based on the argmax( 自动回归滑动平均模型) of the sums of the predicted probabilities, which is recommended for an ensemble of well-calibrated(标准的) classifiers.

#投票规则,默认hard,多数的票;soft 模式看不懂,大约是根据每个方法的概率吧

weights : array-like, shape = [n_classifiers], optional (default=`None`)

Sequence of weights (float or int) to weight the occurrences of predicted class labels (hard voting) or class probabilities before averaging (soft voting). Uses uniform weights if None.

#每个方法预先的权值,默认各方法权值相同.

VotingClassifier的更多相关文章

  1. sklearn 组合分类器

    组合分类器: 组合分类器有4种方法: (1)通过处理训练数据集.如baging  boosting (2)通过处理输入特征.如 Random forest (3)通过处理类标号.error_corre ...

  2. Kaggle竞赛 —— 泰坦尼克号(Titanic)

    完整代码见kaggle kernel 或 NbViewer 比赛页面:https://www.kaggle.com/c/titanic Titanic大概是kaggle上最受欢迎的项目了,有7000多 ...

  3. XGBoost、LightGBM的详细对比介绍

    sklearn集成方法 集成方法的目的是结合一些基于某些算法训练得到的基学习器来改进其泛化能力和鲁棒性(相对单个的基学习器而言)主流的两种做法分别是: bagging 基本思想 独立的训练一些基学习器 ...

  4. 壁虎书7 Ensemble Learning and Random Forests

    if you aggregate the predictions of a group of predictors,you will often get better predictions than ...

  5. Notes : <Hands-on ML with Sklearn & TF> Chapter 7

    .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { border: 1px so ...

  6. sklearn中各种分类器回归器都适用于什么样的数据呢?

    作者:匿名用户链接:https://www.zhihu.com/question/52992079/answer/156294774来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...

  7. 第19月第8天 斯坦福大学公开课机器学习 (吴恩达 Andrew Ng)

    1.斯坦福大学公开课机器学习 (吴恩达 Andrew Ng) http://open.163.com/special/opencourse/machinelearning.html 笔记 http:/ ...

  8. 再论sklearn分类器

    https://www.cnblogs.com/hhh5460/p/5132203.html 这几天在看 sklearn 的文档,发现他的分类器有很多,这里做一些简略的记录. 大致可以将这些分类器分成 ...

  9. sklearn学习总结(超全面)

    https://blog.csdn.net/fuqiuai/article/details/79495865 前言sklearn想必不用我多介绍了,一句话,她是机器学习领域中最知名的python模块之 ...

随机推荐

  1. .gitignore 无效

    利用.gitignore过滤文件,如编译过程中的中间文件,等等,这些文件不需要被追踪管理. 现象: 在.gitignore添加file1文件,以过滤该文件,但是通过Git status查看仍显示fil ...

  2. Node.js的原型继承函数 util.inherits

    转自:http://sentsin.com/web/179.html util.inherits(constructor, superConstructor)是一个实现对象间原型继承 的函数.Java ...

  3. android、ipone在文本框中输入文字的不同

    1.android机会输入时会在键盘上先显示,你确定后再填充如文本框 2.ipone机是你输入时就直接填充到文本框,当你选择输入信息时,就会先把文本框的内容清空,在填充选择的文字,这时就会有个问题,如 ...

  4. pandas的Series

    pandas.Series(data=None, index=None, dtype=None, name=None, copy=False, fastpath=False) 首先介绍一下基本的: d ...

  5. selenium+python3 鼠标事件

    1.鼠标右击 ActionChains(driver).contest_click(right_click).perform() 2.鼠标悬停 ActionChains(driver).move_to ...

  6. js 图片加载失败处理方法

    在项目中不可避免会用到图片,尤其是列表,有时候图片会加载失败:这样就会显示一个很难看的坏图片缩略图:下面介绍两种方法,解决这个问题: 1.如果在你的项目中有引入jQuery插件,你可以使用error( ...

  7. mysql: instr 多个字段 like数据

    你是否一直在寻找比MySQL的LIKE语句更高效的方法的,下面我就为你介绍几种. SELECT * FROM `order_shop` where instr(uuid,  'b') > 0 g ...

  8. 机器学习(三)—线性回归、逻辑回归、Softmax回归 的区别

    1.什么是回归?  是一种监督学习方式,用于预测输入变量和输出变量之间的关系,等价于函数拟合,选择一条函数曲线使其更好的拟合已知数据且更好的预测未知数据. 2.线性回归  于一个一般的线性模型而言,其 ...

  9. 解决mysql登录报错:ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)

    今天在安装一个压缩包mysql-5.7.19时,碰到了一系列问题,现将这些问题罗列出来: 一.  ERROR 2003 (HY000): Can't connect to MySQL server o ...

  10. 条款47:请使用traits class表示类型信息

    在stl的算法中,我们的希望往往是根据不同的迭代器类型进行不同的更有效率的操作: template<typename IterT, typename DistT> void advance ...