任何人都可以从 scipy.sparse.csr_matrix((data, indices, indptr), [shape=(M, N)]) 中理解这个意外的值错误吗?

Can anyone make sense of this unexpected value error from scipy.sparse.csr_matrix((data, indices, indptr), [shape=(M, N)])?

当使用 scipy.sparse.csr_matrix((data, indices, indptr), [shape=(M, N)]) 我得到值错误:data, indices, and indptr should be rank 1. 但是我正在使用的数据索引和 indptr 排名为 1,我已经用 numpy.linalg.matrix_rank() 确认了这一点,其中 returns 每个矩阵的排名为 1 ......有没有人知道可能导致此错误的原因 and/or 去哪里看?

我打电话给 scipy.sparse.csr_matrix 的方式是:

initCSR = sps.csr_matrix(( np.ones((self.N*self.T,1)), ex_s_reshaped, po), shape=(self.mdp_data['states'],self.T*self.N))

变量的秩和形状为:

np.ones((self.N*self.T,1)) rank: 1 and shape (8000, 1)

ex_s_reshaped rank: 1 and shape (8000, 1)

po rank: 1 and shape (1, 8000)

Shape = (4, 8000)

我收到的错误信息是:

由于我使用的 data、indices 和 indptr 排名为 1,而且它们的形状看起来很合理...我只是看不出这个值错误从何而来!

非常感谢任何帮助!谢谢:)

rank 1 数组是 (m, ) 形状的数组,(m, 1) 形状的数组通常称为向量。要使您的 numpy 数组成为 rank-1 数组,请使用 ravel 方法。您可以阅读文档 here。用法如下

vector = np.random.randn(100,1) # vector.shape = (100, 1)
rank_1_array = vector.ravel() # rank_1_array.shape = (100, )