Manually build logistic regression model for prediction in Sklearn

时间:2018-03-09 19:23:44

标签: python scikit-learn logistic-regression

I wonder how to build a LogisticRegression model "m" manually by setting explicit the values for m.coef_ and m.intercept_. This sounds weird but in some cases I try to classify data where all cases are negativ (0) and the fit of the model gives an error so I want to set f.e.

m = LogisticRegression()
m.coef_=np.array([[0,0]]) and
m.intercept_=-1000
m.classes_=np.array([0, 1])
x = np.array([[1, 1],[2, 2]])

m.predict(x) works well as expected, but m.predict_prob(x) gives an error.


*TypeE
rror                                 Traceback (most recent call last)
<ipython-input-78-e122e3e7c447> in <module>()
----> 1 p.predict_proba(x)
~/anaconda3/lib/python3.6/site-packages/sklearn/linear_model/logistic.py in predict_proba(self, X)
   1334         calculate_ovr = self.coef_.shape[0] == 1 or self.multi_class == "ovr"
   1335         if calculate_ovr:
-> 1336             return super(LogisticRegression, self)._predict_proba_lr(X)
   1337         else:
   1338             return softmax(self.decision_function(X), copy=False)
~/anaconda3/lib/python3.6/site-packages/sklearn/linear_model/base.py in _predict_proba_lr(self, X)
    338         prob = self.decision_function(X)
    339         prob *= -1
--> 340         np.exp(prob, prob)
    341         prob += 1
    342         np.reciprocal(prob, prob)
TypeError: ufunc 'exp' output (typecode 'd') could not be coerced to provided output parameter (typecode 'l') according to the casting rule ''same_kind''*

How can I avoid this error, or even better, how persuade the m.fit(X,y) to work also with only one class in the data.

best regards

1 个答案:

答案 0 :(得分:0)

您将系数和截距设置为整数。我将它们设置为浮点数,predict_proba()正在运行:

m = LogisticRegression()
m.coef_= np.array([[0.,0.]]) and
m.intercept_ = -1000.
m.classes_=np.array([0, 1])
x = np.array([[1, 1],[2, 2]])

请注意零后的.

In [12]: m.predict_proba(x)
Out[12]:
array([[1., 0.],
       [1., 0.]])