1

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:

  1. Is is correct to say that the dimensions of a are 3 X 2? In other words, a 3 X 2 matrix?
  2. 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?
  3. 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?
  4. 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?
1
  • 1
    As temporary bridge these descriptions may help, but in long run you should work with numpy's own terminology. Those are all arrays with varying dimensions. A numpy can have 0, 1, 2 or more dimensions. a.shape is a tuple with those dimensions, and hence can be (), (3,) or (2,3,4). Where possible numpy tries to tread each dimension as "equally-important". Commented Feb 12, 2021 at 20:34

1 Answer 1

1
  1. yes
  2. yes
  3. yes
  4. three 2x1 matrices each of which contains one vector of size 1

Just find out using .shape:

import numpy as np

rand = np.random.RandomState(42)

# 1.
a = rand.rand(3, 2)
print(a.shape, a, sep='\n', end='\n\n')

# 2.
b = a[np.newaxis, :, :]
print(b.shape, b, sep='\n', end='\n\n')

# 3.
c = a[:, np.newaxis, :]
print(c.shape, c, sep='\n', end='\n\n')

# 4.a
d = a[:, :, np.newaxis]
print(d.shape, d, sep='\n', end='\n\n')

# 4.b
print(d[0].shape, d[0], sep='\n', end='\n\n')
print(d[0, 0].shape, d[0, 0])

output:

(3, 2)
[[0.37454012 0.95071431]
 [0.73199394 0.59865848]
 [0.15601864 0.15599452]]

(1, 3, 2)
[[[0.37454012 0.95071431]
  [0.73199394 0.59865848]
  [0.15601864 0.15599452]]]

(3, 1, 2)
[[[0.37454012 0.95071431]]

 [[0.73199394 0.59865848]]

 [[0.15601864 0.15599452]]]

(3, 2, 1)
[[[0.37454012]
  [0.95071431]]

 [[0.73199394]
  [0.59865848]]

 [[0.15601864]
  [0.15599452]]]

(2, 1)
[[0.37454012]
 [0.95071431]]

(1,) [0.37454012]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.