2015-07-11 65 views
3

我對Python很陌生。nndary中ndarray的「ndim,shape,size,..etc」的身份是什麼

利用Matlab很多年之後,最近,我開始學習numpy的/ SciPy的

好像numpy的最基本的要素似乎是ndarray。 在ndarray,有以下屬性:

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

我很熟悉C++/Java類,但我是Python OOP的新手。


問題1:我的第一個問題是上述屬性的標識是什麼?

起初,我認爲上述屬性可能是公共成員變量。但很快,我發現a.ndim = 10不起作用(假設andarray的一個對象)所以,它似乎不是一個公共成員變量。

接下來,我猜他們可能是類似於C++中的getter方法的公共方法。但是,當我用括號嘗試a.nidm()時,它不起作用。所以,它似乎不是一個公開的方法。

另一種可能性可能是它們是私有成員變量,但是打印a.ndim的作品,所以它們不能是私有數據成員。

因此,我無法弄清楚上述屬性的真實身份。


Q2。我在哪裏可以找到ndarray的Python代碼實現?由於我在本地PC上安裝了numpy/scipy,我猜可能有一些方法可以查看源代碼,然後我認爲一切都可以清楚。

你能提供一些建議嗎?

+0

對於問題的第一部分,您最好閱讀官方numpy文檔中的'基礎'部分(http://docs.scipy.org/doc/numpy/user/basics.html)。關於第二部分,出於性能原因,'numpy.ndarray'主要用C而不是Python來實現。你可以在numpy的GitHub倉庫[這裏](https://github.com/numpy/numpy/tree/master/numpy/core/src/multiarray)找到與'ndarray'有關的大部分C代碼。 –

+0

在附註中,您可以設置'ndarray.shape',它只是一個可行的形狀,否則會引發錯誤。 –

回答

1

問題1

你提的列表是一個包含了NumPy的數組屬性。

例如:

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

由於a是你能夠利用這些屬性來了解更多關於它的nump.ndarray。 (即a.size將導致3)。要獲得有關每個人所做的操作的信息,請訪問SciPy's有關這些屬性的文檔。

問題2

你可以開始here讓自己熟悉一些與NumPy的基本工具,以及在Reference Manual假設你使用1.9版。有關Numpy Array的具體信息,您可以訪問Array Objects

他們的文檔非常廣泛,非常有幫助。整個網站提供了多個示例的示例。

3

關於你的第一個問題,Python有properties的語法糖,包括獲取,設置,刪除它們的細粒度控制,以及限制上述任何一項。

因此,舉例來說,如果你有

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

,那麼你可以寫Foo().shmip獲得3,但是,如果是這樣的類定義,您已經停用設置Foo().shmip = 4

換句話說,那些是隻讀屬性。

5

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 writen(如MATLAB /八度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) 

在IPython中x.<tab>展示我所有的落成爲ndarray。有4 * 18,有些是方法,有的是att ributes。 x._<tab>顯示了更多以__開頭的羣組。這些是'私人' - 不是爲了公共消費,而只是語義。你可以看看它們並在需要時使用它們。

副手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

相關問題