Skip to content

linear_model giving AttributeError: 'numpy.float64' object has no attribute 'exp' #3142

Closed
@vm-wylbur

Description

@vm-wylbur

sklearn 0.14.1, and it happens only with a particular dataset I'm using, thus I'm not sure how to provide reproducible data, sorry.

Per the stacktrace, I've tracked the problem to the lines in linear_model/base.py. I think that LinearClassifierMixin.decision_function is returning an array of dtype=object which makes np.exp() fail. None of the values in the array look to my eye like anything other than floats. Casting the array explicitly as a float (as the commented line shows) allows predict_proba to exponentiate.

There might be something happening in decision_function such it returns a non-float result, but I can't spot it. thanks -- PB.

from sklearn.linear_model import LogisticRegression
clf_lr = LogisticRegression(penalty='l1')
full = clf_lr.fit(X, Y)
probs = full.predict_proba(X)

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-72-2db71751c630> in <module>()
      2 clf_lr = LogisticRegression(penalty='l1')
      3 full = clf_lr.fit(X, Y)
----> 4 probs = full.predict_proba(X)

/Users/pball/miniconda3/lib/python3.3/site-packages/sklearn/linear_model/logistic.py in predict_proba(self, X)
    120             where classes are ordered as they are in ``self.classes_``.
    121         """
--> 122         return self._predict_proba_lr(X)
    123 
    124     def predict_log_proba(self, X):

/Users/pball/miniconda3/lib/python3.3/site-packages/sklearn/linear_model/base.py in _predict_proba_lr(self, X)
    237         prob = self.decision_function(X)
    238         # PB hack
--> 239         # prob = prob.astype(float)
    240         prob *= -1
    241         np.exp(prob, prob)

AttributeError: 'numpy.float64' object has no attribute 'exp'

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions