# -*- 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
【Python-ML】集成多数投票分类器-训练评估调优
最新推荐文章于 2024-07-03 13:20:03 发布