Logistic Regression - ValueError: classification metrics can't handle a mix of continuous-multi output and binary targets

Logistic Regression - ValueError: classification metrics can't handle a mix of continuous-multi output and binary targets

我是一名数据科学新手,正在研究 Kaggle Titanic dataset。我正在 运行 对其进行逻辑回归,以预测测试数据集中的乘客是幸存还是死亡。

我清理了训练数据和测试数据,运行 逻辑回归拟合了训练数据。一切顺利。

train = pd.read_csv('train.csv')    
X_train = train.drop('Survived',axis=1)
y_train = train['Survived']
from sklearn.linear_model import LogisticRegression
logmodel = LogisticRegression()
logmodel.fit(X_train,y_train)

然后我运行在测试数据上的预测模型是这样的:

test = pd.read_csv('test.csv') 
predictions = logmodel.predict(test)

然后我尝试打印混淆矩阵:

from sklearn.metrics import classification_report, confusion_matrix
print(confusion_matrix(test,predictions))

我收到一条错误消息:

ValueError: Classification metrics can't handle a mix of continuous-multioutput and binary targets

这是什么意思,我该如何解决?

我看到的一些潜在问题是:

  1. 我正在用测试数据的预测模型做一些超级愚蠢和错误的事情。
  2. 特征值 "Age" 和 "Fare"(乘客的成本 ticket) 是浮点数,其余是整数。

我哪里错了?感谢您的帮助!

大概您的 test 由布尔值(存活或死亡)组成,而您 predictions 由浮点数(预测的生存概率)组成。您应该选择一些阈值,然后根据预测概率是否大于阈值生成布尔值。

正如 m-dz 评论的那样,confusion_matrix expects 2 arrays,而在您的代码中您传递了整个 test 数据帧。

此外,另一个 不尊重参数的 顺序 ,这很重要。

总而言之,你应该要求

confusion_matrix(test['Survived'], predictions)