Memory Allocation Numpy Arrays

In this class, We discuss Memory Allocation Numpy Arrays.

For Complete YouTube Video: Click Here

The reader should have prior knowledge of how python allocates the list memory. Click here.

Would you please check the link for memory allocation to the list?

Consider RAM with byte-addressable. I.e., each address is having a byte of memory.

The list memory is random.

In arrays, memory is continuous. If the starting address is known, we can easily access elements in the array.

Accessing elements in the list is difficult.

From now we take RAM horizontally.

The below diagram shows RAM horizontally.

Two ways to store arrays 1) Row Major Order 2) Column Major Order

we discuss how elements in row-major order and column-major order.

To access elements in row-major and column-major order.

Consider the below example. We are taking a 2-dimensional array.

import numpy as np
x=[[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15]]
y=np.array(x)
print(y)

Output:
[[ 1  2  3  4  5]
 [ 6  7  8  9 10]
 [11 12 13 14 15]]

In the example, Given row and column indexes.

Suppose we want to access the first-row second column element, i.e., 8.

How to access it? How does python get the value?

If row-major order. The elements of the array are saved in RAM, as shown below.

Memory Allocation Numpy Arrays1
Row Major Order

Assume the integer is taking two bytes of space.

The array space is starting from memory location 50.

The first element space in memory locations 50 and 51. The second element space in memory locations 52 and 53.

Similarly, take the first-row fill in memory, then the second row fills in memory. So on continuously.

The row-major order saves the elements based on rows.

How to access the elements?

Suppose we want to access the element in the first-row second column. We need to jump one row and two elements.

Suppose we need to access elements in the second-row second column. We need to jump two rows and two elements.

We use the calculation starting address + index of row*(Number of elements in a row *size of each element)+index of column*size of each element.

To access element a[1][2] is 50+1*(5*2)+2*2 = 64.

At 64 location, we have element 8. check-in the above diagram.

Similarly, Column major order stores the elements according to columns.

Memory Allocation Numpy Arrays2
Column Major Order

Take the first column and save it in memory, then the second column, and so on.

The below diagram shows the elements in column-major order.

To access the element a[1][2] in column-major order. We use the below equation.

starting address + index of columns*(number of elements in the column*size of each element)+index of rows* size of each element.

50 + 2*(3*2)+1*2 =64

The element 8 is present in the location 64.