【问题标题】:I'm getting warning while traning the data using logistic regression algorithm我在使用逻辑回归算法转换数据时收到警告
【发布时间】:2020-09-25 06:32:44
【问题描述】:

我正在训练一个数字数据集,以使用逻辑回归多类分类对数字进行分类。通过使用此代码

from sklearn.datasets import load_digits
%matplotlib inline
import matplotlib.pyplot as plt
digits = load_digits()

plt.gray() 
for i in range(5):
    plt.matshow(digits.images[i])

from sklearn.linear_model import LogisticRegression
model = LogisticRegression()

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(digits.data,digits.target, test_size=0.2)

model.fit(X_train, y_train) 

当我执行单元格时,我正在 jupyter notebook 中执行此操作

   model.fit(X_train, y_train)

我收到类似警告

C:\Users\Shubham Teke\anaconda3\envs\allenv\lib\site-packages\sklearn\linear_model\_logistic.py:764: ConvergenceWarning: lbfgs failed to converge (status=1):
STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.

Increase the number of iterations (max_iter) or scale the data as shown in:
    https://scikit-learn.org/stable/modules/preprocessing.html
Please also refer to the documentation for alternative solver options:
    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression
  extra_warning_msg=_LOGISTIC_SOLVER_CONVERGENCE_MSG)
LogisticRegression()

那么这个警告是什么以及如何解决这个问题?

【问题讨论】:

    标签: python data-science logistic-regression


    【解决方案1】:

    这是因为模型训练被max_iter停止了。 也就是模型的参数没有收敛,而是迭代变成了max_iter

    解决方案很简单。增加你的max_iter

    例如,

    model = LogisticRegression(max_iter = 100000) 
    

    【讨论】:

      【解决方案2】:

      这是警告,而不是错误。您需要增加最大迭代次数。

      默认值为 100。试试这个

      LogisticRegression(max_iter=1000)
      

      或增加1000以上

      【讨论】:

        猜你喜欢
        • 2021-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-17
        • 2021-12-07
        • 1970-01-01
        • 2014-04-02
        • 2020-08-31
        相关资源
        最近更新 更多