文章目錄
NumPy 數組的維數稱爲秩 (rank),維度稱爲軸 (axis) ,一維數組的秩爲 1,二維數組的秩爲 2,以此類推。
在 NumPy中,軸 (axis) 一般是在超過一維數組時作用更大的屬性:
- 一維數組時,axis=0
- 二維數組時,axis=0, 1
- N 維數組時,axis=0, 1, …, N
當對某一軸進行操作時,我們可以這樣看待:
- 一維數組,axis=0 表示沿着第 0 軸進行操作,即跨列操作;
- 二維數組,axis=0 表示沿着第 0 軸進行操作,即跨行操作;axis=1 表示沿着第 1 軸進行操作,即跨列操作
- 三維數組,axis=0 表示沿着第 0 軸進行操作,即跨表操作;axis=1 表示沿着第 1 軸進行操作,即跨行操作;axis=2 表示沿着第 2 軸進行操作,即跨列操作
ndarray屬性表
屬性 | 說明 |
---|---|
ndarray.ndim | 秩 (rank),即數組的軸(維度)的個數 |
ndarray.shape | 數組形狀,返回一個元組表示各個維度中元素的個數 |
ndarray.size | 數組元素的總數,相當於np.prod(ndarray.shape) |
ndarray.dtype | 數組中每個元素的數據類型和數據 (bit) 大小 |
ndarray.itemsize | 數組中每個元素的字節 (byte) 大小,1 byte=8 bit |
ndarray.flags | 數組的內存信息 |
ndarray.real | 數組元素的實部 |
ndarray.imag | 數組元素的虛部 |
ndarray.ndim
ndarray.ndim
返回秩 (rank),即數組的軸(維度)的個數
import numpy as np
a = np.arange(24)
print (a.ndim) # a 現只有一個維度
# 現在調整其大小
b = a.reshape(2,4,3) # b 現在擁有三個維度
print (b.ndim)
# 輸出
1
3
ndarray.shape
ndarray.shape
數組形狀,返回一個元組表示各個維度中元素的個數。比如,一個二維數組,其數組形狀爲 (行數, 列數)
import numpy as np
a=np.array([[1,2,3],[4,5,6]])
print(a.shape)
# 輸出
(2, 3)
ndarray.shape
也可以用於調整數組結構,但要保證元素的總個數 (size) 不能變
import numpy as np
a=np.array([[1,2,3],[4,5,6]])
a.shape=(3,2)
print(a)
# 輸出
[[1 2]
[3 4]
[5 6]]
ndarray.reshape(shape)
也可以使用 ndarray.reshape()
函數來調整數組結構,但要保證元素的總個數 (size) 不能變,會創建一個新對象,不會改變原對象
import numpy as np
a=np.array([[1,2,3],[4,5,6]])
b=a.reshape(3,2)
print (b)
# 輸出
[[1, 2]
[3, 4]
[5, 6]]
ndarray.size
ndarray.size
返回數組元素的總數,相當於np.prod(ndarray.shape)
import numpy as np
x=np.zeros((3,5,2),dtype=np.complex128)
print(x.size)
print(np.prod(x.shape))
# 輸出
30
30
ndarray.dtype
ndarray.dtype
返回數組中每個元素的數據類型和數據 (bit) 大小
# 查詢dtype
arr1=np.array([1,2,3],dtype=np.float64)
print(arr1.dtype)
# 輸出
float64
ndarray.astype(dtype)
ndarray.astype(dtype)
轉換數組的數據類型,會創建一個新對象,不會改變原對象
# 浮點數數組轉換整數數組,小數點後的部分將被省略
arr1=np.array([3.7,-1.2,-2.6,0.5,12.9,10.1])
print(arr1)
print(arr1.dtype)
int_arr1=arr1.astype(np.int32)
print(int_arr1)
print(int_arr1.dtype)
# 直接使用另一個數組的類型
arr2=np.array([1,2,3,4,5])
astype_arr=arr1.astype(arr2.dtype)
print(astype_arr)
# 輸出
[ 3.7 -1.2 -2.6 0.5 12.9 10.1]
float64
[ 3 -1 -2 0 12 10]
int32
[ 3 -1 -2 0 12 10]
# 把浮點數含義的Unicode字符串轉換爲浮點數(注:不能直接轉換爲整型)
num_strings=np.array([1.25,-9.6,42],dtype=np.str)
print(num_strings)
print(num_strings.dtype)
num_floats=num_strings.astype(np.float)
print(num_floats)
print(num_floats.dtype)
# 輸出
['1.25' '-9.6' '42']
<U4
[ 1.25 -9.6 42. ]
float64
ndarray.itemsize
ndarray.itemsize
返回數組中每個元素的字節 (byte) 大小
例如,一個數據類型爲 float64 的數組 itemsize 屬性爲 8 (float64 佔用 64 個 bits,每個字節長度爲 8,所以 64/8,佔用 8 個字節);又如,一個數據類型爲 complex32 的數組 itemsize 屬性爲 4(32/8)。
import numpy as np
# x數組的dtype爲int8(一個字節)
x = np.array([1,2,3,4,5], dtype = np.int8)
print (x.itemsize)
# y數組的dtype爲float64(八個字節)
y = np.array([1,2,3,4,5], dtype = np.float64)
print (y.itemsize)
# 輸出
1
8
ndarray.flags
ndarray.flags
返回數組的內存信息,包含以下屬性:
屬性 | 描述 |
---|---|
C_CONTIGUOUS © | 數據是在一個單一的C風格的連續段中 |
F_CONTIGUOUS (F) | 數據是在一個單一的Fortran風格的連續段中 |
OWNDATA (O) | 數組擁有它所使用的內存或從另一個對象中借用它 |
WRITEABLE (W) | 數據區域可以被寫入,將該值設置爲 False,則數據爲只讀 |
ALIGNED (A) | 數據和所有元素都適當地對齊到硬件上 |
WRITEBACKIFCOPY(X) | 該數組是其他數組的副本。 必須先調用C-API函數 PyArray_ResolveWritebackIfCopy,然後再使用該數組的內容更新分配給基本數組的內容。 |
UPDATEIFCOPY (U) | 這個數組是其它數組的一個副本,當這個數組被釋放時,原數組的內容將被更新 |
import numpy as np
x = np.array([1,2,3,4,5])
print (x.flags)
# 輸出
C_CONTIGUOUS : True
F_CONTIGUOUS : True
OWNDATA : True
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False
ndarray.real
ndarray.real
返回數組元素的實部
arr2=[1+2j,3+4j,5+6j]
complex_arr=np.array(arr2)
print(complex_arr.dtype)
print(complex_arr.real)
# 輸出
complex128
[1. 3. 5.]
ndarray.imag
ndarray.imag
返回數組元素的虛部
arr2=[1+2j,3+4j,5+6j]
complex_arr=np.array(arr2)
print(complex_arr.dtype)
print(complex_arr.imag)
# 輸出
complex128
[2. 4. 6.]