使用sklearn的accuracy_score函数键入错误

时间:2018-06-01 15:09:19

标签: python scipy scikit-learn typeerror sparse-matrix

我正在尝试从Python中的sklearn.metrics运行accuracy_score。我的真实情况和预测的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>

为什么我收到此错误以及如何修复输入?

1 个答案:

答案 0 :(得分:0)

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

相关问题