numpy - Python matrix represented as (40000,) -


i have seen , executed python program matrix has been sliced column vector using a[:,j] , passed function. column vector has dimensions 40000x1. while tracing output, printed dimensions of a in function , printed (40000,). in function, matrix multiplied matrix b of dimensions 1x40000. printed dimensions of result , says result 1x40000. how possible? have read a column vector(obviously) how can product generate 1x40000 matrix? doesn't satisfy matrix multiplication rule. i'm using numpy.

edit:

code:

def update_state(xk, sk, wx, wrec):     print("sizes:")     print(xk.shape)     print(wx.shape)     print((xk*wx).shape)     print((xk * wx + sk * wrec).shape)     return xk * wx + sk * wrec  def forward_states(x, wx, wrec):     s = np.zeros((x.shape[0], x.shape[1]+1))     k in range(0, x.shape[1]):         s[:,k+1] = update_state(x[:,k], s[:,k], wx, wrec)     return s 

output:

sizes: (40000,) (1, 40000) (1, 40000) (1, 40000) 

i assuming referring numpy package. there other array packages available python, seems popular one.

numpy deals multidimensional arrays, not matrices. there big difference in 1d array of n elements can interpreted either nx1 or 1xn matrix, depending on how choose interpret it.

another thing aware of function numpy.multiply, a.k.a. * operator not same numpy.dot, a.k.a @ operator. operations involving * see in code element-by element multiplication, not matrix multiplication seem think.

numpy provides number of different mechanisms mapping elements of arrays compatible dimensions each other. common way, being used in example, called broadcasting.

let apply information in link operation kx * wx in code. use simplified version of kx , wx make easier illustrate issue @ hand:

>>> kx = np.arange(10) >>> kx array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> kx.shape (10,)  >>> wx = np.arange(10).reshape(1, 10) >>> wx array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]) >>> wx.shape (1, 10) 

here condensed version of relevant broadcasting rules:

when operating on 2 arrays, numpy compares shapes element-wise. starts trailing dimensions, , works way forward. 2 dimensions compatible when:

  1. they equal, or
  2. one of them 1

... size of resulting array maximum size along each dimension of input arrays.

in case, (n,) array multiplied (1,n) array, resulting in (1,n) array contains element-wise product of inputs:

>>> >>> kx*wx array([[ 0,  1,  4,  9, 16, 25, 36, 49, 64, 81]]) 

this has nothing matrix multiplication. remaining operations can analyzed same rules.