简体   繁体   English

在numpy中ndarray的“ndim,shape,size,.. etc”的标识是什么?

[英]What is the identity of “ndim, shape, size, ..etc” of ndarray in numpy

I'm quite new at Python. 我是Python的新手。

After using Matlab for many many years, recently, I started studying numpy/scipy 在使用Matlab多年之后,最近,我开始研究numpy / scipy

It seems like the most basic element of numpy seems to be ndarray . 似乎numpy最基本的元素似乎是ndarray In ndarray, there are following attributes: 在ndarray中,有以下属性:

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

I'm quite familiar with C++/JAVA classes, but I'm a novice at Python OOP. 我对C ++ / JAVA类非常熟悉,但我是Python OOP的新手。


Q1: My first question is what is the identity of the above attributes? Q1:我的第一个问题是上述属性的身份是什么?

At first, I assumed that the above attribute might be public member variables. 首先,我假设上面的属性可能是公共成员变量。 But soon, I found that a.ndim = 10 doesn't work (assuming a is an object of ndarray ) So, it seems it is not a public member variable. 但很快,我发现a.ndim = 10不起作用(假设andarray的对象)所以,它似乎不是一个公共成员变量。

Next, I guessed that they might be public methods similar to getter methods in C++. 接下来,我猜测它们可能是类似于C ++中的getter方法的公共方法。 However, when I tried a.nidm() with a parenthesis, it doesn't work. 但是,当我尝试带括号的a.nidm()时,它不起作用。 So, it seems that it is not a public method. 所以,它似乎不是一种公共方法。

The other possibility might be that they are private member variables, but but print a.ndim works, so they cannot be private data members. 另一种可能是它们是私有成员变量,但是打印a.ndim有效,所以它们不能是私有数据成员。

So, I cannot figure out what is the true identity of the above attributes. 所以,我无法弄清楚上述属性的真实身份是什么。


Q2. Q2。 Where I can find the Python code implementation of ndarray ? 我在哪里可以找到ndarray的Python代码实现? Since I installed numpy/scipy on my local PC, I guess there might be some ways to look at the source code, then I think everything might be clear. 由于我在我的本地PC上安装了numpy / scipy,我想可能有一些方法来查看源代码,然后我认为一切都可能很清楚。

Could you give some advice on this? 你能就此提出一些建议吗?

numpy is implemented as a mix of C code and Python code. numpy是作为C代码和Python代码的混合实现的。 The source is available for browsing on github , and can be downloaded as a git repository. 源可以在github浏览,可以作为git存储库下载。 But digging your way into the C source takes some work. 但是挖掘你的C源代码需要一些工作。 A lot of the files are marked as .c.src , which means they pass through one or more layers of perprocessing before compiling. 许多文件都标记为.c.src ,这意味着它们在编译之前会通过一个或多个.c.src层。

And Python is written in a mix of C and Python as well. Python也是用C和Python混合编写的。 So don't try to force things into C++ terms. 所以不要试图强迫事物进入C ++术语。

It's probably better to draw on your MATLAB experience, with adjustments to allow for Python. 最好利用您的MATLAB经验,调整以允许Python。 And numpy has a number of quirks that go beyond Python. numpy有许多超越Python的怪癖。 It is using Python syntax, but because it has its own C code, it isn't simply a Python class. 它使用的是Python语法,但由于它有自己的C代码,因此它不仅仅是一个Python类。

