Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
xuzhang5788 opened this issue Apr 23, 2021 · 3 comments
Open

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

xuzhang5788 opened this issue Apr 23, 2021 · 3 comments

Comments

@xuzhang5788
Copy link

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
Copy link

AlanGanem commented Jun 26, 2021

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
Copy link

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

@xuzhang5788
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants