Work with N on 1 matrices in Numpy

Given a numpy sized array (n,), how do you convert it to a numpy sized array (n,1).

The reason is because I'm trying to multiply the matrix by numpy arrays of size (n,)and (,n)to get (n,n), but when I do this:

numpy.dot(a,b.T)

It says that you cannot do this. I know as a fact that the transfer (n,)does nothing, so it would be easy to change (n,)and make them (n,1)and avoid this problem together.

+4
source share
2 answers

Use reshape (-1,1)to change (n,)to (n,1), see detailed examples:

In [1]:

import numpy as np
A=np.random.random(10)
In [2]:

A.shape
Out[2]:
(10,)
In [3]:

A1=A.reshape(-1,1)
In [4]:

A1.shape
Out[4]:
(10, 1)
In [5]:

A.T
Out[5]:
array([ 0.6014423 ,  0.51400033,  0.95006413,  0.54321892,  0.2150995 ,
        0.09486603,  0.54560678,  0.58036358,  0.99914564,  0.09245124])
In [6]:

A1.T
Out[6]:
array([[ 0.6014423 ,  0.51400033,  0.95006413,  0.54321892,  0.2150995 ,
         0.09486603,  0.54560678,  0.58036358,  0.99914564,  0.09245124]])
+2

None , .

a = np.asarray([1,2,3])
a[:]
a[:, None]

In [48]: a
Out[48]: array([1, 2, 3])

In [49]: a[:]
Out[49]: array([1, 2, 3])

In [50]: a[:, None]
Out[50]: 
array([[1],
       [2],
       [3]])
+4

Source: https://habr.com/ru/post/1531610/


All Articles