Using sklearn voting ensemble with partial fit

后端 未结 5 1088
太阳男子
太阳男子 2021-01-04 10:49

Can someone please tell how to use ensembles in sklearn using partial fit. I don\'t want to retrain my model. Alternatively, can we pass pre-trained models for ensembling ?

5条回答
  •  暖寄归人
    2021-01-04 11:43

    It's not too hard to implement the voting. Here's my implementation:

    import numpy as np 
    
    class VotingClassifier(object):
        """ Implements a voting classifier for pre-trained classifiers"""
    
        def __init__(self, estimators):
            self.estimators = estimators
    
        def predict(self, X):
            # get values
            Y = np.zeros([X.shape[0], len(self.estimators)], dtype=int)
            for i, clf in enumerate(self.estimators):
                Y[:, i] = clf.predict(X)
            # apply voting 
            y = np.zeros(X.shape[0])
            for i in range(X.shape[0]):
                y[i] = np.argmax(np.bincount(Y[i,:]))
            return y
    

提交回复
热议问题