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.
python-3.x scikit-learn
add a comment |
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.
python-3.x scikit-learn
add a comment |
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.
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 11 hours ago
Maartenk
32
32
add a comment |
add a comment |
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.
New contributor
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
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 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
6 hours ago
add a comment |
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.
New contributor
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
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 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
6 hours ago
add a comment |
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.
New contributor
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
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 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
6 hours ago
add a comment |
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.
New contributor
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.
New contributor
edited 5 hours ago
New contributor
answered 10 hours ago
Julian Peller
35618
35618
New contributor
New contributor
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
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 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
6 hours ago
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
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 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
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
add a comment |
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