1

I'm trying to obtain all the values in a matrix beta VxK to the power of all the values in a column Vx1 that is part of a dense matrix VxN. So each value in beta should be to the power of the corresponding line in the column and this should be done for all K columns in beta. When I use np.power on python for a practice numpy array for beta using:

np.power(head_beta.T, head_matrix[:,0]) 

I am able to obtain the results I want. The dimensions are (3, 10) for beta and (10,) for head_matrix[:,0] where in this case 3=K and 10=V.

However, if I do this on my actual matrix, which was obtained by using

matrix=csc_matrix((data,(row,col)), shape=(30784,72407) ).todense()  

where data, row, and col are arrays, I am unable to do the same operation:

np.power(beta.T, matrix[:,0])

where the dimensions are (10, 30784) for beta and (30784, 1) for matrix where in this case 10=K and 30784=V. I get the following error

ValueError                                Traceback (most recent call last)
<ipython-input-29-9f55d4cb9c63> in <module>()
----> 1 np.power(beta.T, matrix[:,0])

ValueError: operands could not be broadcast together with shapes (10,30784) (30784,1) `

It seems that the difference is that matrix is a matrix (length,1) and head_matrix is actually a numpy array (length,) that I created. How can I do this same operation with the column of a dense matrix?

GrandMasterFlush
  • 6,269
  • 19
  • 81
  • 104
Rebe
  • 11
  • 1

2 Answers2

1

In the problem case it can't broadcast (10,30784) and (30784,1). As you note it works when (10,N) is used with (N,). That's because it can expand the (N,) to (1,N) and on to (10,N).

M = sparse.csr_matrix(...).todense()

is np.matrix which is always 2d, so M(:,0) is (N,1). There are several solutons.

np.power(beta.T, M[:,0].T)  # change to a (1,N)
np.power(beta, M[:,0])      # line up the expandable dimensions

convert the sparse matrix to an array:

A = sparse.....toarray()
np.power(beta.T, A[:,0])

M[:,0].squeeze() and M[:,0].ravel() both produce a (1,N) matrix. So does M[:,0].reshape(-1). That 2d quality is persistent, as long as it returns a matrix.

M[:,0].A1 produces a (N,) array

From a while back: Numpy matrix to array

Community
  • 1
  • 1
hpaulj
  • 221,503
  • 14
  • 230
  • 353
0

You can use the squeeze method on arrays to get rid of this extra dimension. So np.power(beta.T, matrix[:,0].squeeze()) should do the trick.

rocksportrocker
  • 7,251
  • 2
  • 31
  • 48
  • 1
    Technically with a `np.matrix` `squeeze` doesn't get rid of the extra dimension, it just changes the shape to `(1,N)`, like `ravel`. – hpaulj Apr 15 '16 at 23:34