构建Logistic回归模型时,AttributeError:'str'对象没有属性'decode'

AttributeError: #39;str#39; object has no attribute #39;decode#39; while building a logistic regression model(构建Logistic回归模型时,AttributeError:#39;str#39;对象没有属性#39;decode#39;)
本文介绍了构建Logistic回归模型时,AttributeError:'str'对象没有属性'decode'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立Logistic回归模型,但它显示AttributeError: 'str' object has no attribute 'decode'。请帮我解决这个问题。此代码在Datacamp的服务器上运行良好,但在我的笔记本电脑上显示AttributeError。

import pandas as pd
df = pd.read_csv('datasets/diabetes.csv')
X = df.drop('diabetes',axis = 1)
y = df['diabetes']

# Import the necessary modules
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report, confusion_matrix

# Create training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.4, random_state=42)

# Create the classifier: logreg
logreg = LogisticRegression()

# Fit the classifier to the training data
logreg.fit(X_train,y_train)

错误消息:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-8-c8cf98ee145a> in <module>
     16 
     17 # Fit the classifier to the training data
---> 18 logreg.fit(X_train,y_train)
     19 
     20 #Predict the labels of the test set: y_pred

~anaconda3envs	ensorflowlibsite-packagessklearnlinear_model\_logistic.py in fit(self, X, y, 
sample_weight)
   1405         else:
   1406             prefer = 'processes'
-> 1407         fold_coefs_ = Parallel(n_jobs=self.n_jobs, verbose=self.verbose,
   1408                                **_joblib_parallel_args(prefer=prefer))(
   1409             path_func(X, y, pos_class=class_, Cs=[C_],


~anaconda3envs	ensorflowlibsite-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'

如有任何建议,我们将不胜感激

推荐答案

这似乎是SCHKITL学习版的问题。

无论如何,在最新版本的SCRICIT-LEARN(现在是0.24.1)中,这个问题已经修复,将部分代码包含在try-Catch块中。Gigioz在此堆栈溢出question中对此进行了更详细的解释。

可能您有较旧的版本,所以我建议您使用下面的代码升级SCRICKIT-LINE库:

pip install -U scikit-learn

然后重新启动内核,检查新版本是否正确更新,并再次运行代码。

这篇关于构建Logistic回归模型时,AttributeError:&#39;str&#39;对象没有属性&#39;decode&#39;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)