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

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)