python - 这是什么警告:ConvergenceWarning:lbfgs 无法收敛(状态=1)
问题描述
我该如何处理这个警告?
Warning (from warnings module):
File "C:\Users\SAMSUNG\AppData\Local\Programs\Python\Python37\lib\site-packages\sklearn\linear_model\_logistic.py", line 762
extra_warning_msg=_LOGISTIC_SOLVER_CONVERGENCE_MSG)
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
urls_data = pd.read_csv("data.csv")
TEST_SIZE = 0.001
type(urls_data)
urls_data.head()
def makeTokens(f):
tkns_BySlash = str(f.encode('utf-8')).split('/')
total_Tokens = []
for i in tkns_BySlash :
tokens = str(i).split('-')
tkns_ByDot = []
for j in range(0, len(tokens)):
temp_Tokens = str(tokens[j]).split('.')
tkns_ByDot = tkns_ByDot + temp_Tokens
total_Tokens = total_Tokens + tokens + tkns_ByDot
total_Tokens = list(set(total_Tokens))
if 'com' in total_Tokens:
total_Tokens.remove('com')
return total_Tokens
y = urls_data["label"]
url_list = urls_data["url"]
#Data Preprocessing
vectorizer = TfidfVectorizer(tokenizer=makeTokens)
X = vectorizer.fit_transform(url_list)
#Split Train set and Test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = TEST_SIZE, random_state = 42)
###############################logit
logit = LogisticRegression()
logit.fit(X_train, y_train)
这是我的代码
解决方案
改变
logit = LogisticRegression()
至
logit = LogisticRegression(max_iter=10000)
然后再试一次。
(默认参数max_iter
等于LogisticRegression()
1000,所以任何大于 1000 的数字都可以,不一定是 10000)
您也可以按照警告所说的那样标准化您的数据,使用sklearn.preprocessing.scale()
.
推荐阅读
- c++ - 布局位置或统一变量中的opengl差异设置值
- ruby-on-rails - Sunspot Solr 全文搜索时间分数提升
- merge - 数据流如何组合 PCollection 替换对象
- python - Tensorflow 教程 + 如何从生成器对象中提取类和概率
- php - PHP / Doctrine - 在 for() 循环外使用 flush() 保存日期时间
- r - 绘制多条线的平均线
- c++ - 如何从 Lua 函数中获取多个返回的表?
- vba - VBA 宏需要从活动工作簿更改为特定工作簿/工作表
- ios - 使用 HTTP POST 快速发送数组和图像
- javascript - Angular Material Datepicker openStream 和 closeStream