'What is a rank 1 array in Numpy
Consider the following vector:
import numpy as np
u = np.random.randn(5)
print(u)
[-0.30153275 -1.48236907 -1.09808763 -0.10543421 -1.49627068]
When we print its shape:
print(u.shape)
(5,)
I was told this is neither a column vector nor a row vector. So what is essentially this shape is in numpy (m,)
?
Solution 1:[1]
# one-dimensional array (rank 1 array)
# array([ 0.202421 , 1.04496629, -0.28473552, 0.22865349, 0.49918827])
a = np.random.randn(5,) # or b = np.random.randn(5)
# column vector (5 x 1)
# array([[-0.52259951],
# [-0.2200037 ],
# [-1.07033914],
# [ 0.9890279 ],
# [ 0.38434068]])
c = np.random.randn(5,1)
# row vector (1 x 5)
# array([[ 0.42688689, -0.80472245, -0.86294221, 0.28738552, -0.86776229]])
d = np.random.randn(1,5)
For example (see docs):
numpy.dot(a, b)
- If both a and b are 1-D arrays, it is inner product of vectors (without complex conjugation).
- If both a and b are 2-D arrays, it is matrix multiplication
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | lamsal |