Search code examples
pythonmachine-learningscikit-learnlogistic-regressionvalueerror

ValueError: Penalty term must be positive


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


Solution

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