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
。
對於問題的第一部分,您最好閱讀官方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代碼。 –
在附註中,您可以設置'ndarray.shape',它只是一個可行的形狀,否則會引發錯誤。 –