目录
问题描述
D:\PyCharm\machine_learning_py_codes\venv\lib\site-packages\sklearn\linear_model_logistic.py:762: ConvergenceWarning: lbfgs failed to converge (status=1): STOP: TOTAL NO. of ITERATIONS REACHED
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)
一直重复的打印这些错误信息。
原理
其实是因为迭代的时候迭代总数超过了限制导致程序函数无法收敛。下面是我的程序的一部分代码:
from sklearn.feature_selection import RFECV
from sklearn.svm import SVR
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import RandomForestClassifier
estimator = AdaBoostClassifier(random_state=0)
selector = RFECV(estimator, step=1, cv=5)
selector = selector.fit(features_raw, popular_label)
selector.ranking_
estimator_LR = LogisticRegression(random_state=0)
selector_LR = RFECV(estimator_LR, step=1, cv=5)
selector_LR = selector_LR.fit(features_raw, popular_label)
selector_LR.ranking_
estimator_RF = RandomForestClassifier(random_state=0)
selector_RF = RFECV(estimator_RF, step=1, cv=5)
selector_RF = selector_RF.fit(features_raw, popular_label)
selector_RF.ranking_
其中有这么一行。
estimator_LR = LogisticRegression(random_state=0)
对于LogisticRegression函数,他有一个参数max_iter,int类型,默认值100,表示这个函数达到收敛的最大迭代次数。
这个函数的其他内容可以参考官方文档。
问题解决
通过设置最大迭代次数来避免报错。可以将这一行代码改为,其他的代码不变。
estimator_LR = LogisticRegression(random_state=0,max_iter=100)