Skip to content

SGDClassifier: referencing 'predict_proba' method in unfitted object throws error #10938

Closed

Description

@david-cortes

If I initialize an SGDClassifier object and try to access its 'predict_proba' method (not calling it, just referencing it), it throws NotFittedError

E.g.

from sklearn.linear_model import SGDClassifier

classifier = SGDClassifier()
classifier.predict_proba
# no parentheses on function

throws:

NotFittedError                            Traceback (most recent call last)
<ipython-input-6-02b9a01573f0> in <module>()
----> 1 a.predict_proba

~/anaconda3/lib/python3.6/site-packages/sklearn/linear_model/stochastic_gradient.py in predict_proba(self)
    832         http://jmlr.csail.mit.edu/papers/volume2/zhang02c/zhang02c.pdf
    833         """
--> 834         self._check_proba()
    835         return self._predict_proba
    836 

~/anaconda3/lib/python3.6/site-packages/sklearn/linear_model/stochastic_gradient.py in _check_proba(self)
    790 
    791     def _check_proba(self):
--> 792         check_is_fitted(self, "t_")
    793 
    794         if self.loss not in ("log", "modified_huber"):

~/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py in check_is_fitted(estimator, attributes, msg, all_or_any)
    766 
    767     if not all_or_any([hasattr(estimator, attr) for attr in attributes]):
--> 768         raise NotFittedError(msg % {'name': type(estimator).__name__})
    769 
    770 

NotFittedError: This SGDClassifier instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.

However, the method is not being used, just referenced, which shouldn't cause an error.

Activity

  1. changed the title [-]SGDClassifier: accessing 'predict_proba' function in unfitted object throws error[/-] [+]SGDClassifier: referencing 'predict_proba' method in unfitted object throws error[/+] on Apr 8, 2018
  2. jnothman commented on Apr 10, 2018

    @jnothman
    Member

    Hmm... Yes, I would accept not checking if fitted here and rely on the current loss parameter to determine probability support

  3. david-cortes commented on Apr 10, 2018

    @david-cortes
    ContributorAuthor

    Great! Although my issue here was not really about not being able to output probabilities without fitting first, but more about not being able to reference the method itself - for example, it also fails if I try to assign it without calling it:

    from sklearn.linear_model import SGDClassifier
    
    classifier = SGDClassifier()
    classifier.predict_proba_copy = classifier.predict_proba
  4. jnothman commented on Apr 10, 2018

    @jnothman
    Member
  5. aniruddhadave commented on Apr 11, 2018

    @aniruddhadave
    Contributor

    Hi, I can take this up and remove not fitted check. Seems like a good first issue.

  6. jnothman commented on Apr 11, 2018

    @jnothman
    Member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions