numpy的函數應用

1.numpy的基本操作函數
1)astype(dtype):對數組元素進行數據類型的轉換

定義一維數組 a = [1,2,3,4]並將其元素轉換爲float類型

a = np.array([1,2,3,4])
a.dtype
Out[6]:
dtype(‘int32’)

b = a.astype(np.float)
b.dtype
Out[7]:
dtype(‘float64’)
a.dtype = np.float
a.dtype
Out[8]:
dtype(‘float64’)

2)reshape(tuple):對數組的維度進行調整

定義數組如下

#b = array([[ 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]])

b = np.arange(0,32,2).reshape(2,8)
b
Out[15]:
array([[ 0, 2, 4, 6, 8, 10, 12, 14],
[16, 18, 20, 22, 24, 26, 28, 30]])

import matplotlib.pyplot as plt
c = np.arange(0,1800).reshape(20,30,3)
plt.imshow©

將上述數組轉換爲一維數組array([ 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])

b.reshape(16)
Out[21]:
array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30])

3)concatenate((array1,array2,…),axix):將兩個或多個數組相連
注意:在做行連接是必須保證兩個或多個數組列數相同

定義數組n1 = [[1,2],[3,4]]和n2=[[‘a’,’‘b’],[‘c’,‘d’],[‘e’,‘f’]]

n1 = np.arange(1,7).reshape(3,2,)
n1
Out[37]:
array([[1, 2],
[3, 4],
[5, 6]])

n2 = np.array([[‘a’,‘b’],[‘c’,‘d’],[‘e’,‘f’]])
n2
Out[32]:
array([[‘a’, ‘b’],
[‘c’, ‘d’],
[‘e’, ‘f’]], dtype=’<U1’)

將n1和n2數組進行連接

np.concatenate((n1,n2))
Out[38]:
array([[‘1’, ‘2’],
[‘3’, ‘4’],
[‘5’, ‘6’],
[‘a’, ‘b’],
[‘c’, ‘d’],
[‘e’, ‘f’]], dtype=’<U11’)

將n1與n2按列進行連接

np.concatenate((n1,n2),axis=1)
Out[39]:
array([[‘1’, ‘2’, ‘a’, ‘b’],
[‘3’, ‘4’, ‘c’, ‘d’],
[‘5’, ‘6’, ‘e’, ‘f’]], dtype=’<U11’)
注 :按行連接需要列相同,按列連接需要行相同

4)切分 split(array,num,axis):將數組array切分爲num份。

將數組b平均分成兩份

d = b.reshape(4,4)
d
Out[40]:
array([[ 0, 2, 4, 6],
[ 8, 10, 12, 14],
[16, 18, 20, 22],
[24, 26, 28, 30]])

np.split(d,2)
Out[41]:
[array([[ 0, 2, 4, 6],
[ 8, 10, 12, 14]]),
array([[16, 18, 20, 22],
[24, 26, 28, 30]])]

np.split(d,(1,3))
Out[45]:
[array([[0, 2, 4, 6]]),
array([[ 8, 10, 12, 14],
[16, 18, 20, 22]]),
array([[24, 26, 28, 30]])]

將數組b按列分爲4份

np.split(d,4,axis=1)
Out[46]:
[array([[ 0],
[ 8],
[16],
[24]]),
array([[ 2],
[10],
[18],
[26]]),
array([[ 4],
[12],
[20],
[28]]),
array([[ 6],
[14],
[22],
[30]])]

2.統計函數
1)求和 sum(array,axis)
b
Out[49]:
array([[ 0, 2, 4, 6, 8, 10, 12, 14],
[16, 18, 20, 22, 24, 26, 28, 30]])

將數組b中元素求和

np.sum(b)
Out[48]:
240

將數組b中元素每一列的元素求和

np.sum(b,axis=0)
Out[51]:
array([16, 20, 24, 28, 32, 36, 40, 44])

將數組b中元素每一行的元素求和

np.sum(b,axis=1)
Out[52]:
array([ 56, 184])
axis的值爲1時表示行,axis的值爲0時表示列
2)算數平均值 mean(array,axis)

計算數組b的算數平均值

np.mean(b)
Out[53]:
15.0

計算數組b每一列元素的算數平均值

np.mean(b,axis=0)
Out[55]:
array([ 8., 10., 12., 14., 16., 18., 20., 22.])

計算數組b每一行元素的算數平均值

np.mean(b,axis=1)
Out[56]:
array([ 7., 23.])

3)加權平均值 average(array,weights=權重列表)

已知某同學的成績爲平時成績=900.4 期末成績=800.6,計算該同學的總成績

score = np.array([90,80])
w = [0.4,0.6]
totle = np.average(score,weights=w)
totle
Out[58]:
84.0

4)方差 var(array,axis)

已知數據樣本 n1 = [23,0.5,10,4,56,3] n2 = [12,18,21,13,15],判斷哪個數據樣本的波動較大

n3 = np.array([23,0.5,10,4,56,3])
n4 = np.array([12,18,21,13,15])
np.var(n3)
Out[63]:
373.03472222222234
np.var(n4)
Out[64]:
10.959999999999999

5)標準差 std(array,axis)

計算n1和n2的標準差

