VotingClassifier
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 theVotingClassifier
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的更多相关文章
- sklearn 组合分类器
组合分类器: 组合分类器有4种方法: (1)通过处理训练数据集.如baging boosting (2)通过处理输入特征.如 Random forest (3)通过处理类标号.error_corre ...
- Kaggle竞赛 —— 泰坦尼克号(Titanic)
完整代码见kaggle kernel 或 NbViewer 比赛页面:https://www.kaggle.com/c/titanic Titanic大概是kaggle上最受欢迎的项目了,有7000多 ...
- XGBoost、LightGBM的详细对比介绍
sklearn集成方法 集成方法的目的是结合一些基于某些算法训练得到的基学习器来改进其泛化能力和鲁棒性(相对单个的基学习器而言)主流的两种做法分别是: bagging 基本思想 独立的训练一些基学习器 ...
- 壁虎书7 Ensemble Learning and Random Forests
if you aggregate the predictions of a group of predictors,you will often get better predictions than ...
- Notes : <Hands-on ML with Sklearn & TF> Chapter 7
.caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { border: 1px so ...
- sklearn中各种分类器回归器都适用于什么样的数据呢?
作者:匿名用户链接:https://www.zhihu.com/question/52992079/answer/156294774来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...
- 第19月第8天 斯坦福大学公开课机器学习 (吴恩达 Andrew Ng)
1.斯坦福大学公开课机器学习 (吴恩达 Andrew Ng) http://open.163.com/special/opencourse/machinelearning.html 笔记 http:/ ...
- 再论sklearn分类器
https://www.cnblogs.com/hhh5460/p/5132203.html 这几天在看 sklearn 的文档,发现他的分类器有很多,这里做一些简略的记录. 大致可以将这些分类器分成 ...
- sklearn学习总结(超全面)
https://blog.csdn.net/fuqiuai/article/details/79495865 前言sklearn想必不用我多介绍了,一句话,她是机器学习领域中最知名的python模块之 ...
随机推荐
- java——base64 加密和解密
base64 一.加密 *.若有要求输入字符必须为UTF-8: 则需str.getByte("utf-8"); //在getByte()中指定utf-8编码,否则中文字符将被加密 ...
- COUNT(DISTINCT a.TransportOrderID)的用法
DECLARE @StartDate DATETIME= '2017-12-20 00:00:00';DECLARE @EndDate DATETIME= '2017-12-26 00:00:00'; ...
- QT中phonon的安装和使用
http://write.blog.csdn.net/postedit Phonon严格来说其实非为Qt的library,Phonon原本就是KDE 4的开放原始码多媒体API,後来与Qt合并与开发, ...
- [Kafka] - Kafka内核理解:消息存储机制
一个Topic分为多个Partition来进行数据管理,一个Partition中的数据是有序.不可变的,使用偏移量(offset)唯一标识一条数据,是一个long类型的数据 Partition接收到p ...
- 2017-02-20 Sql Server2016安装后无法找到Microsoft Sql Server Management Studio管理器
最近安装的sql sever2016后发现没有Sql server management studio管理工具,无法操作sql server 解决方案,可去官网单独下载 Sql Server Mana ...
- SQL必知必会 记录
登录数据库mysql -u root -p查看所有数据库 show databases:选择数据库 use 数据库名:查看所有表 show tables查看表结构 descri ...
- JavaScriptSerializer 日期处理 JSON.Net
[WebMethod(Description = "取得所有人员 自带json")] [SoapHeader("key")] [ScriptMethod(Res ...
- Java企业微信开发_15_查询企业微信域名对应的所有ip
一.前言 二.方法 1.在线网站 百度搜索"域名查IP",可查到如下网站,输入域名即可查到所有IP: 站长工具 site.ip138.com tools.ipip.net 2.li ...
- mysql触发器与hash索引
url查询哈希值的维护 触发器 2.1 创建表 pseudohash. 2.2 创建触发器,当对表进行插入和更新时,触发 触发器 delimiter |create trigger pseudohas ...
- SQL夯实基础(一):inner join、outer join和cross join的区别
一.数据构建 先建表,再说话 create database Test use Test create table A ( AID ,) primary key, name ), age int ) ...