Best way to combine probabilistic classifiers in scikit-learn

前端 未结 4 876
误落风尘
误落风尘 2021-01-30 11:46

I have a logistic regression and a random forest and I\'d like to combine them (ensemble) for the final classification probability calculation by taking an average.

Is t

4条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-30 12:00

    NOTE: The scikit-learn Voting Classifier is probably the best way to do this now


    OLD ANSWER:

    For what it's worth I ended up doing this as follows:

    class EnsembleClassifier(BaseEstimator, ClassifierMixin):
        def __init__(self, classifiers=None):
            self.classifiers = classifiers
    
        def fit(self, X, y):
            for classifier in self.classifiers:
                classifier.fit(X, y)
    
        def predict_proba(self, X):
            self.predictions_ = list()
            for classifier in self.classifiers:
                self.predictions_.append(classifier.predict_proba(X))
            return np.mean(self.predictions_, axis=0)
    

提交回复
热议问题