What SKLearn classifiers come with class_weight parameter











up vote
0
down vote

favorite












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


























    up vote
    0
    down vote

    favorite












    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
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      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 11 hours ago









      Maartenk

      32




      32
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          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










          New contributor




          Julian Peller is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • 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
            10 hours ago










          • Thanks, the parenthesis caught me out. What do they do?
            – Maartenk
            6 hours ago












          • 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
            6 hours ago











          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',
          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








          up vote
          0
          down vote



          accepted










          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










          New contributor




          Julian Peller is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • 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
            10 hours ago










          • Thanks, the parenthesis caught me out. What do they do?
            – Maartenk
            6 hours ago












          • 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
            6 hours ago















          up vote
          0
          down vote



          accepted










          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










          New contributor




          Julian Peller is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • 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
            10 hours ago










          • Thanks, the parenthesis caught me out. What do they do?
            – Maartenk
            6 hours ago












          • 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
            6 hours ago













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          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










          New contributor




          Julian Peller is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          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










          New contributor




          Julian Peller is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          share|improve this answer



          share|improve this answer








          edited 5 hours ago





















          New contributor




          Julian Peller is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          answered 10 hours ago









          Julian Peller

          35618




          35618




          New contributor




          Julian Peller is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.





          New contributor





          Julian Peller is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          Julian Peller is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.












          • 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
            10 hours ago










          • Thanks, the parenthesis caught me out. What do they do?
            – Maartenk
            6 hours ago












          • 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
            6 hours ago


















          • 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
            10 hours ago










          • Thanks, the parenthesis caught me out. What do they do?
            – Maartenk
            6 hours ago












          • 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
            6 hours ago
















          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
          10 hours ago




          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
          10 hours ago












          Thanks, the parenthesis caught me out. What do they do?
          – Maartenk
          6 hours ago






          Thanks, the parenthesis caught me out. What do they do?
          – Maartenk
          6 hours ago














          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
          6 hours ago




          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
          6 hours ago


















           

          draft saved


          draft discarded



















































           


          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

          Berounka

          Sphinx de Gizeh

          Different font size/position of beamer's navigation symbols template's content depending on regular/plain...