ValueError: Penalty term must be positive

一笑奈何 提交于 2021-01-29 07:22:18

问题


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)

getting error:

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])


回答1:


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).

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)


来源:https://stackoverflow.com/questions/54423560/valueerror-penalty-term-must-be-positive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!