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












10















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.










share|improve this question

























  • i'm not sure if I understand what you want, but a for loop will do the job!!!

    – Juggernaut
    Oct 5 '16 at 21:49






  • 1





    Just to clarify, the person below answered perfectly.

    – user3436624
    Nov 2 '16 at 17:32






  • 2





    I've edited the question so that it is clear, can someone please unclose it? It's a good question and I have an alternate answer that I cannot add since it is still closed.

    – Moot
    Jul 13 '17 at 7:05






  • 2





    The correct answer is to use scikit's check_is_fitted

    – sapo_cosmico
    Nov 9 '17 at 16:57






  • 1





    @sapo_cosmico, thanks for bringing up check_is_fitted. I updated the answer with more details.

    – elyase
    Apr 3 '18 at 11:11


















10















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.










share|improve this question

























  • i'm not sure if I understand what you want, but a for loop will do the job!!!

    – Juggernaut
    Oct 5 '16 at 21:49






  • 1





    Just to clarify, the person below answered perfectly.

    – user3436624
    Nov 2 '16 at 17:32






  • 2





    I've edited the question so that it is clear, can someone please unclose it? It's a good question and I have an alternate answer that I cannot add since it is still closed.

    – Moot
    Jul 13 '17 at 7:05






  • 2





    The correct answer is to use scikit's check_is_fitted

    – sapo_cosmico
    Nov 9 '17 at 16:57






  • 1





    @sapo_cosmico, thanks for bringing up check_is_fitted. I updated the answer with more details.

    – elyase
    Apr 3 '18 at 11:11
















10












10








10


1






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.










share|improve this question
















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.







python scikit-learn






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 13 '17 at 6:27









Thierry Lathuille

7,80582730




7,80582730










asked Oct 5 '16 at 21:30









user3436624user3436624

3101316




3101316













  • i'm not sure if I understand what you want, but a for loop will do the job!!!

    – Juggernaut
    Oct 5 '16 at 21:49






  • 1





    Just to clarify, the person below answered perfectly.

    – user3436624
    Nov 2 '16 at 17:32






  • 2





    I've edited the question so that it is clear, can someone please unclose it? It's a good question and I have an alternate answer that I cannot add since it is still closed.

    – Moot
    Jul 13 '17 at 7:05






  • 2





    The correct answer is to use scikit's check_is_fitted

    – sapo_cosmico
    Nov 9 '17 at 16:57






  • 1





    @sapo_cosmico, thanks for bringing up check_is_fitted. I updated the answer with more details.

    – elyase
    Apr 3 '18 at 11:11





















  • i'm not sure if I understand what you want, but a for loop will do the job!!!

    – Juggernaut
    Oct 5 '16 at 21:49






  • 1





    Just to clarify, the person below answered perfectly.

    – user3436624
    Nov 2 '16 at 17:32






  • 2





    I've edited the question so that it is clear, can someone please unclose it? It's a good question and I have an alternate answer that I cannot add since it is still closed.

    – Moot
    Jul 13 '17 at 7:05






  • 2





    The correct answer is to use scikit's check_is_fitted

    – sapo_cosmico
    Nov 9 '17 at 16:57






  • 1





    @sapo_cosmico, thanks for bringing up check_is_fitted. I updated the answer with more details.

    – elyase
    Apr 3 '18 at 11:11



















i'm not sure if I understand what you want, but a for loop will do the job!!!

– Juggernaut
Oct 5 '16 at 21:49





i'm not sure if I understand what you want, but a for loop will do the job!!!

– Juggernaut
Oct 5 '16 at 21:49




1




1





Just to clarify, the person below answered perfectly.

– user3436624
Nov 2 '16 at 17:32





Just to clarify, the person below answered perfectly.

– user3436624
Nov 2 '16 at 17:32




2




2





I've edited the question so that it is clear, can someone please unclose it? It's a good question and I have an alternate answer that I cannot add since it is still closed.

