Skip to content

[Question] How to get individual models from the ensemble? #1224

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

Closed
FelipeFuhr opened this issue Aug 17, 2021 · 2 comments
Closed

[Question] How to get individual models from the ensemble? #1224

FelipeFuhr opened this issue Aug 17, 2021 · 2 comments
Labels

Comments

@FelipeFuhr
Copy link

Hello, nice project!

Is there a way to retrieve the individual models from the final ensemble? I mean, not only the hyperparameters used to train it, but the individual weights learned in the fitting/training phase.

Thank you

@eddiebergman
Copy link
Contributor

Hi @FelipeFuhr,

This is not documented and should be made more accessible as in our plans for the future but here is a code snippet to help you out. I've also renamed your question to be more precise.

from autosklearn.pipeline.util import get_dataset
from autosklearn.classification import AutoSklearnClassifier

X_train, y_train, X_test, y_test = get_dataset('iris')
automodel = AutoSklearnClassifier(time_left_for_this_task=60)
automodel.fit(X_train, y_train)

# A list of pipelines with their weights [ (ensemble_weight, Pipeline) ]
models_with_weights = automodel.get_models_with_weights() 

# Get the first model with it's weight
weight, model = models_with_weights[0]

# Note that these models and the following are sklearn compatible
# The steps in the models pipeline
# [ 
#    ('data_preprocessing', DataPreprocessor),
#    ('balancing', Balancing ),
#    ('feature_preprocessor', FeaturePreprocessorChoice)
#    ('classifier', ClassifierChoice)
# ]
models_steps = model.steps

# Get the ClassifierChoice wrapper
classifier_str, classifier = model.steps[-1]

# The autosklearn wrapped model 
classifier = classifier.choice 
print(type(classifier)) # autosklearn.pipeline.components.classification.random_forest.RandomForest

# The sklearn model
sklearn_classifier = classifier.estimator

Note that these models were trained with the other three parts of the pipeline in mind so if you were planning to use them in some way, you'd have to consider those too.

@eddiebergman eddiebergman changed the title [Question] My Question? [Question] How to get individual models from the ensemble? Aug 17, 2021
@eddiebergman
Copy link
Contributor

This has put into issue #1298 that we need to document things better

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

No branches or pull requests

2 participants