I am new to numpy and i was reading numpy but i am not able to understand row major and column major in numpy can anyone explain with example with easiest way? Any answer would be appreciated thanks
Consider an array such as this:
>>> A = np.random.randint(low=1, high=9, size=(3,3))
>>> A
array([[8, 7, 2],
[4, 2, 5],
[8, 6, 7]])
Using row-major means the values would be stored in memory like this (assuming 64-bit integers):
Memory address 0x00 0x08 0x10 0x18 0x20 0x28 0x30 0x38 0x40
Value 8 7 2 4 2 5 8 6 7
Whereas column-major storage would look like this:
Memory address 0x00 0x08 0x10 0x18 0x20 0x28 0x30 0x38 0x40
Value 8 4 8 7 2 6 2 5 7
Numpy stores in row-major order by default.
>>> A[0].__array_interface__['data']
(14502656, False)
>>> A[1].__array_interface__['data']
(14502680, False) # 14502680 - 14502656 == 24
You can see the second row of data is 24 bytes (three int64 worth) away from the first. The transposed array offers a view into the original array data, not a copy, which is strided in a way that appears column major (the actual data in memory remains the same order):
>>> A.T[0].__array_interface__['data']
(14502656, False)
>>> A.T[1].__array_interface__['data']
(14502664, False) # 14502664 - 14502656 == 8