sklearn 的 accuracy_score 函数输入错误

Type error with sklearn's accuracy_score function

我正在尝试从 Python 中的 sklearn.metrics 运行 accuracy_score。我的真实 y 和预测 y 都是稀疏矩阵格式 --

import scipy.sparse as sp
from sklearn.metrics import accuracy_score
y_true = sp.csr_matrix(y.values) # where y is a multi-label dataframe
y_pred = model.predict(X) # X is same format as y_true    
accuracy_score(y_true, y_pred)

我收到以下错误:

TypeError: len() of unsized object

我检查了documentation ,它应该能够接受稀疏矩阵。

为清楚起见,当我尝试查看内容时,我得到以下内容:

[In]  y_true
[Out] <9646x1248 sparse matrix of type '<class 'numpy.int64'>'
                 with 36700 stored elements in Compressed Sparse Row format>
[In]  y_pred
[Out] <9646x1248 sparse matrix of type '<class 'numpy.int64'>'
                 with 373603 stored elements in Compressed Sparse Row format>

为什么会出现此错误以及如何修复我的输入?

将矩阵转换为正则矩阵y_pred = y_pred.Ay_true = y_true.A,然后计算accuracy_score(y_true, y_pred)