I'm trying to make a clean connection between the dimensions in a numpy array and the dimensions of a matrix via classical linear algebra. Suppose the following:
In [1] import numpy as np
In [2] rand = np.random.RandomState(42)
In [3] a = rand.rand(3,2)
In [4] a
Out[4]:
array([[0.61185289, 0.13949386],
[0.29214465, 0.36636184],
[0.45606998, 0.78517596]])
In [5]: a[np.newaxis,:,:]
Out[5]:
array([[[0.61185289, 0.13949386],
[0.29214465, 0.36636184],
[0.45606998, 0.78517596]]])
In [6]: a[:,np.newaxis,:]
Out[6]:
array([[[0.61185289, 0.13949386]],
[[0.29214465, 0.36636184]],
[[0.45606998, 0.78517596]]])
In [7]: a[:,:,np.newaxis]
Out[7]:
array([[[0.61185289],
[0.13949386]],
[[0.29214465],
[0.36636184]],
[[0.45606998],
[0.78517596]]])
My questions are as follows:
- Is is correct to say that the dimensions of
aare 3 X 2? In other words, a 3 X 2 matrix? - Is it correct to say that the dimensions of
a[np.newaxis,:,:]are 1 X 3 X 2? In other words, a matrix containing a 3 X 2 matrix? - Is it correct to say that the dimensions of
a[:,np.newaxis,:]are 3 X 1 X 2? In other words a matrix containing 3 1 X 2 matrices? - Is it correct to say that the dimensions of
a[:,:,np.newaxis]are 3 X 2 X1? In other words a matrix containing 3 matrices each of which contain 2 1 X 1 matrices?
arrayswith varying dimensions. Anumpycan have 0, 1, 2 or more dimensions.a.shapeis a tuple with those dimensions, and hence can be(),(3,)or(2,3,4). Where possiblenumpytries to tread each dimension as "equally-important".