Block of memory

Numpy array consists of raw data and mechanism to access data. Numpy arrays are stored in continuous memory locations and views of array shares the same memory locations.

We can access those meta information using np.array_interface.

a = np.arange(10)
 
a.__array_interface__
# {'data': (105553130113056, False),
#'strides': None,
#'descr': [('', '<i8')],
#'typestr': '<i8',
#'shape': (10,),
#'version': 3}

It has memory location, datatype description and other meta information.

ndarray from other memory

We can create ndarray using a buffer doesn’t own by numpy.

a = b'abc'
 
b = np.frombuffer(a, dtype=np.int8)
# array([97, 98, 99], dtype=int8)

b uses a as source of data and b contains ascii values of abc.

b.flags
 
# C_CONTIGUOUS : True
# F_CONTIGUOUS : True
# OWNDATA : False
# WRITEABLE : False
# ALIGNED : True
# WRITEBACKIFCOPY : False

b is only readable and it doesn’t own the data.

References

  1. https://lectures.scientific-python.org/advanced/advanced_numpy/index.html#block-of-memory