– Moot
Jul 13 '17 at 7:05





I've edited the question so that it is clear, can someone please unclose it? It's a good question and I have an alternate answer that I cannot add since it is still closed.

– Moot
Jul 13 '17 at 7:05




2




2





The correct answer is to use scikit's check_is_fitted

– sapo_cosmico
Nov 9 '17 at 16:57





The correct answer is to use scikit's check_is_fitted

– sapo_cosmico
Nov 9 '17 at 16:57




1




1





@sapo_cosmico, thanks for bringing up check_is_fitted. I updated the answer with more details.

– elyase
Apr 3 '18 at 11:11







@sapo_cosmico, thanks for bringing up check_is_fitted. I updated the answer with more details.

– elyase
Apr 3 '18 at 11:11














3 Answers
3






active

oldest

votes


















8














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.






share|improve this answer


























  • Thank you, this is perfect.

    – user3436624
    Nov 2 '16 at 17:32






  • 1





    @elyase is right for sklearn. There are others: from xgboost.core import XGBoostError and from lightgbm.sklearn import LightGBMError, for instance

    – George Fisher
    May 1 '17 at 19:37













  • @GeorgeFisher: True, but the OP did specify sklearn specifically.

    – Moot
    Jul 12 '17 at 23:20






  • 1





    This is not a good solution, as your try might fail because of a problem with some_test_data. For instance, if some_test_data = "duck", the model might be completely fine, but will fail. You will then report the wrong error. Use check_is_fitted instead

    – sapo_cosmico
    Nov 9 '17 at 16:53













  • Along the same lines with @elyase, see scikit-learn.org/stable/modules/generated/…

    – Zouzias
    Feb 3 '18 at 19:58



















2














I do this for classifiers:



def check_fitted(clf): 
return hasattr(clf, "classes_")





