+4 votes
in Machine Learning by (17.9k points)
I want to dynamically check whether an estimator’s  (e.g., logistic regression) fit() method supports a given parameter. Is there any scikit-learn method for it?

1 Answer

+1 vote
by (14.6k points)

There is a method "has_fit_parameter()" in sklearn.utils. It checks whether the estimator’s fit() method supports the given parameter. If the estimator supports the parameter, the method returns True; otherwise, False.

Here is an example:

>>> from sklearn.linear_model import LogisticRegression

>>> from sklearn.utils.validation import has_fit_parameter

>>> has_fit_parameter(LogisticRegression(), "sample_weight")

True

>>> has_fit_parameter(LogisticRegression(), "class_weight")

False

The fit() method of LogisticRegression supports sample_weight but does not support class_weight.


...