[英]Manually build logistic regression model for prediction in Sklearn
I wonder how to build a LogisticRegression model "m" manually by setting explicit the values for m.coef_ and m.intercept_. 我想知道如何通过显式设置m.coef_和m.intercept_的值来手动构建LogisticRegression模型“ m”。 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 fe
这听起来很怪异,但在某些情况下,我尝试对所有情况均为负(0)的数据进行分类,并且模型的拟合给出错误,因此我想设置
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. m.predict(x)可以按预期工作,但是m.predict_prob(x)给出了错误。
*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. 我如何才能避免这种错误,或更妙的是,如何说服m.fit(X,y)也只对数据中的一个类起作用。
best regards 最好的祝福
You are setting your coefficients and intercept as integers. 您正在设置系数并以整数形式截距。 I set them as floats and the
predict_proba()
is working: 我将它们设置为浮点型,
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]])
Note the .
注意
.
after the zeros. 在零之后。
In [12]: m.predict_proba(x)
Out[12]:
array([[1., 0.],
[1., 0.]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.