share|improve this answer































    1














    This is sort of a greedy approach, but it should be fine for most if not all models. The only time this might not work is for models that set an attribute ending in an underscore prior to being fit, which I'm pretty sure would violate scikit-learn convention so this should be fine.



    import inspect

    def is_fitted(model):
    """Checks if model object has any attributes ending with an underscore"""
    return 0 < len( [k for k,v in inspect.getmembers(model) if k.endswith('_') and not k.startswith('__')] )





    share|improve this answer
























    • interesting solution! Goes along the lines of scikit-learn.org/dev/glossary.html#term-fitted. +1

      – elyase
      Apr 3 '18 at 14:39











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f39884009%2fwhats-the-best-way-to-test-whether-an-sklearn-model-has-been-fitted%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    8














    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.






    share|improve this answer


























    • Thank you, this is perfect.

      – user3436624
      Nov 2 '16 at 17:32






    • 1





      @elyase is right for sklearn. There are others: from xgboost.core import XGBoostError and from lightgbm.sklearn import LightGBMError, for instance

      – George Fisher
      May 1 '17 at 19:37













    • @GeorgeFisher: True, but the OP did specify sklearn specifically.

      – Moot
      Jul 12 '17 at 23:20






    • 1





      This is not a good solution, as your try might fail because of a problem with some_test_data. For instance, if some_test_data = "duck", the model might be completely fine, but will fail. You will then report the wrong error. Use check_is_fitted instead

      – sapo_cosmico
      Nov 9 '17 at 16:53













    • Along the same lines with @elyase, see scikit-learn.org/stable/modules/generated/…

      – Zouzias
      Feb 3 '18 at 19:58
















    8














    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.






    share|improve this answer


























    • Thank you, this is perfect.

      – user3436624
      Nov 2 '16 at 17:32






    • 1





      @elyase is right for sklearn. There are others: from xgboost.core import XGBoostError and from lightgbm.sklearn import LightGBMError, for instance

      – George Fisher
      May 1 '17 at 19:37













    • @GeorgeFisher: True, but the OP did specify sklearn specifically.

      – Moot
      Jul 12 '17 at 23:20






    • 1





      This is not a good solution, as your try might fail because of a problem with some_test_data. For instance, if some_test_data = "duck", the model might be completely fine, but will fail. You will then report the wrong error. Use check_is_fitted instead

      – sapo_cosmico
      Nov 9 '17 at 16:53













    • Along the same lines with @elyase, see scikit-learn.org/stable/modules/generated/…

      – Zouzias
      Feb 3 '18 at 19:58














    8












    8








    8







    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.






    share|improve this answer















    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Apr 3 '18 at 14:38

























    answered Oct 6 '16 at 16:13









    elyaseelyase

    24k45784




    24k45784













    • Thank you, this is perfect.

      – user3436624
      Nov 2 '16 at 17:32






    • 1





      @elyase is right for sklearn. There are others: from xgboost.core import XGBoostError and from lightgbm.sklearn import LightGBMError, for instance

      – George Fisher
      May 1 '17 at 19:37













    • @GeorgeFisher: True, but the OP did specify sklearn specifically.

      – Moot
      Jul 12 '17 at 23:20






    • 1





      This is not a good solution, as your try might fail because of a problem with some_test_data. For instance, if some_test_data = "duck", the model might be completely fine, but will fail. You will then report the wrong error. Use check_is_fitted instead

      – sapo_cosmico
      Nov 9 '17 at 16:53













    • Along the same lines with @elyase, see scikit-learn.org/stable/modules/generated/…

      – Zouzias
      Feb 3 '18 at 19:58



















    • Thank you, this is perfect.

      – user3436624
      Nov 2 '16 at 17:32






    • 1





      @elyase is right for sklearn. There are others: from xgboost.core import XGBoostError and from lightgbm.sklearn import LightGBMError, for instance

      – George Fisher
      May 1 '17 at 19:37













    • @GeorgeFisher: True, but the OP did specify sklearn specifically.

      – Moot
      Jul 12 '17 at 23:20






    • 1





      This is not a good solution, as your try might fail because of a problem with some_test_data. For instance, if some_test_data = "duck", the model might be completely fine, but will fail. You will then report the wrong error. Use check_is_fitted instead

      – sapo_cosmico
      Nov 9 '17 at 16:53













    • Along the same lines with @elyase, see scikit-learn.org/stable/modules/generated/…

      – Zouzias
      Feb 3 '18 at 19:58

















    Thank you, this is perfect.

    – user3436624
    Nov 2 '16 at 17:32





    Thank you, this is perfect.

    – user3436624
    Nov 2 '16 at 17:32




    1




    1





    @elyase is right for sklearn. There are others: from xgboost.core import XGBoostError and from lightgbm.sklearn import LightGBMError, for instance

    – George Fisher
    May 1 '17 at 19:37







    @elyase is right for sklearn. There are others: from xgboost.core import XGBoostError and from lightgbm.sklearn import LightGBMError, for instance

    – George Fisher
    May 1 '17 at 19:37















    @GeorgeFisher: True, but the OP did specify sklearn specifically.

    – Moot
    Jul 12 '17 at 23:20





    @GeorgeFisher: True, but the OP did specify sklearn specifically.

    – Moot
    Jul 12 '17 at 23:20




    1




    1





    This is not a good solution, as your try might fail because of a problem with some_test_data. For instance, if some_test_data = "duck", the model might be completely fine, but will fail. You will then report the wrong error. Use check_is_fitted instead

    – sapo_cosmico
    Nov 9 '17 at 16:53







    This is not a good solution, as your try might fail because of a problem with some_test_data. For instance, if some_test_data = "duck", the model might be completely fine, but will fail. You will then report the wrong error. Use check_is_fitted instead

    – sapo_cosmico
    Nov 9 '17 at 16:53















    Along the same lines with @elyase, see scikit-learn.org/stable/modules/generated/…

    – Zouzias
    Feb 3 '18 at 19:58





    Along the same lines with @elyase, see scikit-learn.org/stable/modules/generated/…

    – Zouzias
    Feb 3 '18 at 19:58













    2














    I do this for classifiers:



    def check_fitted(clf): 
    return hasattr(clf, "classes_")





    share|improve this answer




























      2














      I do this for classifiers:



      def check_fitted(clf): 
      return hasattr(clf, "classes_")





      share|improve this answer


























        2












        2








        2







        I do this for classifiers:



        def check_fitted(clf): 
        return hasattr(clf, "classes_")





        share|improve this answer













        I do this for classifiers:



        def check_fitted(clf): 
        return hasattr(clf, "classes_")






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jul 5 '18 at 22:46









        O.rkaO.rka

        7,27530107172




        7,27530107172























            1














            This is sort of a greedy approach, but it should be fine for most if not all models. The only time this might not work is for models that set an attribute ending in an underscore prior to being fit, which I'm pretty sure would violate scikit-learn convention so this should be fine.



            import inspect

            def is_fitted(model):
            """Checks if model object has any attributes ending with an underscore"""
            return 0 < len( [k for k,v in inspect.getmembers(model) if k.endswith('_') and not k.startswith('__')] )





            share|improve this answer
























            • interesting solution! Goes along the lines of scikit-learn.org/dev/glossary.html#term-fitted. +1

              – elyase
              Apr 3 '18 at 14:39
















            1














            This is sort of a greedy approach, but it should be fine for most if not all models. The only time this might not work is for models that set an attribute ending in an underscore prior to being fit, which I'm pretty sure would violate scikit-learn convention so this should be fine.



            import inspect

            def is_fitted(model):
            """Checks if model object has any attributes ending with an underscore"""
            return 0 < len( [k for k,v in inspect.getmembers(model) if k.endswith('_') and not k.startswith('__')] )





            share|improve this answer
























            • interesting solution! Goes along the lines of scikit-learn.org/dev/glossary.html#term-fitted. +1

              – elyase
              Apr 3 '18 at 14:39














            1












            1








            1







            This is sort of a greedy approach, but it should be fine for most if not all models. The only time this might not work is for models that set an attribute ending in an underscore prior to being fit, which I'm pretty sure would violate scikit-learn convention so this should be fine.



            import inspect

            def is_fitted(model):
            """Checks if model object has any attributes ending with an underscore"""
            return 0 < len( [k for k,v in inspect.getmembers(model) if k.endswith('_') and not k.startswith('__')] )





            share|improve this answer













            This is sort of a greedy approach, but it should be fine for most if not all models. The only time this might not work is for models that set an attribute ending in an underscore prior to being fit, which I'm pretty sure would violate scikit-learn convention so this should be fine.



            import inspect

            def is_fitted(model):
            """Checks if model object has any attributes ending with an underscore"""
            return 0 < len( [k for k,v in inspect.getmembers(model) if k.endswith('_') and not k.startswith('__')] )






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 1 '18 at 1:42









            David MarxDavid Marx

            5,06512447




            5,06512447













            • interesting solution! Goes along the lines of scikit-learn.org/dev/glossary.html#term-fitted. +1

              – elyase
              Apr 3 '18 at 14:39



















            • interesting solution! Goes along the lines of scikit-learn.org/dev/glossary.html#term-fitted. +1

              – elyase
              Apr 3 '18 at 14:39

















            interesting solution! Goes along the lines of scikit-learn.org/dev/glossary.html#term-fitted. +1

            – elyase
            Apr 3 '18 at 14:39





            interesting solution! Goes along the lines of scikit-learn.org/dev/glossary.html#term-fitted. +1

            – elyase
            Apr 3 '18 at 14:39


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f39884009%2fwhats-the-best-way-to-test-whether-an-sklearn-model-has-been-fitted%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks