0

Suppose a machine learning model, such as LightGBM's LGBMRegressor, has an attribute best_iteration_. How is this attribute accessible after calling the fit method, whereby sklearn's Pipeline and MultiOutputRegressor were utilized?

For Pipeline I've tried named_steps:

foo.named_steps['reg']

which returns the following object sklearn.multioutput.MultiOutputRegressor.

Then, I've tried .estimators_:

foo.named_steps['reg'].estimators_

which returns a list. However, the list contains the initial parameters that were supplied to the model.

Could someone please explain the ideal way to access a model's attributes?

sunspots
  • 1,047
  • 13
  • 29
  • This might be part of the solution: https://stackoverflow.com/questions/28822756/getting-model-attributes-from-scikit-learn-pipeline/58359509#58359509 – Guillaume Chevalier Oct 16 '19 at 05:48

1 Answers1

1

I assume foo is a sklearn pipeline object, if so, you can probably do this:

for e in foo.named_steps['reg'].estimators_:
    print(e.best_iteration_)
  • foo.named_steps['reg'].estimators_ returns a list of estimators inside of MultiOutputRegressor.
  • e is the LGBMRegressor you used inside of your MultiOutputRegressor.

You can replace best_iteration_ with any attributes of the model you wanted to access.

Alex Ramses
  • 538
  • 3
  • 19
  • Yes, `foo` has type `sklearn.pipeline.Pipeline`. However, I tried your `for` loop suggestion, and `None` is returned. – sunspots Oct 16 '19 at 09:07
  • According to the [documentation](https://lightgbm.readthedocs.io/en/latest/pythonapi/lightgbm.LGBMRegressor.html#lightgbm.LGBMRegressor.best_iteration_), and I quote: `The best iteration of fitted model if early_stopping_rounds has been specified.` You need to specify `early_stopping_rounds` or it will return None. – Alex Ramses Oct 16 '19 at 09:17