mlens icon indicating copy to clipboard operation
mlens copied to clipboard

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

Open xuzhang5788 opened this issue 3 years ago • 3 comments

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

xuzhang5788 avatar Apr 23 '21 23:04 xuzhang5788

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

AlanGanem avatar Jun 26 '21 02:06 AlanGanem

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

tanweer-mahdi avatar Jan 27 '22 21:01 tanweer-mahdi

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

xuzhang5788 avatar Jan 28 '22 00:01 xuzhang5788