本文的主要目的在于理解
numpy.ndarray的内存结构及其背后的设计哲学。
ndarray是什么
NumPy provides an N-dimensional array type, the ndarray, which describes a collection of “items” of the same type. The items can be indexed using for example N integers.
—— from https://docs.scipy.org/doc/numpy-1.17.0/reference/arrays.html
ndarray是numpy中的多维数组,数组中的元素具有相同的类型,且可以被索引。
如下所示:
>>> import numpy as np
>>> a = np.array([[0,1,2,3],[4,5,6,7],[8,9,10,11]])
>>> a
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> type(a)
<class 'numpy.ndarray'>
>>> a.dtype
dtype('int32')
>>> a[1,2]
6
>>> a[:,1:3]
array([[ 1, 2],
[ 5, 6],
[ 9, 10]])
>>> a.ndim
2
>>> a.shape
(3, 4)
>>> a.strides
(

本文详细介绍了numpy的ndarray,包括它的设计哲学,如数据存储与解释方式的分离,以及内存布局,强调了数据的连续存储和元数据的重要性。ndarray的内存布局由数据缓冲区和元数据组成,元数据包含数据类型、维度、形状和步长等信息。此外,文章还讨论了副本和视图的概念,以及这样的设计如何提高效率和节省内存。最后,指出ndarray的设计是为矩阵运算优化,所有数据单元同类型,使得按秩访问高效并节省空间。
最低0.47元/天 解锁文章
2153

被折叠的 条评论
为什么被折叠?



