flennerhag / mlens

ML-Ensemble – high performance ensemble learning
http://ml-ensemble.com
MIT License
843 stars 108 forks source link

If I already have trained models, how can I use mlens #137

Open xuzhang5788 opened 3 years ago

xuzhang5788 commented 3 years ago

I already have trained several models, I want to ensemble them in the end. I don't want to retrain my models. I can get model.predict(test) values. How to stack them together using mlens? Many thanks

AlanGanem commented 3 years ago

Hey, @xuzhang5788 , have you considered creating a wrapper class like this one?

class PrefitEstimator(sklearn.base.BaseEstimator):

    def __init__(self, prefit_estimator):
        self.prefit_estimator = prefit_estimator

    def __getattr__(self, attr):
        '''
        gets the attributes from prefit_estimator, except if the attribute (or method)
        is "fit".

        if the "transform" or "predict" method is called, it'll return self.prefit_estimator's method
        '''
        if attr == 'fit':
            return self.fit        
        elif attr == 'fit_transform':
            return self.fit_transform
        elif attr == 'fit_predict':
            return self.fit_predict            
        else:
            return getattr(self.prefit_estimator, attr)

    def fit(self, X, y = None, **kwargs):
        '''
        the fit method does nothing (since prefit_estimator is already fitted) and returns self.
        '''
        return self    

    def fit_transform(self, X, y = None, **kwargs):
        return self.transform(X) #will get "transform" method from self.prefit_estimator

    def fit_predict(self, X, y = None, **kwargs):
        return self.predict(X) #will get "predict" method from self.prefit_estimator
tanweer-mahdi commented 2 years ago

Excellent question @xuzhang5788. Did @AlanGanem 's solution work?

xuzhang5788 commented 2 years ago

@tanweer-mahdi I didn't try. If you would like to try, please let me know if it works. Many thanks.