I use Ipython as my usual working environment. 我使用Ipython作为我通常的工作环境。 With that I can use foo? 有了这个我可以使用foo? to see the documentation for foo (same as the Python help(foo) , and foo?? to see the code - if it is writen in Python (like the MATLAB/Octave type(foo) ) 查看foo的文档(与Python help(foo)foo??查看代码 - 如果它是用Python编写的(如MATLAB / Octave type(foo)

Python objects have attributes, and methods. Python对象具有属性和方法。 Also properties which look like attributes, but actually use methods to get/set. 也是properties看起来像属性,但实际上使用方法来获取/设置。 Usually you don't need to be aware of the difference between attributes and 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> in Ipython shows me all the completions for a ndarray . x.Ipython中的x.<tab>显示了ndarray所有完成。 There are 4*18. 有4 * 18。 Some are methods, some attributes. 一些是方法,一些属性。 x._<tab> shows a bunch more that start with __ . x._<tab>显示了一些以__开头的更多内容。 These are 'private' - not meant for public consumption, but that's just semantics. 这些都是“私人” - 不是为了公共消费,而是仅仅是语义。 You can look at them and use them if needed. 您可以查看它们并在需要时使用它们。

Off hand x.shape is the only ndarray property that I set, and even with that I usually use reshape(...) instead. off hand x.shape是我设置的唯一ndarray属性,即便如此,我通常使用reshape(...)代替。 Read their docs to see the difference. 阅读他们的文档,看看差异。 ndim is the number of dimensions, and it doesn't make sense to change that directly. ndim是维度的数量,直接更改它是没有意义的。 It is len(x.shape) ; 它是len(x.shape) ; change the shape to change ndim . 更改形状以更改ndim Likewise x.size shouldn't be something you change directly. 同样, x.size不应该是你直接改变的东西。

Some of these properties are accessible via functions. 其中一些属性可通过函数访问。 np.shape(x) == x.shape , similar to MATLAB size(x) . np.shape(x) == x.shape ,类似于MATLAB size(x) (MATLAB doesn't have . attribute syntax). (MATLAB没有.属性语法)。

x.__array_interface__ is a handy property, that gives a dictionary with a number of the attributes x.__array_interface__是一个方便的属性,它给出了一个包含许多属性的字典

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

The docs for ndarray(shape, dtype=float, buffer=None, offset=0, strides=None, order=None) , the __new__ method lists these attributes: ndarray(shape, dtype=float, buffer=None, offset=0, strides=None, order=None)的文档ndarray(shape, dtype=float, buffer=None, offset=0, strides=None, order=None) __new__ 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.

All of these should be treated as properties, though I don't think numpy actually uses the property mechanism. 所有这些应该被视为属性,但我不认为numpy实际上使用property机制。 In general they should be considered to be 'read-only'. 一般来说,它们应被视为“只读”。 Besides shape , I only recall changing data (pointer to a data buffer), and strides . 除了shape ,我只记得改变data (指向数据缓冲区的指针)和strides

Regarding your first question, Python has syntactic sugar for properties , including fine-grained control of getting, setting, deleting them, as well as restricting any of the above. 关于你的第一个问题,Python有属性的语法糖,包括获取,设置,删除它们的细粒度控制,以及限制上述任何一个。

So, for example, if you have 所以,例如,如果你有

class Foo(object):
    @property
    def shmip(self):
        return 3

then you can write Foo().shmip to obtain 3 , but, if that is the class definition, you've disabled setting Foo().shmip = 4 . 那么你可以写Foo().shmip来获得3 ,但是,如果那是类定义,你就禁用了设置Foo().shmip = 4

In other words, those are read-only properties. 换句话说,那些是只读属性。

Question 1 问题1

The list you're mentioning is one that contains attributes for a Numpy array. 您提到的列表是包含Numpy数组的属性的列表。

For example: 例如:

a = np.array([1, 2, 3])
print(type(a))
>><class 'numpy.ndarray'>

Since a is an nump.ndarray you're able to use those attributes to find out more about it. 由于a是nump.ndarray,你可以使用这些属性来了解更多信息。 (ie a.size will result in 3). (即a.size将导致3)。 To get information about what each one does visit SciPy's documentation about the attributes. 要获取每个人的信息,请访问SciPy关于属性文档。

Question 2 问题2

You can start here to get yourself familiar with some of the basics tools of Numpy as well as the Reference Manual assuming you're using v1.9. 您可以从这里开始熟悉Numpy的一些基础工具以及参考手册,假设您使用的是v1.9。 For information specific to Numpy Array you can go to Array Objects . 有关Numpy Array的特定信息,您可以转到Array Objects

Their documentation is very extensive and very helpful. 他们的文档非常广泛且非常有用。 Examples are provided throughout the website showing multiple examples. 整个网站提供了示例,显示了多个示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM