Numpy is based on the ndarray memory layout property

Mondo games Updated on 2024-02-01

The ndarray property reflects the information of the array itself, and the internal information of the array can be accessed or set by accessing the property.

The ndarray property includes memory layout attributes, data type attributes, and other attributes.

The memory layout properties of the ndarray are as follows:

Description:

ndarray.flags obtains the memory information of the ndarray object, including the following attributes:

Example

>>import numpy as np

>arr=np.array([1,2,3])

>arr

array([1, 2, 3])

# arr.flags to get the array memory information.

>arr.flags

c_contiguous : true

f_contiguous : true

owndata : true

writeable : true

aligned : true

writebackifcopy : false

Access memory information in the form of a dictionary.

>arr.flags['c_contiguous']

true accesses memory information through a period + lowercase property name.

>arr.flags.c_contiguous

true

Description:ndarray.ndim gets the number of dimensions of the numpy array, or the number of axes, called rank.

In numpy, each linear array is called an dimensions or an axis.

Each element of a one-dimensional array is a single number or string with a number of axes of 1 and an outermost one.

Each element of a two-dimensional array is a one-dimensional array, with 2 axes and 2 outermost layers.

Each element of a three-dimensional array is a two-dimensional array. The number of axes is 3, and the most predicate is 3.

Example

>>import numpy as np

A one-dimensional array where each element is a single number or a single string.

>ar1=np.array([1,2,3])

A two-dimensional array where each element is a single number.

>ar2=np.array([[1,2,3]])

Three-dimensional arrays, where each element is a two-dimensional array.

>ar3=np.array([[1,2,3]]]

>ar1

array([1, 2, 3])

>ar2

array([[1, 2, 3]])

>ar3

array([[1, 2, 3]]]

The number of axes of a one-dimensional array is 1, and the outermost layer is 1.

>ar1.ndim

The number of axes of the two-dimensional array is 2, and the outermost layer is 2.

>ar2.ndim

The number of axes of the three-dimensional array is 3, and the outermost layer is 3.

>ar3.ndim

Description:ndarray.shape returns tuples that represent the size of each axis of the array.

The size of the axis represents the number of elements of the same dimension.

Example

>>import numpy as np

>ar1=np.array([1,2,3])

>ar2=np.array([[1,2,3],[5,6,7]])

>ar3=np.array([[1,2,3],[5,6,7]]]

>ar1.shape

>ar2.shape

shape returns a tuple of the size of each axis.

The size of the axis represents the number of elements of the same dimension.

The axis counts the size from the outside to the inside.

The outermost three-dimensional array has one two-dimensional array [[1,2,3],[5,6,7]] with a number of elements

The sub-outer, two-dimensional array has two one-dimensional arrays [1,2,3], [5,6,7], and the number of elements is 2

The innermost layer, a one-dimensional array has 3 elements.

>ar3.shape

Description:ndarray.size returns the total number of elements in the array.

Example

>>import numpy as np

>ar1=np.array([1,2,3])

>ar2=np.array([[1,2,3],[5,6,7]])

>ar3=np.array([[1,2],[3,5]],6,7],[8,9]]]

# ndarray.size returns the total number of elements in the array.

>ar1.size

>ar2.size

>ar3.size

Description:ndarray.ItemSize returns the byte length (size) of an element.

Example

>>import numpy as np

>ar3=np.array([[1,2],[3,5]],6,7],[8,9]]]

itemsize returns the byte length of the element.

>ar3.itemsize

dtype returns the element type, int32 is 4 bytes.

>ar3.dtype

dtype('int32')

Description:ndarray.nbytes returns the total byte length (size) of the array element.

Use the nbytes itemsize to obtain the total number of elements.

Example

>>import numpy as np

>ar3=np.array([[1,2],[3,5]],6,7],[8,9]]]

nbytes returns the total byte length of the array element.

>ar3.nbytes

>ar3.itemsize

Use the nbytes itemsize to obtain the total number of elements.

>ar3.nbytes/ar3.itemsize

Same as size.

>ar3.size

Related Pages