Michael Chase
Michael Chase

Reputation: 67

Scipy: Sparse matrix is of wrong dimension?

Good Afternoon,

I am trying to do:

scipy.sparse.dia_matrx(x, shape = (x.size, x.size))

but the resulting shape of the matrix is x.size x 1. Am i doing something wrong? Or did i miss something in the documentation?

It matters because I'm multiplying by dense matrices/vectors.

TIA

>>> t scipy.sparse.dia_matrix(x, shape = (x.size, x.size))
>>> t
<217766x1 sparse matrix of type '<class 'numpy.float64'>'
with 217766 stored elements (217766 diagonals) in DIAgonal format>
>>> t.shape
(217766, 1) 

X is a 217766x1 Numpy Array

Upvotes: 1

Views: 1119

Answers (1)

Warren Weckesser
Warren Weckesser

Reputation: 114781

If x has shape (N, 1), then I think you want:

 t = dia_matrix((x.T, 0), shape=(x.size, x.size))

If x has shape (N,) (i.e. it is a 1-d array), then the transpose of x in the above is not necessary.

Upvotes: 2

Related Questions