Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 属性错误:';str';对象没有属性';解码';Logistic回归模型的拟合_Python_Scikit Learn_Logistic Regression - Fatal编程技术网

Python 属性错误:';str';对象没有属性';解码';Logistic回归模型的拟合

Python 属性错误:';str';对象没有属性';解码';Logistic回归模型的拟合,python,scikit-learn,logistic-regression,Python,Scikit Learn,Logistic Regression,我目前正在尝试使用逻辑回归创建二元分类。目前我正在确定功能的重要性。我已经做了数据预处理(一次热编码和采样),并使用XGBoost和RandomFOrestClassifier运行了它,没有问题 然而,当我尝试拟合逻辑回归模型时(下面是我在笔记本中的代码) 它给出了一个错误 ... ~\AppData\Local\Continuum\anaconda3\lib\site-packages\joblib\parallel.py in <listcomp>(.0) 223

我目前正在尝试使用逻辑回归创建二元分类。目前我正在确定功能的重要性。我已经做了数据预处理(一次热编码和采样),并使用XGBoost和RandomFOrestClassifier运行了它,没有问题

然而,当我尝试拟合逻辑回归模型时(下面是我在笔记本中的代码)

它给出了一个错误

...
~\AppData\Local\Continuum\anaconda3\lib\site-packages\joblib\parallel.py in <listcomp>(.0)
    223         with parallel_backend(self._backend, n_jobs=self._n_jobs):
    224             return [func(*args, **kwargs)
--> 225                     for func, args, kwargs in self.items]
    226 
    227     def __len__(self):

~\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\linear_model\_logistic.py in _logistic_regression_path(X, y, pos_class, Cs, fit_intercept, max_iter, tol, verbose, solver, coef, class_weight, dual, penalty, intercept_scaling, multi_class, random_state, check_input, max_squared_sum, sample_weight, l1_ratio)
    762             n_iter_i = _check_optimize_result(
    763                 solver, opt_res, max_iter,
--> 764                 extra_warning_msg=_LOGISTIC_SOLVER_CONVERGENCE_MSG)
    765             w0, loss = opt_res.x, opt_res.fun
    766         elif solver == 'newton-cg':

~\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\utils\optimize.py in _check_optimize_result(solver, result, max_iter, extra_warning_msg)
    241                 "    https://scikit-learn.org/stable/modules/"
    242                 "preprocessing.html"
--> 243             ).format(solver, result.status, result.message.decode("latin1"))
    244             if extra_warning_msg is not None:
    245                 warning_msg += "\n" + extra_warning_msg

AttributeError: 'str' object has no attribute 'decode'    
。。。
~\AppData\Local\Continuum\anaconda3\lib\site packages\joblib\parallel.py in(.0)
223具有并行_后端(self._后端,n_作业=self._n_作业):
224返回[func(*args,**kwargs)
-->225用于自身项目中的func、ARG、kwargs]
226
227定义长度(自):
~\AppData\Local\Continuum\anaconda3\lib\site packages\sklearn\linear\u model\u logistic.py in\u logistic\u regression\u path(X,y,pos\u class,Cs,fit\u intercept,max\u iter,tol,verbose,solver,coef,class\u weight,dual,惩罚,intercept\u scaling,multi\u class,random\u state,check\u输入,max\u squared\u sum,sample\u weight,l1\u ratio)
762 n\u iter\u i=\u检查\u优化\u结果(
763解算器,可选分辨率,最大分辨率,
-->764额外警告消息=\u逻辑消息\u解算器\u收敛消息)
765 w0,损耗=opt_res.x,opt_res.fun
766 elif解算器==“牛顿cg”:
~\AppData\Local\Continuum\anaconda3\lib\site packages\sklearn\utils\optimize.py in\u check\u optimize\u result(解算器、结果、最大值、额外警告消息)
241                 "    https://scikit-learn.org/stable/modules/"
242“preprocessing.html”
-->格式(解算器,result.status,result.message.decode(“拉丁1”))
244如果额外警告消息不是无:
245警告信息+=“\n”+额外警告信息
AttributeError:“str”对象没有属性“decode”

我用谷歌搜索了一下,大多数回复都说这个错误是因为scikit学习库试图解码一个已经解码的字符串。但我不知道如何解决我的问题。我确保我的所有数据都是整数或float64,并且没有字符串。

在最新版本的scikit learn(现在是0.24.1)中,将部分代码包含在try-catch块中的问题已得到修复,我在下面报告:该文件是

optimize.py -> _check_optimize_result(solver, result, max_iter=None,
                       extra_warning_msg=None)
代码段是

if solver == "lbfgs":
    if result.status != 0:
        try:
            # The message is already decoded in scipy>=1.6.0
            result_message = result.message.decode("latin1")
        except AttributeError:
            result_message = result.message
            warning_msg = (
                "{} failed to converge (status={}):\n{}.\n\n"
                "Increase the number of iterations (max_iter) "
                "or scale the data as shown in:\n"
                "    https://scikit-learn.org/stable/modules/"
                "preprocessing.html"
            ).format(solver, result.status, result_message)
那只是

if solver == "lbfgs":
    if result.status != 0:
        warning_msg = (
            "{} failed to converge (status={}):\n{}.\n\n"
            "Increase the number of iterations (max_iter) "
            "or scale the data as shown in:\n"
            "    https://scikit-learn.org/stable/modules/"
            "preprocessing.html"
        ).format(solver, result.status, result.message.decode("latin1"))
以前。
因此,升级scikit learn解决了问题。

我尝试使用下面的命令升级我的
scikit learn
,但仍然没有解决
AttributeError:“str”对象没有属性“decode”
问题

pip install scikit-learn  -U
最后,在解决问题的代码片段下面,将解算器添加为
liblinear

model = LogisticRegression(solver='liblinear')

solver='lbfgs'有一个错误。
更改为“sag”可以解决这个问题。

你能显示你的数据吗?我在ubuntu 18 WSL上运行scikit learn='0.23.2'时遇到同样的错误,将解算器更改为liblinear,错误消失了。升级scikit learn会引发另一个问题。GridSearchCV函数无法正常工作。即使我将verbose设置为10,也不会显示verbose消息。加上它需要很长的时间,就像一个模型应该在2分钟内完成运行,大约需要20分钟。这是最新版本中出现的一个主要错误。
model = LogisticRegression(solver='liblinear')