NumPy的數據處理
由於Python中list中的元素爲對象指針,因此對於一個簡單數列[1,2,3],就需要3個指針和3個對象來存儲,這樣比較浪費空間。而Python中的array模塊,提供的array對象和list不同,其元素爲數據對象本身,這和c語言的一維數組類似。但是其不支持多維數組和計算,因此一般不用其做數值計算。
NumPy提供了兩種基本對象:ndarray(n-dimensional array object) 和 ufunc(universal function objext)。ndarray爲存儲單一數據類型的多維數組;ufunc爲能對數組進行處理的函數。
1. ndarray對象
1.1 屬性shape
數組array有個屬性shape,可以通過shape改變當前數組的屬性,這個屬性值是一個元組(tuple)類型的,描述的是數組個軸的長度。當設置某個軸的元素個數爲-1時,將自動計算此軸的長度。
import numpy as np
a=np.array([1,2,3,4,5,6])
print(a)
print(a.shape)
運行結果:
[1 2 3 4 5 6]
(6,)
改變數組a的形狀屬性。
a.shape = 2,3
print(a)
a.shape =(3,-1)
print(a)
a.shape = (6,)
print(a)
運行結果:
[[1 2 3]
[4 5 6]]
[[1 2]
[3 4]
[5 6]]
[1 2 3 4 5 6]
使用reshape()創建一個新的指定形狀的數組,而原數組的形狀保持不變。注意:新建的數組與原數組只是形狀不同,但是兩個數組a和b是共享數據存儲空間的,也就是說改變其中任何一個的數據,另一個的相應位置的數據也會改變。
b = a.reshape((3,2))
print(a)
print(b)
運行結果:
[1 2 3 4 5 6]
[[1 2]
[3 4]
[5 6]]
改變b的數據,看a數組的數據是否改變。
b[0,1] = 99
print(b)
print(a)
運行結果:
[[ 1 99]
[ 3 4]
[ 5 6]]
[ 1 99 3 4 5 6]
1.2 屬性dtype
數組元素的數據類型可以通過dtype屬性獲得,也可在創建數組時,通過dtype屬性指定數據類型。
顯示b的數據類型:
>>>b.dtype
dtype('int32')
指定數據類型,創建數組:
fd = np.array([1,2,3], dtype=np.float)
print(fd)
cd = np.array([1,2,3], dtype=np.complex)
print(cd)
結果:
[1. 2. 3.]
[1.+0.j 2.+0.j 3.+0.j]
用dtype強制轉換數據類型: 把float64轉換爲uint64,只是把內存中的數據以uint64格式表示,並沒有將float類型的數值轉換爲int類型數值。 用double類型重新表示float類型。
print(fd.dtype)
fd.dtype = np.uint64
print(fd)
fd.dtype = np.double
print(fd)
結果:
float64
[4607182418800017408 4611686018427387904 4613937818241073152]
[1. 2. 3.]
NumPy中的數據類型可以用typeDict查看:
>>>np.typeDict
{'?': numpy.bool_,
0: numpy.bool_,
'byte': numpy.int8,
'b': numpy.int8,
1: numpy.int8,
'ubyte': numpy.uint8,
'B': numpy.uint8,
2: numpy.uint8,
'short': numpy.int16,
'h': numpy.int16,
3: numpy.int16,
'ushort': numpy.uint16,
'H': numpy.uint16,
4: numpy.uint16,
'i': numpy.int32,
5: numpy.int32,
'uint': numpy.uint32,
'I': numpy.uint32,
6: numpy.uint32,
'intp': numpy.int64,
'p': numpy.int64,
.......
.......
'bytes': numpy.bytes_,
'a': numpy.bytes_}
用set去掉重複項:
>>>set(np.typeDict.values())
{numpy.bool_,
numpy.bytes_,
numpy.complex128,
numpy.complex128,
numpy.complex64,
numpy.datetime64,
numpy.float16,
numpy.float32,
numpy.float64,
numpy.float64,
numpy.int16,
numpy.int32,
numpy.int32,
numpy.int64,
numpy.int8,
numpy.object_,
numpy.str_,
numpy.timedelta64,
numpy.uint16,
numpy.uint32,
numpy.uint32,
numpy.uint64,
numpy.uint8,
numpy.void}
1.3 創建數組
函數 | 作用 |
---|---|
arange(start, end, step) | 通過指定開始值、終值和步長,創建一個形式爲等差數列的一維數組 。 注意:數組值不包含終值 |
linspace(start, end, num) | 通過指定開始值、終值和元素個數,創建一個形式爲等差數列的一維數組 。 注意:可以通過設置參數 endpoint 來指定數組值是否包含終值,endpoint的默認值爲True |
logspace(start, end, num) | 通過指定開始值、終值和元素個數,創建一個形式爲等比數列的一維數組 。 注意:可以通過設置參數 endpoint 來指定數組值是否包含終值,endpoint的默認值爲True;通過 base 參數指定基數,默認值爲10. |
zeros(shape, dtype) | 通過指定形狀和類型 ,創建一個值爲初始化值0的數組。 注意:默認類型爲np.float64. |
ones(shape, dtype) | 通過指定形狀和類型 ,創建一個值爲初始化值1的數組。 注意:默認類型爲np.float64. |
empty(shape, dtype) | 通過指定形狀和類型 ,創建的數組只分配內存空間,不初始化值。 注意:默認類型爲np.float64. |
eye(N, M=None, k=0, dtype=<class ‘float’>, order=‘C’) | 創建一個1對角陣,可爲非方陣。 Parameters: N : int,=Number of rows in the output; M : int, optional =Number of columns in the output. If None, defaults to N; k : int, optional =Index of the diagonal: 0 (the default) refers to the main diagonal, a positive value refers to an upper diagonal, and a negative value to a lower diagonal; dtype : data-type, optional =Data-type of the returned array; order : {‘C’, ‘F’}, optional =Whether the output should be stored in row-major (C-style) or column-major (Fortran-style) order in memory. |
identity(n, dtype=None) | 創建一個1對角陣,方陣。 n : int =Number of rows (and columns) in n x n output;dtype : data-type, optional =Data-type of the output. Defaults to float. |
mgrid,ogrid | mgrid生成網格矩陣,輸出值其實是兩個矩陣,一個表示y軸的劃分,一個表示x軸的劃分;ogrid生成列向量和行向量,也是網格的值。 |
對角單位矩陣:
>>> np.eye(2, dtype=int)
array([[1, 0],
[0, 1]])
>>> np.eye(3, k=1)
array([[0., 1., 0.],
[0., 0., 1.],
[0., 0., 0.]])
>>> np.identity(3)
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
網格生成:
>>> np.mgrid[0:5,0:5]
array([[[0, 0, 0, 0, 0],
[1, 1, 1, 1, 1],
[2, 2, 2, 2, 2],
[3, 3, 3, 3, 3],
[4, 4, 4, 4, 4]],
[[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4]]])
>>> np.ogrid[0:5,0:5]
[array([[0],
[1],
[2],
[3],
[4]]), array([[0, 1, 2, 3, 4]])]