Understanding the ndarray and its shape

The best way to think about NumPy arrays is that they consist of two parts, a data buffer which is just a block of raw elements, and a view which describes how to interpret the data buffer. The data buffer is a contiguous block of memory that contains the actual data, and the view is a way of accessing the data in a different way.

For more refer to links


1
2
3
4
5
import numpy as np    # (assuming you imported numpy)

a = np.arange(12)
print(a)
# Output: [ 0 1 2 3 4 5 6 7 8 9 10 11]

a is a 1-dimensional array with 12 consecutive integers from 0 to 11.

1
2
b = a.reshape(3, 4)
print(b)

This reshapes the same data into a 2-dimensional array with 3 rows and 4 columns:

1
2
3
[[ 0  1  2  3]
[ 4 5 6 7]
[ 8 9 10 11]]

Important notes:

  • b is a new view of the same data buffer as a (not a copy) whenever possible. This means:
    1
    2
    b[0, 0] = 99
    print(a[0]) # → 99 (modifying b also modifies a)

NumPy 默认使用 C-order(行优先,row-major) 存储二维数组

a 在内存的表现

Index Data
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
11 11

b 在内存的表现

默认行有限

row_index col_index Data
0 0 0
0 1 1
0 2 2
0 3 3
1 0 4
1 1 5
1 2 6
1 3 7
2 0 8
2 1 9
2 2 10
2 3 11

b 的 ndarray 对象中的 data 指针指向 a 的存储区域


Understanding the ndarray and its shape
https://jackiedai.github.io/2025/12/02/011Python/013理解ndarray结构/
Author
Lenthiu
Posted on
December 2, 2025
Licensed under