Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to test whether an sklearn model has been fitted?

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.

like image 554
user3436624 Avatar asked Oct 05 '16 21:10

user3436624


People also ask

What does fit () do in sklearn?

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.

How do you serve a sklearn model?

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.

What is estimator in sklearn?

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.

What does a fit function do?

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.


1 Answers

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.

Update:

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.

like image 65
elyase Avatar answered Oct 17 '22 02:10

elyase