【Python-ML】集成多数投票分类器-训练评估调优

# -*- coding: utf-8 -*-
'''
Created on 2018年1月19日

@author: Jason.F
@summary: 集成学习,多数投票分类器
'''
from sklearn.base import BaseEstimator
from sklearn.base import ClassifierMixin
from sklearn.preprocessing import LabelEncoder
from sklearn.externals import six
from sklearn.base import clone
from sklearn.pipeline import _name_estimators
import numpy as np
import time
from sklearn import datasets
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.cross_validation import cross_val_score
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.pipeline import Pipeline
from sklearn.metrics import roc_curve
from sklearn.metrics import auc
import matplotlib.pyplot as plt
from itertools import product
from sklearn.grid_search import GridSearchCV
class MajorityVoteClassifier(BaseEstimator,ClassifierMixin):
    '''
    A majority vote ensemble classifier
    Paramters:
    classifiers:array-like,shape=[n_classifiers],Different classifiers for the ensemble
    vote:str,{'classlabel','probability'},Default:'classlabel',
         if 'classlabel' the prediction is based on the argmax of class labels,
         else if 'probability' ,the argmax of the sum of probabilities is used to predict the class label
         (recommened for calibrated classifier). 
    weights:array-like,shape=[n_classifiers],Optional,default:None
         if a list of 'int' or 'float' values are provided,the classifiers are weighted by importance;
         Uses uniform weights if 'weights=None'
    '''
    def __init__(self,classifiers,vote='classlabel',weights=None):
        self.classifiers = classifiers
        self.named_classifiers = {key:value for key,value in _name_estimators(classifiers)}
        self.vote = vote
        self.weights=weights
    
    def fit(self,X,y):
        '''
        Fit class
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值