What's the most elegant way to check whether an sklearn model has been fitted? i.e. whether its fit()
function has been called after it was instantiated, or not.
The fit() method takes the training data as arguments, which can be one array in the case of unsupervised learning, or two arrays in the case of supervised learning.
For using the Model Serving service, select the Model Serving service on the left panel (1) and then select on Create new serving (2). Next, select “SkLearn serving” and click on the “Python Script” button to select a python script from your project that you want to serve.
Fitting data: the main API implemented by scikit-learn is that of the estimator . An estimator is any object that learns from data; it may be a classification, regression or clustering algorithm or a transformer that extracts/filters useful features from raw data.
Fit function adjusts weights according to data values so that better accuracy can be achieved. After training, the model can be used for predictions, using . predict() method call.
You can do something like:
from sklearn.exceptions import NotFittedError for model in models: try: model.predict(some_test_data) except NotFittedError as e: print(repr(e))
Ideally you would check the results of model.predict
against expected results but if all you want to know if wether the model is fitted or not that should suffice.
Some commenters have suggested using check_is_fitted. I consider check_is_fitted
an internal method. Most algorithms will call check_is_fitted
inside their predict method which in turn might raise NotFittedError
if needed. The problem with using check_is_fitted
directly is that it is model specific, i.e. you need to know which members to check depending on your algorithm. For example:
╔════════════════╦════════════════════════════════════════════╗ ║ Tree models ║ check_is_fitted(self, 'tree_') ║ ║ Linear models ║ check_is_fitted(self, 'coefs_') ║ ║ KMeans ║ check_is_fitted(self, 'cluster_centers_') ║ ║ SVM ║ check_is_fitted(self, 'support_') ║ ╚════════════════╩════════════════════════════════════════════╝
and so on. So in general I would recommend calling model.predict()
and letting the specific algorithm handle the best way to check whether it is already fitted or not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With