【问题标题】:AttributeError: 'str' object has no attribute 'decode' while building a logistic regression model [duplicate]AttributeError:'str'对象在构建逻辑回归模型时没有属性'decode'[重复]
【发布时间】:2021-05-20 01:22:00
【问题描述】:

我正在尝试构建逻辑回归模型,但它显示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

~\anaconda3\envs\tensorflow\lib\site-packages\sklearn\linear_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_],


~\anaconda3\envs\tensorflow\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'

任何建议将不胜感激

【问题讨论】:

    标签: python scikit-learn logistic-regression


    【解决方案1】:

    看来这是scikitl-learn版本的问题。

    无论如何,在最新版本的 scikit-learn(现为 0.24.1)中,问题已得到修复,将部分代码包含在 try-catch 块中。 Ggioz 在此 stackoverflow question 中对此进行了更详细的解释。

    可能您的版本较旧,因此我建议您使用以下代码升级 scikit-learn 库:

    pip install -U scikit-learn
    

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

    【讨论】:

      猜你喜欢
      • 2021-04-17
      • 2021-05-11
      • 1970-01-01
      • 2018-12-01
      • 2014-05-26
      • 2021-07-05
      • 2021-10-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多