当我使用逻辑回归拟合模型时,会显示一个值错误,例如 ValueError:惩罚项必须为正。
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)
出现错误:
ValueError: Penalty term must be positive; got (C=[0.0001, 0.001, 0.01, 0.1, 1.0, 10.0, 100.0, 1000.0, 10000.0])
最佳答案
仔细观察,您会发现您正在运行一个循环,其中代码没有任何变化 - 它始终是 C=C
,无论您的 i 的当前值如何
。您会收到预期的错误,因为 C
必须是 float ,而不是列表 ( docs )。
如果,正如我怀疑的那样,您正在尝试对 C
列表中的所有值运行逻辑回归分类器,那么您应该如何修改代码:
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)
关于python - ValueError:惩罚项必须为正,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54423560/