Search code examples
pythonnumpymachine-learningscipyartificial-intelligence

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


When using scipy.sparse.csr_matrix((data, indices, indptr), [shape=(M, N)]) I get the value error: data, indices, and indptr should be rank 1. BUT the data indices and indptr I am using are rank 1 and I have confirmed this with numpy.linalg.matrix_rank() which returns rank 1 for each of the matrices … does anyone have any idea what may be causing this error and/or where to look?

the way I'm calling scipy.sparse.csr_matrix is:

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

the rank and shape of variables are:

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)

The error message I get is:

enter image description here

Since data, indices and indptr I am using have rank 1, and their shapes seems sensible ... I just can't see where this value error is coming from!

Any help is greatly appreciated! Thanks :)


Solution

  • rank 1 arrays are arrays of shape (m, ), the arrays of shape (m, 1) are commonly known as vectors. To make your numpy arrays rank-1 array, use ravel method. You can read the doc here. Usage is as follows

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