?
?
?
?
In?[227]:import numpy as npIn?[?]:
# NumPy是Python中科學(xué)計算的基礎(chǔ)軟件包。# 它是一個提供多了維數(shù)組對象,多種派生對象(如:掩碼數(shù)組、矩陣)以及用于快速操作數(shù)組的函數(shù)及API,# 它包括數(shù)學(xué)、邏輯、數(shù)組形狀變換、排序、選擇、I/O 、離散傅立葉變換、基本線性代數(shù)、基本統(tǒng)計運(yùn)算、隨機(jī)模擬等等。# NumPy包的核心是ndarray對象。# 它封裝了python原生的同數(shù)據(jù)類型的n維數(shù)組,為了保證其性能優(yōu)良,其中有許多操作都是代碼在本地進(jìn)行編譯后執(zhí)行的。# NumPy數(shù)組 和 標(biāo)準(zhǔn)Python Array(數(shù)組) 之間 的區(qū)別 # NumPy數(shù)組在創(chuàng)建時具有固定的大小,與Python的原生數(shù)組對象(可以動態(tài)增長)不同。 更改ndarray的大小將創(chuàng)建一個新數(shù)組并刪除原來的數(shù)組。 # NumPy數(shù)組中的元素都需要具有相同的數(shù)據(jù)類型,因此在內(nèi)存中的大小相同。 例外情況:Python的原生數(shù)組里包含了NumPy的對象的時候, # 這種情況下就允許不同大小元素的數(shù)組。# 安裝# python -m pip install --user numpy scipy matplotlib ipython jupyter pandas sympy noseIn?[2]:
# NumPy的主要對象是同類型的多維數(shù)組。它是一張表,所有元素(通常是數(shù)字)的類型都相同,并通過正整數(shù)元組索引。在NumPy中,維度稱為軸。# 軸的數(shù)目為rank。# [1, 2, 1] 是rank為1的數(shù)組,因為它具有一個軸。該軸的長度為3t1 = [[ 1., 0., 0.],[ 0., 1., 2.]]# t1 有兩個軸 第一個軸(維度)的長度為2,第二個軸(維度)的長度為3。In?[3]:
# 主要屬性# ndarray.ndim:數(shù)組的軸(維度)的個數(shù)。在Python世界中,維度的數(shù)量被稱為rank。# ndarray.shape:數(shù)組的維度。這是一個整數(shù)的元組,表示每個維度中數(shù)組的大小。對于有n行和m列的矩陣,shape將是(n,m)。因此,# shape元組的長度就是rank或維度的個數(shù) ndim。# ndarray.size:數(shù)組元素的總數(shù)。這等于shape的元素的乘積。# ndarray.dtype:一個描述數(shù)組中元素類型的對象??梢允褂脴?biāo)準(zhǔn)的Python類型創(chuàng)建或指定dtype。另外NumPy提供它自己的類型。# 例如numpy.int32、numpy.int16和numpy.float64。# ndarray.itemsize:數(shù)組中每個元素的字節(jié)大小。例如,元素為 float64 類型的數(shù)組的 itemsize 為8(=64/8),而 complex32 類型的數(shù)組的# itemsize 為4(=32/8)。它等于 ndarray.dtype.itemsize 。# ndarray.data:該緩沖區(qū)包含數(shù)組的實際元素。通常,我們不需要使用此屬性,因為我們將使用索引訪問數(shù)組中的元素。In?[50]:
# 創(chuàng)建數(shù)組In?[228]:
# 1. 用普通的python列表創(chuàng)建數(shù)組a1 = np.array([1,2,3])print(a1.ndim)print(a1.dtype)print(a1.size)print(a1.shape)a1?
2int323(1, 3)Out[228]:
array([[1, 2, 3]])In?[9]:
a2 = a1 = np.array([[1,2,3], [3,4,5]], dtype='int')a2Out[9]:
array([[1, 2, 3], [3, 4, 5]])In?[12]:
print(a2.ndim)print(a2.dtype)print(a2.size)print(a2.shape)?
2int326(2, 3)In?[?]:
# 用 nunpy 內(nèi)置的方法來創(chuàng)建數(shù)組In?[18]:
#np.zeros(shape, dtype=None, order='C')#創(chuàng)建shape是3x5的全1(默認(rèn)浮點型)數(shù)組a3= np.zeros((3,5), order='C')a3Out[18]:
array([[0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.]])In?[17]:
#np.zeros(shape, dtype=None, order='C')#創(chuàng)建shape是3x5的全1(默認(rèn)浮點型)數(shù)組a4 = np.ones((3,5))a4Out[17]:
array([[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]])In?[19]:
#np.eye(N, M=None, k=0, dtype=<class 'float'>, order='C')#創(chuàng)建一個形狀為 N x N 的單位矩陣(二維數(shù)組)a5 = np.eye(3)a5Out[19]:
array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]])In?[21]:
#empty(shape, dtype=float, order='C')#創(chuàng)建一個形狀為shape的未初始化數(shù)組,值為隨機(jī)值a6 =np.empty(5, dtype='int')Out[21]:
array([0, 0, 0, 0, 0])In?[23]:
a7 = np.empty((3,4))a7Out[23]:
array([[1.50665937e-312, 0.00000000e 000, 8.76794447e 252, 2.15895723e 227], [6.48224638e 170, 3.67145870e 228, 7.58253645e-096, 9.03292329e 271], [9.08366793e 223, 1.41075687e 232, 1.16070543e-028, 1.54730878e 295]])In?[34]:
#np.full(shape, fill_value, dtype=None, order='C')#創(chuàng)建shape是3x5的(默認(rèn)浮點型)數(shù)組,值fill_value都是3.14a7 = np.full((3,5),5)a7Out[34]:
array([[5, 5, 5, 5, 5], [5, 5, 5, 5, 5], [5, 5, 5, 5, 5]])In?[35]:
# 使用 arange 來創(chuàng)建# arange([start,] stop[, step,], dtype=None)a8 = np.arange(0,20,5)a8Out[35]:
array([ 0, 5, 10, 15])In?[230]:
# 使用linspace 來創(chuàng)建# np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)a9= np.linspace(10,20,6)a9Out[230]:
array([10., 12., 14., 16., 18., 20.])In?[233]:
# reshapea10 = a9.reshape(2,3)a10Out[233]:
array([[10., 12., 14.], [16., 18., 20.]])In?[51]:
# 形狀 和操作In?[236]:
aa = np.arange(20).reshape(4,5)aaOut[236]:
array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19]])In?[237]:
print(aa.shape)print(aa.size)?
(4, 5)20In?[43]:
aa.reshape(5,4) # 不改變數(shù)組本身Out[43]:
array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19]])In?[44]:
aaOut[44]:
array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19]])In?[45]:
aa.ravel() # 不改變數(shù)組本身Out[45]:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19])In?[46]:
aaOut[46]:
array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19]])In?[238]:
aaOut[238]:
array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19]])In?[239]:
aa.resize(5,4) # 改變數(shù)組本身In?[240]:
aaOut[240]:
array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19]])In?[241]:
aa.reshape(5,-1) #reshape操作中將維度指定為-1,則會自動計算其他維度Out[241]:
array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19]])In?[52]:
# 將不同的數(shù)組堆疊在一起In?[243]:
aa = np.floor(10*np.random.random((2,2)))aaOut[243]:
array([[1., 8.], [4., 4.]])In?[244]:
bb = np.floor(10*np.random.random((2,2)))bbOut[244]:
array([[7., 0.], [9., 0.]])In?[245]:
xx = np.vstack((aa,bb))xxOut[245]:
array([[1., 8.], [4., 4.], [7., 0.], [9., 0.]])In?[246]:
np.hstack((aa, bb))Out[246]:
array([[1., 8., 7., 0.], [4., 4., 9., 0.]])In?[60]:
# numpy 切片和索引In?[61]:
a = np.arange(10)aOut[61]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])In?[67]:
s = slice(2,7,2)print(a[s])?
[2 4 6]In?[68]:
b = a[2:7:2]bOut[68]:
array([2, 4, 6])In?[250]:
aa = np.floor(10*np.random.random((2,12)))aaOut[250]:
array([[2., 2., 3., 9., 7., 8., 4., 6., 9., 3., 8., 6.], [1., 7., 4., 3., 2., 2., 0., 7., 3., 6., 3., 7.]])In?[258]:
aaOut[258]:
array([[2., 2., 3., 9., 7., 8., 4., 6., 9., 3., 8., 6.], [1., 7., 4., 3., 2., 2., 0., 7., 3., 6., 3., 7.]])In?[259]:
np.hsplit(aa,3) # 平均分成3份Out[259]:
[array([[2., 2., 3., 9.], [1., 7., 4., 3.]]), array([[7., 8., 4., 6.], [2., 2., 0., 7.]]), array([[9., 3., 8., 6.], [3., 6., 3., 7.]])]In?[260]:
np.hsplit(aa,(3,4)) # 0-3 3-4 4-Out[260]:
[array([[2., 2., 3.], [1., 7., 4.]]), array([[9.], [3.]]), array([[7., 8., 4., 6., 9., 3., 8., 6.], [2., 2., 0., 7., 3., 6., 3., 7.]])]In?[73]:
# 分割 aa = np.random.randint(1, 50,size=(7,7))aaOut[73]:
array([[ 9, 23, 26, 3, 14, 18, 38], [14, 32, 24, 27, 47, 34, 7], [31, 23, 42, 21, 39, 12, 46], [32, 32, 42, 44, 24, 33, 19], [49, 37, 25, 27, 5, 30, 11], [40, 32, 40, 40, 35, 24, 9], [41, 8, 47, 7, 43, 3, 48]])In?[74]:
# np.split(arr,index,axis)# arr 為被切分的數(shù)組# index(整數(shù)或者數(shù)組) # 如果是一個整數(shù) 就把當(dāng)前數(shù)組平均分成index份,如果數(shù)組的size不能被index整除則報錯, # 如果為一個數(shù)組,則沿的數(shù)組的軸線進(jìn)行切分,比如:[2, 3] 則會被切割成[:2],[2:3],[3:]# axis 為切分的方向 默認(rèn)為0 橫向 1為豎向np.split(aa, [2,4,6], axis=1)Out[74]:
[array([[ 9, 23], [14, 32], [31, 23], [32, 32], [49, 37], [40, 32], [41, 8]]), array([[26, 3], [24, 27], [42, 21], [42, 44], [25, 27], [40, 40], [47, 7]]), array([[14, 18], [47, 34], [39, 12], [24, 33], [ 5, 30], [35, 24], [43, 3]]), array([[38], [ 7], [46], [19], [11], [ 9], [48]])]In?[75]:
aaOut[75]:
array([[ 9, 23, 26, 3, 14, 18, 38], [14, 32, 24, 27, 47, 34, 7], [31, 23, 42, 21, 39, 12, 46], [32, 32, 42, 44, 24, 33, 19], [49, 37, 25, 27, 5, 30, 11], [40, 32, 40, 40, 35, 24, 9], [41, 8, 47, 7, 43, 3, 48]])In?[76]:
aa[5]Out[76]:
array([40, 32, 40, 40, 35, 24, 9])In?[77]:
aa[5,3]Out[77]:
40In?[78]:
aa[5:]Out[78]:
array([[40, 32, 40, 40, 35, 24, 9], [41, 8, 47, 7, 43, 3, 48]])In?[79]:
aa[:,1:2]Out[79]:
array([[23], [32], [23], [32], [37], [32], [ 8]])In?[261]:
a = np.array([[1,2,3],[3,4,5],[4,5,6]]) print(a)print("-----------------")print (a[...,1]) # 第2列元素print("-----------------")print (a[1,...]) # 第2行元素print("-----------------")print (a[...,1:]) # 第2列及剩下的所有元素# 三個點( ... )表示產(chǎn)生完整索引元組所需的冒號。例如,如果 x 是rank為的5數(shù)組(即,它具有5個軸),則# x[1,2,...] 等于 x[1,2,:,:,:]。# x[...,3] 等效于 x[:,:,:,:,3]。?
[[1 2 3] [3 4 5] [4 5 6]]-----------------[2 4 5]-----------------[3 4 5]-----------------[[2 3] [4 5] [5 6]]In?[263]:
# 整數(shù)數(shù)組索引# 實例獲取數(shù)組中(0,0),(1,1)和(2,0)位置處的元素。x = np.array([[1, 2], [3, 4], [5, 6]]) print(x)print('--------------')y = x[[0,1,2], [0,1,0]] print (y)?
[[1 2] [3 4] [5 6]]--------------[1 4 5]In?[85]:
#獲取了 4X3 數(shù)組中的四個角的元素。 行索引是 [0,0] 和 [3,3],而列索引是 [0,2] 和 [0,2]。x = np.array([[ 0, 1, 2],[ 3, 4, 5],[ 6, 7, 8],[ 9, 10, 11]]) print ('我們的數(shù)組是:' )print (x)print ('\n')rows = np.array([[0,0],[3,3]]) cols = np.array([[0,2],[0,2]]) y = x[rows,cols] print ('這個數(shù)組的四個角元素是:')print (y)?
我們的數(shù)組是:[[ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11]]這個數(shù)組的四個角元素是:[[ 0 2] [ 9 11]]In?[264]:
# 布爾索引x = np.array([[ 0, 1, 2],[ 3, 4, 5],[ 6, 7, 8],[ 9, 10, 11]]) print ('我們的數(shù)組是:')print (x)print ('\n')# 現(xiàn)在我們會打印出大于 5 的元素 print ('大于 5 的元素是:')print(x>5)print (x[x > 5])?
我們的數(shù)組是:[[ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11]]大于 5 的元素是:[[False False False] [False False False] [ True True True] [ True True True]][ 6 7 8 9 10 11]In?[86]:
a = np.array([np.nan, 1,2,np.nan,3,4,5]) print (a[~np.isnan(a)])?
[1. 2. 3. 4. 5.]In?[92]:
np.nan == np.nanOut[92]:
FalseIn?[93]:
# 花式索引# 花式索引指的是利用整數(shù)數(shù)組進(jìn)行索引。# 花式索引根據(jù)索引數(shù)組的值作為目標(biāo)數(shù)組的某個軸的下標(biāo)來取值。對于使用一維整型數(shù)組作為索引,如果目標(biāo)是一維數(shù)組,# 那么索引的結(jié)果就是對應(yīng)位置的元素;如果目標(biāo)是二維數(shù)組,那么就是對應(yīng)下標(biāo)的行。 # 花式索引跟切片不一樣,它總是將數(shù)據(jù)復(fù)制到新數(shù)組中。# 1、傳入順序索引數(shù)組In?[265]:
x=np.arange(32).reshape((8,4))print(x)print('--------------------')print (x[[4,2,1,7]])?
[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15] [16 17 18 19] [20 21 22 23] [24 25 26 27] [28 29 30 31]]--------------------[[16 17 18 19] [ 8 9 10 11] [ 4 5 6 7] [28 29 30 31]]In?[266]:
x=np.arange(32).reshape((8,4))print (x[[-4,-2,-1,-7]])?
[[16 17 18 19] [24 25 26 27] [28 29 30 31] [ 4 5 6 7]]In?[268]:
print (x[([1,5,7,2],[0,3,1,2])])?
[ 4 23 29 10]In?[270]:
print (x[np.ix_([1,5,7,2],[0,3,1,2])])?
[[ 4 7 5 6] [20 23 21 22] [28 31 29 30] [ 8 11 9 10]]In?[226]:
x = np.array([[1,2],[3,4],[5,6]])print(x)print('-----------')print(x[[0,1]]) print('-------------')print(x[[0,1],[0,1]])print('------------------')# 使用numpy.ix_()函數(shù)增強(qiáng)可讀性print (x[np.ix_([0,1],[0,1])])print('--------------')x[[0,1],[0,1]] = [0,0]print(x) # [[0,2],[3,0],[5,6]]?
[[1 2] [3 4] [5 6]]-----------[[1 2] [3 4]]-------------[1 4]------------------[[1 2] [3 4]]--------------[[0 2] [3 0] [5 6]]In?[271]:
# 迭代數(shù)組In?[273]:
# 迭代a = np.arange(6).reshape(2,3)print ('原始數(shù)組是:')print (a)print ('\n')print ('迭代輸出元素:')for x in np.nditer(a): print (x, end=", " )print ('\n')?
原始數(shù)組是:[[0 1 2] [3 4 5]]迭代輸出元素:[0 1 2], [3 4 5],In?[275]:
# for x in np.nditer(a, order='F'):Fortran order,即是列序優(yōu)先;# for x in np.nditer(a.T, order='C'):C order,即是行序優(yōu)先;a = np.arange(6).reshape(2,3)print(a)print('------------')print(a.T)for x in np.nditer(a, order ='C'): print (x, end=", " )print ('\n')?
[[0 1 2] [3 4 5]]------------[[0 3] [1 4] [2 5]]0, 1, 2, 3, 4, 5,In?[117]:
for i in a: print(i)?
[0 1 2][3 4 5]In?[123]:
# 修改數(shù)組中元素的值# nditer 對象有另一個可選參數(shù) op_flags。 默認(rèn)情況下,nditer 將視待迭代遍歷的數(shù)組為只讀對象(read-only),為了在遍歷數(shù)組的同時,# 實現(xiàn)對數(shù)組元素值得修改,必須指定 read-write 或者 write-only 的模式。a = np.arange(0,60,5) a = a.reshape(3,4) print ('原始數(shù)組是:')print (a)print ('\n')for x in np.nditer(a, op_flags=['readwrite']): x[...]=2*x print ('修改后的數(shù)組是:')print (a)?
原始數(shù)組是:[[ 0 5 10 15] [20 25 30 35] [40 45 50 55]]修改后的數(shù)組是:[[ 0 10 20 30] [ 40 50 60 70] [ 80 90 100 110]]In?[124]:
# numpy.ndarray.flat 是一個數(shù)組元素迭代器,實例如下:a = np.arange(9).reshape(3,3) print ('原始數(shù)組:')for row in a: print (row) #對數(shù)組中每個元素都進(jìn)行處理,可以使用flat屬性,該屬性是一個數(shù)組元素迭代器:print ('迭代后的數(shù)組:')for element in a.flat: print (element)?
原始數(shù)組:[0 1 2][3 4 5][6 7 8]迭代后的數(shù)組:012345678In?[125]:
# 數(shù)組操作# numpy.ndarray.flatten 返回一份數(shù)組拷貝,對拷貝所做的修改不會影響原始數(shù)組,格式如下:# ndarray.flatten(order='C')# order:'C' -- 按行,'F' -- 按列,'A' -- 原順序,'K' -- 元素在內(nèi)存中的出現(xiàn)順序。a = np.arange(8).reshape(2,4) print ('原數(shù)組:')print (a)print ('\n')# 默認(rèn)按行 print ('展開的數(shù)組:')print (a.flatten())print ('\n') print ('以 F 風(fēng)格順序展開的數(shù)組:')print (a.flatten(order = 'F'))?
原數(shù)組:[[0 1 2 3] [4 5 6 7]]展開的數(shù)組:[0 1 2 3 4 5 6 7]以 F 風(fēng)格順序展開的數(shù)組:[0 4 1 5 2 6 3 7]In?[127]:
# 翻轉(zhuǎn)數(shù)組In?[276]:
# numpy.transpose 函數(shù)用于對換數(shù)組的維度 numpy.ndarray.T 類似 numpy.transpose a = np.arange(12).reshape(3,4) print ('原數(shù)組:')print (a )print ('\n') print ('對換數(shù)組:')print (np.transpose(a))print('-------------------')print(a.T)?
原數(shù)組:[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]]對換數(shù)組:[[ 0 4 8] [ 1 5 9] [ 2 6 10] [ 3 7 11]]-------------------[[ 0 4 8] [ 1 5 9] [ 2 6 10] [ 3 7 11]]In?[134]:
# 軸的理解In?[277]:
a = np.array([[78, 34, 87, 25, 83], [25, 67, 97, 22, 13], [78, 43, 87, 45, 89]])print(a)print('-------------------')print(a.max(axis=0))print(a.max(axis=1))#axis=0就是豎軸的數(shù)據(jù) axis=1就是橫軸的?
[[78 34 87 25 83] [25 67 97 22 13] [78 43 87 45 89]]-------------------[78 67 97 45 89][87 97 89]In?[278]:
a = np.array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]])print(a.shape)print(a)print('--------------')print(a.max(axis=0))print('-----------')print(a.max(axis=1))print('-------------')print(a.max(axis=2))# 這里的最大值就是把一個一維數(shù)組當(dāng)成了一個元素?
(2, 2, 2)[[[0 1] [2 3]] [[4 5] [6 7]]]--------------[[4 5] [6 7]]-----------[[2 3] [6 7]]-------------[[1 3] [5 7]]In?[279]:
# numpy.rollaxis 函數(shù)向后滾動特定的軸到一個特定位置# numpy.rollaxis(arr, axis, start)# arr:數(shù)組# axis:要向后滾動的軸,其它軸的相對位置不會改變# start:默認(rèn)為零,表示完整的滾動。會滾動到特定位置。# 創(chuàng)建了三維的 ndarraya = np.arange(8).reshape(2,2,2) print ('原數(shù)組:')print (a)print ('\n')# 將軸 2 滾動到軸 0(寬度到深度) # 程序運(yùn)行np.rollaxis(a, 2)時,講軸2滾動到了軸0前面,其他軸相對2軸位置不變(start默認(rèn)0),數(shù)組下標(biāo)排序由0,1,2變成了1,2,0 print ('調(diào)用 rollaxis 函數(shù):')print (np.rollaxis(a,2))# 將軸 0 滾動到軸 1:(寬度到高度)print ('\n') print ('調(diào)用 rollaxis 函數(shù):')print (np.rollaxis(a,2,1))?
原數(shù)組:[[[0 1] [2 3]] [[4 5] [6 7]]]調(diào)用 rollaxis 函數(shù):[[[0 2] [4 6]] [[1 3] [5 7]]]調(diào)用 rollaxis 函數(shù):[[[0 2] [1 3]] [[4 6] [5 7]]]In?[136]:
# numpy.swapaxes 函數(shù)用于交換數(shù)組的兩個軸 numpy.swapaxes(arr, axis1, axis2)# arr:輸入的數(shù)組# axis1:對應(yīng)第一個軸的整數(shù)# axis2:對應(yīng)第二個軸的整數(shù)# 創(chuàng)建了三維的 ndarraya = np.arange(8).reshape(2,2,2) print ('原數(shù)組:')print (a)print ('\n')# 現(xiàn)在交換軸 0(深度方向)到軸 2(寬度方向) print ('調(diào)用 swapaxes 函數(shù)后的數(shù)組:')print (np.swapaxes(a, 2, 0))?
原數(shù)組:[[[0 1] [2 3]] [[4 5] [6 7]]]調(diào)用 swapaxes 函數(shù)后的數(shù)組:[[[0 4] [2 6]] [[1 5] [3 7]]]In?[137]:
# 隨機(jī)數(shù)組In?[280]:
# 隨機(jī)創(chuàng)建 指定形狀的數(shù)組 0-1之間的數(shù)np.random.rand(5)Out[280]:
array([0.30169196, 0.9670064 , 0.47162448, 0.62679488, 0.73837024])In?[281]:
np.random.rand(5,5, 5)Out[281]:
array([[[0.89417906, 0.70946608, 0.38672627, 0.76701869, 0.78554922], [0.8395321 , 0.6066907 , 0.92352055, 0.9223712 , 0.79842334], [0.48239551, 0.01127968, 0.23233443, 0.33877177, 0.84191728], [0.31445345, 0.69452591, 0.29333318, 0.46591799, 0.35955757], [0.83014411, 0.66361905, 0.2745486 , 0.44315133, 0.11167157]], [[0.2932468 , 0.51403315, 0.53770464, 0.81108324, 0.76383529], [0.97409406, 0.40976911, 0.21684626, 0.06193826, 0.99271513], [0.96543415, 0.6577333 , 0.28238376, 0.82693036, 0.29221568], [0.10894843, 0.40101362, 0.87845106, 0.84427336, 0.75716817], [0.07480796, 0.19393097, 0.7915619 , 0.4755644 , 0.52351122]], [[0.14791434, 0.90730496, 0.14768148, 0.93386785, 0.61918179], [0.27887145, 0.82167391, 0.96227453, 0.31035279, 0.96435175], [0.61742694, 0.3994602 , 0.9876201 , 0.17050054, 0.80599466], [0.93448767, 0.93377353, 0.17567329, 0.35896092, 0.86895971], [0.57438796, 0.2828866 , 0.39134851, 0.25140481, 0.37755792]], [[0.73122306, 0.41387161, 0.73493688, 0.56681141, 0.38409986], [0.62762826, 0.72740016, 0.03333494, 0.2724378 , 0.87094496], [0.36300417, 0.06047278, 0.19580073, 0.5297497 , 0.36031132], [0.27977137, 0.3582478 , 0.93715713, 0.77016022, 0.5999013 ], [0.48798223, 0.24158915, 0.97154431, 0.35866503, 0.54551312]], [[0.13168029, 0.61278339, 0.27324304, 0.12304226, 0.90762162], [0.98750724, 0.67448499, 0.44045933, 0.1650775 , 0.63162946], [0.65731927, 0.39280892, 0.72215663, 0.66654893, 0.53636512], [0.33444494, 0.92841366, 0.931691 , 0.06398883, 0.31514842], [0.01835854, 0.60237696, 0.03410581, 0.20646853, 0.23836661]]])In?[282]:
# 隨機(jī)獲取指定范圍的一個數(shù)np.random.uniform(0,100)Out[282]:
22.56103050009125In?[283]:
# 隨機(jī)獲取指定范圍的一個整數(shù)np.random.randint(0,52)Out[283]:
35In?[284]:
np.random.randint(0,20,size=(3,4))Out[284]:
array([[ 7, 15, 8, 15], [ 6, 19, 10, 9], [ 0, 18, 16, 10]])In?[144]:
np.random.randint(0,20,size=(5,4))Out[144]:
array([[15, 5, 16, 16], [ 0, 4, 2, 0], [13, 12, 15, 7], [14, 9, 10, 16], [11, 15, 10, 1]])In?[145]:
# np.random.choice(F,N,P)# 從0-F范圍里面去N個數(shù)(不包含N),P表示概率np.random.choice(2,2)Out[145]:
array([1, 1])In?[288]:
a1 = np.random.choice(5,3,p=[0.2,0,0.5,0.3,0])a1Out[288]:
array([0, 3, 2], dtype=int32)In?[150]:
b = np.random.randint(0,20,size=(5,4))print(b)?
[[16 18 0 1] [ 3 2 5 19] [ 2 17 10 16] [10 5 1 7] [11 5 14 12]]In?[151]:
np.random.shuffle(b)print(b)?
[[ 2 17 10 16] [16 18 0 1] [ 3 2 5 19] [11 5 14 12] [10 5 1 7]]In?[152]:
# 計算In?[153]:
a = [1,2,3,4]b= np.array(a,dtype='float')bOut[153]:
array([1., 2., 3., 4.])In?[154]:
b 1Out[154]:
array([2., 3., 4., 5.])In?[155]:
bOut[155]:
array([1., 2., 3., 4.])In?[156]:
a = np.array([1,3,5,7])aOut[156]:
array([1, 3, 5, 7])In?[157]:
# 同緯度且個數(shù)相同的數(shù)組相加a bOut[157]:
array([ 2., 5., 8., 11.])In?[289]:
# 數(shù)組a中大于3的數(shù)就會顯示True小于3的數(shù)就會顯示Falsea[a>3]Out[289]:
array([4, 5, 6, 7])In?[159]:
print(a)a[a>3]?
[1 3 5 7]Out[159]:
array([5, 7])In?[161]:
c = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15])cOut[161]:
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])In?[290]:
# 求出c中大于5且為偶數(shù)的元素c[(c>5) & (c%2==0)]Out[290]:
array([ 6, 8, 10])In?[292]:
d=np.array([1.1,2.2,3.3,4.4,5.5,6.6,7.7]) dOut[292]:
array([1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7])In?[294]:
# 向上取整print(np.ceil(d))# 向下取整print(np.floor(d))# 絕對值print(np.abs(d))# 開方print(np.sqrt(b))# 分別去除小數(shù)部分和整數(shù)部分print('------------------')print(np.modf(d))# e為底的指數(shù)函數(shù)print(np.exp(d))print(np.log(d))# 四舍五入print(np.round(d))print('------------------')print(np.rint(d))# 向0取整print(np.trunc(d))# 是否不等于任何整數(shù)print(np.isnan(d))# print(np.cos(b))print(np.sin(b))print(np.tan(b))# 上述操作 對普通列表 也實用?
[2. 3. 4. 5. 6. 7. 8.][1. 2. 3. 4. 5. 6. 7.][1.1 2.2 3.3 4.4 5.5 6.6 7.7][7.41619849 1. 1.41421356 1.73205081 2. 2.23606798 2.44948974 2.64575131 2.82842712 3. 3.16227766 3.31662479]------------------(array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7]), array([1., 2., 3., 4., 5., 6., 7.]))[ 3.00416602 9.0250135 27.11263892 81.45086866 244.69193226 735.09518924 2208.34799189][0.09531018 0.78845736 1.19392247 1.48160454 1.70474809 1.88706965 2.04122033][1. 2. 3. 4. 6. 7. 8.]------------------[1. 2. 3. 4. 6. 7. 8.][1. 2. 3. 4. 5. 6. 7.][False False False False False False False][ 0.02212676 0.54030231 -0.41614684 -0.9899925 -0.65364362 0.28366219 0.96017029 0.75390225 -0.14550003 -0.91113026 -0.83907153 0.0044257 ][-0.99975517 0.84147098 0.90929743 0.14112001 -0.7568025 -0.95892427 -0.2794155 0.6569866 0.98935825 0.41211849 -0.54402111 -0.99999021][-4.51830879e 01 1.55740772e 00 -2.18503986e 00 -1.42546543e-01 1.15782128e 00 -3.38051501e 00 -2.91006191e-01 8.71447983e-01 -6.79971146e 00 -4.52315659e-01 6.48360827e-01 -2.25950846e 02]In?[295]:
print(d)print('-------------------------')# 求和print(d.sum())# 平均值print(d.mean())# 等差數(shù)列print(d.cumsum())# 標(biāo)準(zhǔn)差print(d.std())# 方差print(d.var())# 最小值print(d.min())#最大值print(d.max())print('---------------')# 最大值索引print(d.argmin())# 最小值索引print(d.argmax())?
[1.1 2.2 3.3 4.4 5.5 6.6 7.7]-------------------------30.84.4[ 1.1 3.3 6.6 11. 16.5 23.1 30.8]2.24.8400000000000011.17.7---------------06In?[297]:
a = np.array( [20,30,40,50] )b = np.arange( 4 )print(a)print('-----')print(b)?
[20 30 40 50]-----[0 1 2 3]In?[298]:
c = a-bcOut[298]:
array([20, 29, 38, 47])In?[174]:
b**2Out[174]:
array([0, 1, 4, 9], dtype=int32)In?[175]:
10*np.sin(a)Out[175]:
array([ 9.12945251, -9.88031624, 7.4511316 , -2.62374854])In?[307]:
# 與許多矩陣語言不同,乘法運(yùn)算符 * 的運(yùn)算在NumPy數(shù)組中是元素級別的。矩陣乘積可以使用 dot 函數(shù)或方法執(zhí)行a = np.array( [[1,1,1],[0,1,1]] )aOut[307]:
array([[1, 1, 1], [0, 1, 1]])In?[317]:
b = np.array( [[2,0], [3,4], [1,1] ])bOut[317]:
array([[2, 0], [3, 4], [1, 1]])In?[315]:
a*bOut[315]:
array([[2, 0, 1], [0, 4, 1]])In?[318]:
a.dot(b)Out[318]:
array([[6, 5], [4, 5]])In?[319]:
a = np.ones((2,3), dtype=int)aOut[319]:
array([[1, 1, 1], [1, 1, 1]])In?[320]:
a*=3aOut[320]:
array([[3, 3, 3], [3, 3, 3]])In?[322]:
b = np.random.randint(0,6, size=(2,3))bOut[322]:
array([[1, 3, 2], [1, 0, 2]])In?[323]:
b =abOut[323]:
array([[4, 6, 5], [4, 3, 5]])In?[325]:
print(a)print(b)a =ba?
[[7 9 8] [7 6 8]][[4 6 5] [4 3 5]]Out[325]:
array([[11, 15, 13], [11, 9, 13]])In?[?]:
# 復(fù)制和視圖In?[?]:
# NumPy中,數(shù)組的復(fù)制有三種方式:# Python通用的地址復(fù)制:通過 b = a 復(fù)制 a 的值,b 與 a 指向同一地址,改變 b 同時也改變 a。# 通過視圖ndarray.view()僅復(fù)制值,當(dāng)對 c 值進(jìn)行改變會改變 a 的對應(yīng)的值,而改變 c 的 shape 不改變 a 的 shape。# ndarray.copy() 進(jìn)行的完整的拷貝,產(chǎn)生一份完全相同的獨立的復(fù)制。In?[210]:
a = np.arange(12)aOut[210]:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])In?[211]:
b=aa is bOut[211]:
TrueIn?[212]:
c = a.view() cOut[212]:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])In?[326]:
a is cOut[326]:
FalseIn?[216]:
c.shape = 2,6cOut[216]:
array([[ 0, 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10, 11]])In?[217]:
c [0,0] = 55aOut[217]:
array([55, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])In?[218]:
d = a.copy()d is aOut[218]:
FalseIn?[219]:
dOut[219]:
array([55, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])In?[221]:
d.shape = 2,6dOut[221]:
array([[55, 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10, 11]])In?[222]:
d[1,1] = 22aOut[222]:
array([55, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])In?[?]:?In?[?]:?來源:http://www.icode9.com/content-4-91601.html