有人可以从scipy.sparse.csr_matrixdata,index,indptr,[shape =M,N]理解这个意外的值错误吗?

问题描述

使用scipy.sparse.csr_matrix(((数据,索引,indptr),[shape =(M,N)]))时出现值错误:数据,索引和indptr的排名应为1。但是数据索引我正在使用的indptr和indptr是等级1,我已经用numpy.linalg.matrix_rank()确认了这一点,该函数返回每个矩阵的等级1…没有人知道是什么原因导致此错误和/或在哪里看? / p>

调用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)

我收到的错误消息是:

enter image description here

由于我使用的数据,索引和indptr的排名为1,因此它们的形状似乎很合理...我只是看不到此值错误来自何处!

任何帮助将不胜感激!谢谢:)

解决方法

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

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