Numpy narray對象的屬性分析

參考官方文檔連接:html

narray是Numpy的基本數據結構,本文主要分析對象的屬性(可經過.進行訪問)數組

1:導入numpy:數據結構

import numpy as np

2:初始化narray對象:app

>>> x = np.array([[1, 2, 3], [4, 5, 6]], np.int32)
>>> x
array([[1, 2, 3],
       [4, 5, 6]], dtype=int32)

3:查看np對象的行列sharp(np.shape)(返回兩個元素元組,分別是行,列.):ide

>>> x.shape
(2, 3)

4:查看np對象的內存佈局(np.flags)(詳情點這裏):佈局

>>> x.flags
  C_CONTIGUOUS : True              :The data is in a single, C-style contiguous segment.
  F_CONTIGUOUS : False             :The data is in a single, Fortran-style contiguous segment.
  OWNDATA : True                   :The array owns the memory it uses or borrows it from another object.
  WRITEABLE : True                 :The data area can be written to.
  ALIGNED : True                   :The data and all elements are aligned appropriately for the hardware.
  UPDATEIFCOPY : False             :(Deprecated, use WRITEBACKIFCOPY) This array is a copy of some other array. When this array is deallocated, the base array will be updated with the contents of this array.

5:查看數組的大小:(np.size)(即全部元素個數Number of elements in the array.):this

>>> x.size
6

6:遍歷數組時,在每一個維度中步進的字節數組(np.strides)(Tuple of bytes to step in each dimension when traversing an array.):spa

>>> x
array([[1, 2, 3],
       [4, 5, 6]], dtype=int32)
>>> x.strides
(12, 4)
以本片代碼爲例:int32位佔據4個字節的數據,所以同行內移動一個數據至相鄰的列須要4個字節,移動到下一行相同列須要(元素大小4*列數3)12個字節
>>> x = np.array([[1, 2, 3], [4, 5, 6]], np.int64)
>>> x.strides
(24, 8)

7:查看數組維度(np.ndim)(Number of array dimensions.):code

>>> x.ndim
2

8:查看數組內存緩衝區的開始位置(np.data)(Python buffer object pointing to the start of the array’s data.):htm

>>> x.data
<memory at 0x7f49c189a990>

9:查看數組每個元素所佔的內存大小(np.itemsize)(Length of one array element in bytes.):

>>> x = np.array([1, 2], np.complex128)
>>> x.itemsize
16
>>> x = np.array([1, 2], np.int16)
>>> x.itemsize

10:查看數組元素消耗的總字節(np.nbytes)(Total bytes consumed by the elements of the array.):

>>> x = np.array([1, 2], np.int16)
>>> x.nbytes
4

11:查看數組的基對象(np.base)(Base object if memory is from some other object.)

>>> x = np.array([[1, 2, 3], [4, 5, 6]], np.int64)
>>> x.base
>>> y = x[1:]     (分片後的對象與原對象共享內存)
>>> y.base
array([[1, 2, 3],
       [4, 5, 6]])

 

請以官方文檔爲準,有問題能夠留言,

相關文章
相關標籤/搜索