3

I have looked online for the past 4 hours and I have not been able to find a solution to this that works. It's such a stupid problem that I didn't want to ask it so I wouldn't get downvoted, but here goes nothing.

The task:

I have a (1xN) csr matrix and I want to simply make it (1xN+1) matrix by adding the number "1" to the end.

Everything I tried and the errors:

vstack(xi, np.ones((1,1))):

TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U32') dtype('<U32') dtype('<U32')

hstack(xi, np.ones((1,1))):

ValueError: blocks must be 2-D

np.concatenate([xi, [np.ones(1)]]):

ValueError: zero-dimensional arrays cannot be concatenated

xi = np.concatenate([xi, np.ones(1)]):

ValueError: zero-dimensional arrays cannot be concatenated

and so many variants of the above... please anybody help I've been on this for so long and I'm thinking of giving up on this entire project.

JoeVictor
  • 1,806
  • 1
  • 17
  • 38
  • 2
    A `csr` matrix is not a numpy array. Many numpy functions don't work with it. Use the `sparse` functions. https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.hstack.html – hpaulj Jul 17 '18 at 01:17
  • Yes, that worked. This is what did it: `hstack([xi, np.ones(1)])` apparently I was two square brackets away from the right answer. Please post an answer so I can accept it as a solution! – JoeVictor Jul 17 '18 at 01:34
  • 1
    Yes, the sparse `hstack` wants a list, just like the `np.hstack`. It'll be worth your while to look at its code (and at `bmat`). – hpaulj Jul 17 '18 at 01:51

0 Answers0