What SKLearn classifiers come with class_weight parameter












0















Working on an imbalanced project I was wondering what classifiers come with a class_weigth parameter out of the box.



Having been inspired by:



from sklearn.utils.testing import all_estimators

estimators = all_estimators()

for name, class_ in estimators:
if hasattr(class_, 'predict_proba'):
print(name)


'compute_class_weight' is a function and not a class. So essentially I am looking for a snippet that prints any classifier that calls for compute_class_weight (to be 'balanced':-) function.










share|improve this question



























    0















    Working on an imbalanced project I was wondering what classifiers come with a class_weigth parameter out of the box.



    Having been inspired by:



    from sklearn.utils.testing import all_estimators

    estimators = all_estimators()

    for name, class_ in estimators:
    if hasattr(class_, 'predict_proba'):
    print(name)


    'compute_class_weight' is a function and not a class. So essentially I am looking for a snippet that prints any classifier that calls for compute_class_weight (to be 'balanced':-) function.










    share|improve this question

























      0












      0








      0








      Working on an imbalanced project I was wondering what classifiers come with a class_weigth parameter out of the box.



      Having been inspired by:



      from sklearn.utils.testing import all_estimators

      estimators = all_estimators()

      for name, class_ in estimators:
      if hasattr(class_, 'predict_proba'):
      print(name)


      'compute_class_weight' is a function and not a class. So essentially I am looking for a snippet that prints any classifier that calls for compute_class_weight (to be 'balanced':-) function.










      share|improve this question














      Working on an imbalanced project I was wondering what classifiers come with a class_weigth parameter out of the box.



      Having been inspired by:



      from sklearn.utils.testing import all_estimators

      estimators = all_estimators()

      for name, class_ in estimators:
      if hasattr(class_, 'predict_proba'):
      print(name)


      'compute_class_weight' is a function and not a class. So essentially I am looking for a snippet that prints any classifier that calls for compute_class_weight (to be 'balanced':-) function.







      python-3.x scikit-learn






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '18 at 14:46









      MaartenkMaartenk

      32




      32
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You can get the classifiers (not all estimators) and check for class_weight attribute in the instantiated objects:



          from sklearn.utils.testing import all_estimators

          estimators = all_estimators(type_filter='classifier')
          for name, class_ in estimators:
          if hasattr(class_(), 'class_weight'): # Note the parenthesis: class_()
          print(name)


          Generates the list of the classifiers that can handle class imbalance:



          DecisionTreeClassifier
          ExtraTreeClassifier
          ExtraTreesClassifier
          LinearSVC
          LogisticRegression
          LogisticRegressionCV
          NuSVC
          PassiveAggressiveClassifier
          Perceptron
          RandomForestClassifier
          RidgeClassifier
          RidgeClassifierCV
          SGDClassifier
          SVC




          Note that class_weight is an attribute of the instantiated models and not of the classes of the models. The class LogisticRegression doesn't have class_weight, but a model of type LogisticRegression does. This is the basic Object-Oriented distiction between an instance and a class.
          You can check the difference practically with this code:



          from sklearn.linear_model import LogisticRegression

          logreg_class = LogisticRegression
          print(type(logreg_class))
          # >>> <class 'type'>

          logreg_model = LogisticRegression()
          print(type(logreg_model))
          # >>> <class 'sklearn.linear_model.logistic.LogisticRegression'>


          During the loop, class_ refers to the model class and class_() is a call to the constructor of that class, which returns an instance.






          share|improve this answer


























          • I did a mayor fix to my answer since it seems that regressors may have the attribute class_weight (although not-accesible) because of the inheritance structure of sklearn (DecisionTreeRegressor has class_weight attribute, for example, but it's not exposed in the constructor and makes no sense). Now it works as expected.

            – Julian Peller
            Nov 20 '18 at 15:32











          • Thanks, the parenthesis caught me out. What do they do?

            – Maartenk
            Nov 20 '18 at 19:37













          • class_ is a reference to the estimator class on each iteration. For example, DecisionTreeClassifier, LogisticRegression, etc. So doing class_() you are actually creating an object of that class: class_() translates to, for example, LogisticRegression(). You are creating an instance of a class. Assigning the result to a variable may help understanding it: 1. model = LogisticRegression(), 2. hasattr(model, 'class_weight'). The classes themselves don't have 'class_weight', but the instances do. I'll add some more data to the answer itself now.

            – Julian Peller
            Nov 20 '18 at 19:59











          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%2f53395542%2fwhat-sklearn-classifiers-come-with-class-weight-parameter%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You can get the classifiers (not all estimators) and check for class_weight attribute in the instantiated objects:



          from sklearn.utils.testing import all_estimators

          estimators = all_estimators(type_filter='classifier')
          for name, class_ in estimators:
          if hasattr(class_(), 'class_weight'): # Note the parenthesis: class_()
          print(name)


          Generates the list of the classifiers that can handle class imbalance:



          DecisionTreeClassifier
          ExtraTreeClassifier
          ExtraTreesClassifier
          LinearSVC
          LogisticRegression
          LogisticRegressionCV
          NuSVC
          PassiveAggressiveClassifier
          Perceptron
          RandomForestClassifier
          RidgeClassifier
          RidgeClassifierCV
          SGDClassifier
          SVC




          Note that class_weight is an attribute of the instantiated models and not of the classes of the models. The class LogisticRegression doesn't have class_weight, but a model of type LogisticRegression does. This is the basic Object-Oriented distiction between an instance and a class.
          You can check the difference practically with this code:



          from sklearn.linear_model import LogisticRegression

          logreg_class = LogisticRegression
          print(type(logreg_class))
          # >>> <class 'type'>

          logreg_model = LogisticRegression()
          print(type(logreg_model))
          # >>> <class 'sklearn.linear_model.logistic.LogisticRegression'>


          During the loop, class_ refers to the model class and class_() is a call to the constructor of that class, which returns an instance.






          share|improve this answer


























          • I did a mayor fix to my answer since it seems that regressors may have the attribute class_weight (although not-accesible) because of the inheritance structure of sklearn (DecisionTreeRegressor has class_weight attribute, for example, but it's not exposed in the constructor and makes no sense). Now it works as expected.

            – Julian Peller
            Nov 20 '18 at 15:32











          • Thanks, the parenthesis caught me out. What do they do?

            – Maartenk
            Nov 20 '18 at 19:37













          • class_ is a reference to the estimator class on each iteration. For example, DecisionTreeClassifier, LogisticRegression, etc. So doing class_() you are actually creating an object of that class: class_() translates to, for example, LogisticRegression(). You are creating an instance of a class. Assigning the result to a variable may help understanding it: 1. model = LogisticRegression(), 2. hasattr(model, 'class_weight'). The classes themselves don't have 'class_weight', but the instances do. I'll add some more data to the answer itself now.

            – Julian Peller
            Nov 20 '18 at 19:59
















          0














          You can get the classifiers (not all estimators) and check for class_weight attribute in the instantiated objects:



          from sklearn.utils.testing import all_estimators

          estimators = all_estimators(type_filter='classifier')
          for name, class_ in estimators:
          if hasattr(class_(), 'class_weight'): # Note the parenthesis: class_()
          print(name)


          Generates the list of the classifiers that can handle class imbalance:



          DecisionTreeClassifier
          ExtraTreeClassifier
          ExtraTreesClassifier
          LinearSVC
          LogisticRegression
          LogisticRegressionCV
          NuSVC
          PassiveAggressiveClassifier
          Perceptron
          RandomForestClassifier
          RidgeClassifier
          RidgeClassifierCV
          SGDClassifier
          SVC




          Note that class_weight is an attribute of the instantiated models and not of the classes of the models. The class LogisticRegression doesn't have class_weight, but a model of type LogisticRegression does. This is the basic Object-Oriented distiction between an instance and a class.
          You can check the difference practically with this code:



          from sklearn.linear_model import LogisticRegression

          logreg_class = LogisticRegression
          print(type(logreg_class))
          # >>> <class 'type'>

          logreg_model = LogisticRegression()
          print(type(logreg_model))
          # >>> <class 'sklearn.linear_model.logistic.LogisticRegression'>


          During the loop, class_ refers to the model class and class_() is a call to the constructor of that class, which returns an instance.






          share|improve this answer


























          • I did a mayor fix to my answer since it seems that regressors may have the attribute class_weight (although not-accesible) because of the inheritance structure of sklearn (DecisionTreeRegressor has class_weight attribute, for example, but it's not exposed in the constructor and makes no sense). Now it works as expected.

            – Julian Peller
            Nov 20 '18 at 15:32











          • Thanks, the parenthesis caught me out. What do they do?

            – Maartenk
            Nov 20 '18 at 19:37













          • class_ is a reference to the estimator class on each iteration. For example, DecisionTreeClassifier, LogisticRegression, etc. So doing class_() you are actually creating an object of that class: class_() translates to, for example, LogisticRegression(). You are creating an instance of a class. Assigning the result to a variable may help understanding it: 1. model = LogisticRegression(), 2. hasattr(model, 'class_weight'). The classes themselves don't have 'class_weight', but the instances do. I'll add some more data to the answer itself now.

            – Julian Peller
            Nov 20 '18 at 19:59














          0












          0








          0







          You can get the classifiers (not all estimators) and check for class_weight attribute in the instantiated objects:



          from sklearn.utils.testing import all_estimators

          estimators = all_estimators(type_filter='classifier')
          for name, class_ in estimators:
          if hasattr(class_(), 'class_weight'): # Note the parenthesis: class_()
          print(name)


          Generates the list of the classifiers that can handle class imbalance:



          DecisionTreeClassifier
          ExtraTreeClassifier
          ExtraTreesClassifier
          LinearSVC
          LogisticRegression
          LogisticRegressionCV
          NuSVC
          PassiveAggressiveClassifier
          Perceptron
          RandomForestClassifier
          RidgeClassifier
          RidgeClassifierCV
          SGDClassifier
          SVC




          Note that class_weight is an attribute of the instantiated models and not of the classes of the models. The class LogisticRegression doesn't have class_weight, but a model of type LogisticRegression does. This is the basic Object-Oriented distiction between an instance and a class.
          You can check the difference practically with this code:



          from sklearn.linear_model import LogisticRegression

          logreg_class = LogisticRegression
          print(type(logreg_class))
          # >>> <class 'type'>

          logreg_model = LogisticRegression()
          print(type(logreg_model))
          # >>> <class 'sklearn.linear_model.logistic.LogisticRegression'>


          During the loop, class_ refers to the model class and class_() is a call to the constructor of that class, which returns an instance.






          share|improve this answer















          You can get the classifiers (not all estimators) and check for class_weight attribute in the instantiated objects:



          from sklearn.utils.testing import all_estimators

          estimators = all_estimators(type_filter='classifier')
          for name, class_ in estimators:
          if hasattr(class_(), 'class_weight'): # Note the parenthesis: class_()
          print(name)


          Generates the list of the classifiers that can handle class imbalance:



          DecisionTreeClassifier
          ExtraTreeClassifier
          ExtraTreesClassifier
          LinearSVC
          LogisticRegression
          LogisticRegressionCV
          NuSVC
          PassiveAggressiveClassifier
          Perceptron
          RandomForestClassifier
          RidgeClassifier
          RidgeClassifierCV
          SGDClassifier
          SVC




          Note that class_weight is an attribute of the instantiated models and not of the classes of the models. The class LogisticRegression doesn't have class_weight, but a model of type LogisticRegression does. This is the basic Object-Oriented distiction between an instance and a class.
          You can check the difference practically with this code:



          from sklearn.linear_model import LogisticRegression

          logreg_class = LogisticRegression
          print(type(logreg_class))
          # >>> <class 'type'>

          logreg_model = LogisticRegression()
          print(type(logreg_model))
          # >>> <class 'sklearn.linear_model.logistic.LogisticRegression'>


          During the loop, class_ refers to the model class and class_() is a call to the constructor of that class, which returns an instance.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 '18 at 20:08

























          answered Nov 20 '18 at 15:11









          Julian PellerJulian Peller

          8941511




          8941511













          • I did a mayor fix to my answer since it seems that regressors may have the attribute class_weight (although not-accesible) because of the inheritance structure of sklearn (DecisionTreeRegressor has class_weight attribute, for example, but it's not exposed in the constructor and makes no sense). Now it works as expected.

            – Julian Peller
            Nov 20 '18 at 15:32











          • Thanks, the parenthesis caught me out. What do they do?

            – Maartenk
            Nov 20 '18 at 19:37













          • class_ is a reference to the estimator class on each iteration. For example, DecisionTreeClassifier, LogisticRegression, etc. So doing class_() you are actually creating an object of that class: class_() translates to, for example, LogisticRegression(). You are creating an instance of a class. Assigning the result to a variable may help understanding it: 1. model = LogisticRegression(), 2. hasattr(model, 'class_weight'). The classes themselves don't have 'class_weight', but the instances do. I'll add some more data to the answer itself now.

            – Julian Peller
            Nov 20 '18 at 19:59



















          • I did a mayor fix to my answer since it seems that regressors may have the attribute class_weight (although not-accesible) because of the inheritance structure of sklearn (DecisionTreeRegressor has class_weight attribute, for example, but it's not exposed in the constructor and makes no sense). Now it works as expected.

            – Julian Peller
            Nov 20 '18 at 15:32











          • Thanks, the parenthesis caught me out. What do they do?

            – Maartenk
            Nov 20 '18 at 19:37













          • class_ is a reference to the estimator class on each iteration. For example, DecisionTreeClassifier, LogisticRegression, etc. So doing class_() you are actually creating an object of that class: class_() translates to, for example, LogisticRegression(). You are creating an instance of a class. Assigning the result to a variable may help understanding it: 1. model = LogisticRegression(), 2. hasattr(model, 'class_weight'). The classes themselves don't have 'class_weight', but the instances do. I'll add some more data to the answer itself now.

            – Julian Peller
            Nov 20 '18 at 19:59

















          I did a mayor fix to my answer since it seems that regressors may have the attribute class_weight (although not-accesible) because of the inheritance structure of sklearn (DecisionTreeRegressor has class_weight attribute, for example, but it's not exposed in the constructor and makes no sense). Now it works as expected.

          – Julian Peller
          Nov 20 '18 at 15:32





          I did a mayor fix to my answer since it seems that regressors may have the attribute class_weight (although not-accesible) because of the inheritance structure of sklearn (DecisionTreeRegressor has class_weight attribute, for example, but it's not exposed in the constructor and makes no sense). Now it works as expected.

          – Julian Peller
          Nov 20 '18 at 15:32













          Thanks, the parenthesis caught me out. What do they do?

          – Maartenk
          Nov 20 '18 at 19:37







          Thanks, the parenthesis caught me out. What do they do?

          – Maartenk
          Nov 20 '18 at 19:37















          class_ is a reference to the estimator class on each iteration. For example, DecisionTreeClassifier, LogisticRegression, etc. So doing class_() you are actually creating an object of that class: class_() translates to, for example, LogisticRegression(). You are creating an instance of a class. Assigning the result to a variable may help understanding it: 1. model = LogisticRegression(), 2. hasattr(model, 'class_weight'). The classes themselves don't have 'class_weight', but the instances do. I'll add some more data to the answer itself now.

          – Julian Peller
          Nov 20 '18 at 19:59





          class_ is a reference to the estimator class on each iteration. For example, DecisionTreeClassifier, LogisticRegression, etc. So doing class_() you are actually creating an object of that class: class_() translates to, for example, LogisticRegression(). You are creating an instance of a class. Assigning the result to a variable may help understanding it: 1. model = LogisticRegression(), 2. hasattr(model, 'class_weight'). The classes themselves don't have 'class_weight', but the instances do. I'll add some more data to the answer itself now.

          – Julian Peller
          Nov 20 '18 at 19:59


















          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%2f53395542%2fwhat-sklearn-classifiers-come-with-class-weight-parameter%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

          Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

          ts Property 'filter' does not exist on type '{}'

          mat-slide-toggle shouldn't change it's state when I click cancel in confirmation window