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 | |
a is a 1-dimensional array with 12 consecutive integers from 0 to 11.
1 | |
This reshapes the same data into a 2-dimensional array with 3 rows and 4 columns:
1 | |
Important notes:
bis a new view of the same data buffer asa(not a copy) whenever possible. This means:1
2b[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结构/