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. QT 中文乱码问题

    1. 在main函数中创建完 QApplication对象后马上添加 QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8&qu ...

  2. php环境之Wampserver端口修改

    WampServer是一款由法国人开发的Apache Web服务器.PHP解释器以及MySQL数据库的整合软件包.免去了开发人员将时间花费在繁琐的配置环境过程,从而腾出更多精力去做开发.WampSer ...

  3. Spring初学之FactoryBean配置Bean

    实体bean: Car.java: package spring.beans.factorybean; public class Car { private String name; private ...

  4. LightOJ 1245 数学

    Harmonic Number (II) Description I was trying to solve problem '1234 - Harmonic Number', I wrote the ...

  5. Use default arguments instead of short circuiting or conditionals使用默认实参代替短路和条件

  6. IntelliJ Idea 免费激活方法免激活码

    1. 到网站 http://idea.lanyus.com/ 获取注册码. 2.填入下面的license server: http://intellij.mandroid.cn/ http://ide ...

  7. Python快速学习-高级特性

    1.切片 取一个list或tuple的部分元素是非常常见的操作 L = ['hello','the','world','and','my','love'] 取前三个元素 L[0:3],L[:3] 取倒 ...

  8. Angular各版本和组件下载

    Angular各版本和组件下载:https://code.angularjs.org/

  9. js的constructor

    js创建一个构造函数,会默认在原型链上添加一个constructor的属性,它保存了构造函数内的代码. 一般情况下我们不需要去改动它,但是有些时候我们会不经意的改写它. 比如下面这个例子: var F ...

  10. 177. Nth Highest Salary

    问题描述 解决方案 CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT BEGIN declare number int; set numbe ...