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

后端 未结 4 1000
误落风尘
误落风尘 2021-02-07 02:30

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

4条回答
  •  滥情空心
    2021-02-07 03:08

    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.

提交回复
热议问题