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. <
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.