Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'LogisticRegression' object has no attribute 'classes_' #11444

Closed
Yuchen-Peng opened this issue Jul 5, 2018 · 11 comments
Closed
Labels
Documentation Easy Well-defined and straightforward way to resolve help wanted

Comments

@Yuchen-Peng
Copy link

Yuchen-Peng commented Jul 5, 2018

Description

I'm creating a logistic regression Python model from existing parameters for production. This is done by creating a LogisticRegression object and manually specifying the model coefficients. When I try to use this model object and the predict() method to predict an np matrix, I got the error message

AttributeError: 'LogisticRegression' object has no attribute 'classes_'

On the other hand, the predict_proba() works fine.

Steps/Code to Reproduce

Example:

from sklearn.linear_model import LogisticRegression
import numpy as np
import pickle
import pandas as pd

# initialize model coefficience and intercepts
logreg = LogisticRegression()
# also tried import sklearn.linear_model as lm, logreg = lm.LogisticRegression(), same error
logreg.intercept_ = 1
logreg.coef_ = np.array([0.5,2]).reshape((1,-1))
X = np.array([[1,1],[2,2]])
logreg.predict(X)

Expected Results

It should return the label predicted.

Actual Results

AttributeError: 'LogisticRegression' object has no attribute 'classes_' 

Versions

Darwin-17.6.0-x86_64-i386-64bit
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 12:04:33)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]
NumPy 1.14.0
SciPy 1.0.0
Scikit-Learn 0.19.1

@rth
Copy link
Member

rth commented Jul 5, 2018

This is done by creating a LogisticRegression object and manually specifying the model coefficients

The usual way would be fit the estimator, serialize it (e.g. with joblib pickle) then unserialize and predict. Monkeypatching of estimator attributes as you are doing is not officially supported. You can do it, but it's up to you to read the code and set the needed attributes (all of which will not be public). In this case the classes_ attributes would also need to be set.

Though, the documentation of the classes_ attribute is indeed missing from LogisticRegression and possibly other estimators inheriting from LinearClassifierMixin.

@rth rth added Easy Well-defined and straightforward way to resolve Documentation help wanted labels Jul 5, 2018
@Yuchen-Peng
Copy link
Author

Yuchen-Peng commented Jul 5, 2018

The usual way would be fit the estimator, serialize it (e.g. with joblib pickle) then unserialize and predict.

The model was built in the past using other programming language. We are simply implementing the model in production, so it doesn't make sense to retrieve the build dataset.

In this case the classes_ attributes would also need to be set.

The 'LogisticRegression' object has no attribute 'classes_', and I don't know where it comes from. But indeed this is solved by

logreg.classes_ = np.array([-1, 1])

@Olamyy
Copy link

Olamyy commented Jul 5, 2018

The LogisticRegression is one of sklearn's estimators. It's important to remember this.

Estimators after learning by calling their fit method, expose some of their learned parameters as class attributes with trailing underscores after them. A good example is the Imputer's statistics_ parameter.

Back to LogisticRegression, one of it's learned parameters is the class_ attribute which would not be available until you've called the fit method.
So, you're going to have to look for a way to call fit on the logreg instance before accessing class.

@Yuchen-Peng
Copy link
Author

@Olamyy this is not necessarily true. When the fit method is called, the classes_ attribute is learned from the actual target (together with intercept_ and coef_). But manually specifying the classes_ attribute (as I did for intercept_ and coef_) would work.

I'm with @rth that this is more of a document issue. The classes_ attribute is not mentioned in the Sklearn LogisticRegression documentation. I'm feeling comfortable to close this issue if we can have a short description of classes_ attribute in the documentation.

@jnothman
Copy link
Member

jnothman commented Jul 6, 2018

@agramfort
Copy link
Member

agramfort commented Jul 6, 2018 via email

@rth
Copy link
Member

rth commented Jul 6, 2018

+1 and at the same time check than all estimators that inherit from
ClassifierMixin do the same.

It could be worth enforcing this in sklearn/tests/test_docstring_parameters.py.

@blooraspberry
Copy link

working on this.

@Matt-arch
Copy link

hello can someone tell me about what this do "self.coef_.T" i read it used to change the coefficient of X[i] into sparse matrix but i can't get clear image, please help me out here

@judithabk6
Copy link
Contributor

working on this

@lesteve
Copy link
Member

lesteve commented Jan 29, 2020

Closing since LogisticRegression classes_ attribute is now documented (was done in #12795)

Thanks @judithabk6 for helping to clean our issue tracker!

@lesteve lesteve closed this as completed Jan 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Easy Well-defined and straightforward way to resolve help wanted
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants