Using sklearn voting ensemble with partial fit

后端 未结 5 1060
太阳男子
太阳男子 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:47

    The Mlxtend library has an implementation works, you still need to call the fit function for the EnsembleVoteClassifier. Seems the fit function doesn't really modify any parameters rather checking the possible label values. In the example below, you have to give an array contains all the possible values appear in original y(in this case 1,2) to eclf2.fit It doesn't matter for X.

    import numpy as np
    from mlxtend.classifier import EnsembleVoteClassifier
    from sklearn.linear_model import LogisticRegression
    from sklearn.naive_bayes import GaussianNB
    import copy
    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])
    
    for clf in (clf1, clf2, clf3):
        clf.fit(X, y)    
    eclf2 = EnsembleVoteClassifier(clfs=[clf1, clf2, clf3],voting="soft",refit=False)
    eclf2.fit(None,np.array([1,2]))
    print(eclf2.predict(X))
    

提交回复
热议问题