What SKLearn classifiers come with class_weight parameter
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
add a comment |
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
add a comment |
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
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
python-3.x scikit-learn
asked Nov 20 '18 at 14:46
MaartenkMaartenk
32
32
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
I did a mayor fix to my answer since it seems that regressors may have the attributeclass_weight
(although not-accesible) because of the inheritance structure of sklearn (DecisionTreeRegressor
hasclass_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 doingclass_()
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
I did a mayor fix to my answer since it seems that regressors may have the attributeclass_weight
(although not-accesible) because of the inheritance structure of sklearn (DecisionTreeRegressor
hasclass_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 doingclass_()
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
add a comment |
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.
I did a mayor fix to my answer since it seems that regressors may have the attributeclass_weight
(although not-accesible) because of the inheritance structure of sklearn (DecisionTreeRegressor
hasclass_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 doingclass_()
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
add a comment |
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.
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.
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 attributeclass_weight
(although not-accesible) because of the inheritance structure of sklearn (DecisionTreeRegressor
hasclass_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 doingclass_()
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
add a comment |
I did a mayor fix to my answer since it seems that regressors may have the attributeclass_weight
(although not-accesible) because of the inheritance structure of sklearn (DecisionTreeRegressor
hasclass_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 doingclass_()
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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