python - numpy中ndarray的 "ndim, shape, size, ..etc"的身份是什么

标签 python numpy multidimensional-array

我是 Python 的新手。

用了很多年Matlab,最近开始研究numpy/scipy

似乎numpy最基本的元素似乎是ndarray。 在ndarray中,有如下属性:

  • ndarray.ndim
  • ndarray.shape
  • ndarray.size
  • ...等等

我对 C++/JAVA 类非常熟悉,但我是 Python OOP 的新手。


Q1:我的第一个问题是上述属性的身份是什么?

起初,我假设上述属性可能是公共(public)成员变量。但是很快,我发现a.ndim = 10不起作用(假设andarray的对象)所以,看来不是公共(public)成员变量。

接下来,我猜测它们可能是公共(public)方法,类似于C++中的getter方法。但是,当我尝试使用括号 a.nidm() 时,它不起作用。所以,它似乎不是一个公共(public)方法。

另一种可能是它们是私有(private)成员变量,但是 print a.ndim 有效,所以它们不能是私有(private)数据成员。

所以,我无法弄清楚上述属性的真实身份是什么。


Q2。在哪里可以找到 ndarray 的 Python 代码实现?由于我在本地 PC 上安装了 numpy/scipy,我想可能有一些方法可以查看源代码,然后我想一切都会清楚。

您能就此提出一些建议吗?

最佳答案

numpy作为 C 的混合实现代码和Python代码。来源可在 github 上浏览, 并且可以下载为 git存储库。但是深入研究 C来源需要一些工作。许多文件被标记为 .c.src ,这意味着它们在编译之前要经过一层或多层的处理。

Python 也是用 C 和 Python 混合编写的。所以不要试图用 C++ 术语来强制事情。

最好利用您的 MATLAB 经验,并进行调整以允许 Python。和 numpy有许多超越 Python 的怪癖。它使用 Python 语法,但因为它有自己的 C 代码,所以它不仅仅是一个 Python 类。

我使用 Ipython作为我平时的工作环境。这样我就可以使用 foo?查看 foo 的文档(与 Python help(foo)foo?? 相同以查看代码 - 如果它是用 Python 编写的(如 MATLAB/Octave type(foo) )

Python 对象具有属性和方法。还有 properties看起来像属性,但实际上使用方法来获取/设置。通常你不需要知道属性和特性之间的区别。

 x.ndim   # as noted, has a get, but no set; see also np.ndim(x)
 x.shape   # has a get, but can also be set; see also np.shape(x)

x.<tab>在 Ipython 中向我展示了 ndarray 的所有补全。 .有4*18。有些是方法,有些是属性。 x._<tab>显示更多以 __ 开头的内容.这些是“私有(private)的”——不适合公共(public)消费,但这只是语义。您可以查看它们并在需要时使用它们。

副手 x.shape是唯一ndarray我设置的属性,甚至我通常使用 reshape(...)反而。阅读他们的文档以了解不同之处。 ndim是维数,直接改变它没有意义。是len(x.shape) ;改变形状改变ndim .同样x.size不应该是你直接改变的东西。

其中一些属性可以通过函数访问。 np.shape(x) == x.shape , 类似于 MATLAB size(x) . (MATLAB 没有 . 属性语法)。

x.__array_interface__是一个方便的属性,它给出了一个包含许多属性的字典

In [391]: x.__array_interface__
Out[391]: 
{'descr': [('', '<f8')],
 'version': 3,
 'shape': (50,),
 'typestr': '<f8',
 'strides': None,
 'data': (165646680, False)}

ndarray(shape, dtype=float, buffer=None, offset=0, strides=None, order=None) 的文档, __new__方法列出了这些属性:

`Attributes
----------
T : ndarray
    Transpose of the array.
data : buffer
    The array's elements, in memory.
dtype : dtype object
    Describes the format of the elements in the array.
flags : dict
    Dictionary containing information related to memory use, e.g.,
    'C_CONTIGUOUS', 'OWNDATA', 'WRITEABLE', etc.
flat : numpy.flatiter object
    Flattened version of the array as an iterator.  The iterator
    allows assignments, e.g., ``x.flat = 3`` (See `ndarray.flat` for
    assignment examples; TODO).
imag : ndarray
    Imaginary part of the array.
real : ndarray
    Real part of the array.
size : int
    Number of elements in the array.
itemsize : int
    The memory use of each array element in bytes.
nbytes : int
    The total number of bytes required to store the array data,
    i.e., ``itemsize * size``.
ndim : int
    The array's number of dimensions.
shape : tuple of ints
    Shape of the array.
strides : tuple of ints
    The step-size required to move from one element to the next in
    memory. For example, a contiguous ``(3, 4)`` array of type
    ``int16`` in C-order has strides ``(8, 2)``.  This implies that
    to move from element to element in memory requires jumps of 2 bytes.
    To move from row-to-row, one needs to jump 8 bytes at a time
    (``2 * 4``).
ctypes : ctypes object
    Class containing properties of the array needed for interaction
    with ctypes.
base : ndarray
    If the array is a view into another array, that array is its `base`
    (unless that array is also a view).  The `base` array is where the
    array data is actually stored.

所有这些都应该被视为属性,但我不认为 numpy实际上使用 property机制。一般来说,它们应该被认为是“只读的”。除了shape ,我只记得更改 data (指向数据缓冲区的指针)和 strides .

关于python - numpy中ndarray的 "ndim, shape, size, ..etc"的身份是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31362680/

相关文章:

python - pandas 中将字符串插入数据帧的 set_value 相当于什么

python - 多维数组的一维向量点积

python - 无法 `pip install -r requirements.txt`

r - 具有多值变量的熔化数组

php - 按值获取所有数组键

python - 将 request.user 与 Django ModelForm 一起使用

python - 您可以在不创建实例的情况下使用其他文件类中的函数吗?

python - 将训练数据的四分位数切割应用于测试数据

python - NumPy,如何有效地进行涉及数组其他元素的元素明智的操作(无循环)

c++ - 通过另一个指针删除指向类中多维数组的指针 - 如何?