np.std(n3)
Out[67]:
19.314106819167755
np.std(n4)
Out[68]:
3.3105890714493698

6)最大值max(array,axis)

計算數組b的最大值

np.max(b)
Out[69]:
30

計算數組b每一行的最大值

np.max(b,axis=1)
Out[71]:
array([14, 30])

計算數組b每一列的最大值

np.max(b,axis=0)
Out[72]:
array([16, 18, 20, 22, 24, 26, 28, 30])

7)最小值min(array,axis)

計算數組b的最小值

np.min(b)
Out[74]:
0

計算數組b每一行的最小值

np.min(b,axis=1)
Out[75]:
array([ 0, 16])

計算數組b每一列的最小值

np.min(b,axis=0)
Out[76]:
array([ 0, 2, 4, 6, 8, 10, 12, 14])

8)最大值與最小值的差ptp(array,axis)

計算數組b最大值與最小值的差

np.ptp(b)
Out[77]:
30

計算數組b每一行最大最小值差

np.ptp(b,axis=1)
Out[78]:
array([14, 14])

計算數組b每一列最大最小值差

np.ptp(b,axis=0)
Out[80]:
array([16, 16, 16, 16, 16, 16, 16, 16])

9)最大值索引 argmax(array,axis)

將b中元素打亂

np.random.shuffle(b)
b
Out[81]:
array([[16, 18, 20, 22, 24, 26, 28, 30],
[ 0, 2, 4, 6, 8, 10, 12, 14]])

b中元素最大值的索引

np.argmax(b)
Out[82]:
7

np.argmax(b,axis=0)
Out[83]:
array([0, 0, 0, 0, 0, 0, 0, 0], dtype=int64)

np.argmax(b,axis=1)
Out[84]:
array([7, 7], dtype=int64)
10)累計求和cumsum()

計算b的累計和

np.cumsum(b)
Out[85]:
array([ 16, 34, 54, 76, 100, 126, 154, 184, 184, 186, 190, 196, 204,
214, 226, 240], dtype=int32)

計算數組b每一列的累計和

np.cumsum(b,axis=0)
Out[86]:
array([[16, 18, 20, 22, 24, 26, 28, 30],
[16, 20, 24, 28, 32, 36, 40, 44]], dtype=int32)

計算數組b每一行的累計和

np.cumsum(b,axis=1)
Out[87]:
array([[ 16, 34, 54, 76, 100, 126, 154, 184],
[ 0, 2, 6, 12, 20, 30, 42, 56]], dtype=int32)
11)累計求積cumprod()

計算b的累計和積

b.dtype = np.int64
np.cumprod(b)
Out[90]:
array([ 77309411344, 3058016715072, 109126529064448,
4045103278802944, 1847179534663680, 7388718138654720,
59109745109237760, 709316941310853120], dtype=int64)

計算數組b每一列的累計積

np.cumprod(b,axis=0)
Out[91]:
array([[ 77309411344, 94489280532, 111669149720, 128849018908],
[ 137438953472, 893353197648, 1924145348800, 3229815406928]],
dtype=int64)

計算數組b每一行的累計積

np.cumprod(b,axis=1)
Out[92]:
array([[ 77309411344, 3058016715072, 109126529064448,
4045103278802944],
[ 8589934592, 34359738368, 274877906944,
3298534883328]], dtype=int64)

3.排序函數
1)sort(array,axis)數值排序

定義一個隨機整數數組 值範圍爲1-20,生成10個元素並轉換爲2*5的二維數組

n = np.random.randint(1,20,10).reshape(2,5)
n
Out[95]:
array([[15, 15, 17, 16, 2],
[ 3, 14, 8, 19, 16]])

將該數組進行排序

np.sort(n)
Out[96]:
array([[ 2, 15, 15, 16, 17],
[ 3, 8, 14, 16, 19]])

將該數組按行排序

np.sort(n,axis=1)
Out[98]:
array([[ 2, 15, 15, 16, 17],
[ 3, 8, 14, 16, 19]])

2)索引顯示排序結果 argsort()

將數組n用索引顯示排序結果

np.argsort(n)
Out[99]:
array([[4, 0, 1, 3, 2],
[0, 2, 1, 4, 3]], dtype=int64)

4.篩選元素的函數
1)where(條件列表,條件滿足時顯示數組,條件不滿足是顯示的數組)
a1 = np.array([1,2,3,4])
a2 = np.array([4,5,6,7])

條件數組爲 t = [True,True,False,True],從a1和a2中篩選出滿足條件的數組

t = [True,True,False,True]
np.where(t,a1,a2)
Out[103]:
array([1, 2, 6, 4])

2)all()

判斷數組a1中是否都大於0

np.all(a1>0)
Out[104]:
True

判斷數組a2中元素是否都爲奇數

np.all(a2%2!=0)
Out[106]:
False

3)any()

判斷數組a1中是否含有大於2的數

np.any(a1>2)
Out[107]:
True

判斷數組a2中是否含有偶數

np.any(a2%2==0)
Out[108]:
True

4)in1d() : 判斷a1數組中是否含有a2數組中相同的元素

判斷a2中是否與a1有重複的元素

np.in1d(a2,a1)
Out[109]:
array([ True, False, False, False])

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章