AttributeError: 'str' 对象在拟合逻辑回归模型时没有属性 'decode' [英] AttributeError: 'str' object has no attribute 'decode' in fitting Logistic Regression Model

查看:12
本文介绍了AttributeError: 'str' 对象在拟合逻辑回归模型时没有属性 'decode'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am currently trying to create a binary classification using Logistic regression. Currently I am in determining the feature importance. I already did the data preprocessing (One Hot Encoding and sampling) and ran it with XGBoost and RandomFOrestClassifier, no problem

但是,当我尝试拟合 LogisticRegression 模型时(以下是我在 Notebook 中的代码),

However, when I tried to fit a LogisticRegression model (below is my code in Notebook),

from sklearn.linear_model import LogisticRegression

#Logistic Regression
# fit the model
model = LogisticRegression()
# fit the model
model.fit(np.array(X_over), np.array(y_over))
# get importance
importance = model.coef_[0]
# summarize feature importance
df_imp = pd.DataFrame({'feature':list(X_over.columns), 'importance':importance})
display(df_imp.sort_values('importance', ascending=False).head(20))

# plot feature importance
plt.bar(list(X_over.columns), importance)
plt.show()

它给了一个错误

...
~AppDataLocalContinuumanaconda3libsite-packagesjoblibparallel.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):

~AppDataLocalContinuumanaconda3libsite-packagessklearnlinear_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':

~AppDataLocalContinuumanaconda3libsite-packagessklearnutilsoptimize.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 += "
" + extra_warning_msg

AttributeError: 'str' object has no attribute 'decode'    

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

I googled it and mostly all the responses said that this error is because the scikit-learn library tried to decode an already decoded string. But I don't know how to solve it in my case here. I made sure all my data is either integer or float64, and no strings.

推荐答案

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

I tried to upgrade my scikit-learn using the below command, still, that didn't solve the AttributeError: 'str' object has no attribute 'decode' issue

pip install scikit-learn  -U

最后,下面的代码片段解决了这个问题,将求解器添加为 liblinear

Finally, below code snippet solved the issue, add the solver as liblinear

model = LogisticRegression(solver='liblinear')

这篇关于AttributeError: 'str' 对象在拟合逻辑回归模型时没有属性 'decode'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