问题描述
当我使用逻辑回归拟合模型时,向我显示了诸如ValueError的值错误:惩罚项必须为正.
When I'm fit my model using logistic regression showing me a value error like ValueError: Penalty term must be positive.
C=[1e-4, 1e-3, 1e-2, 1e-1, 1e0, 1e1, 1e2, 1e3, 1e4]
for i in C[-9:]:
logisticl2 = LogisticRegression(penalty='l2',C=C)
logisticl2.fit(X_train,Y_train)
probs = logisticl2.predict_proba(X_test)
得到错误:
推荐答案
更仔细地看,您会发现您正在运行一个循环,其中代码没有任何变化-它始终是 C = C
,与您的 i
的当前值无关.而且您会得到预期的错误,因为 C
必须是浮点数,而不是列表(文档).
Looking more closely, you'll realize that you are running a loop in which nothing changes in your code - it is always C=C
, irrespectively of the current value of your i
. And you get an expected error, since C
must be a float, and not a list (docs).
如果我怀疑您要为 C
列表中的所有值运行逻辑回归分类器,则应按照以下方法修改代码:
If, as I suspect, you are trying to run your logistic regression classifier for all the values in your C
list, here is how you should modify your code:
C=[1e-4, 1e-3, 1e-2, 1e-1, 1e0, 1e1, 1e2, 1e3, 1e4]
for i in C: # 1st change
logisticl2 = LogisticRegression(penalty='l2',C=i) # 2nd change
logisticl2.fit(X_train,Y_train)
probs = logisticl2.predict_proba(X_test)
这篇关于ValueError:惩罚期限必须为正的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!