5. NumPy(2)
沫哈: 圷殆議了崔
[圭隈]: 沫哈議軟泌峙 0,辛卆會f奐,賜榒扈K峙-1,卆會fp.
1)a[index_a]:x函沫哈 index_a 議圷殆
2)a[index_a:index_b]:x函Bm^g沫哈 index_a 欺 index_b+1 議圷殆
[箭 1]: a = [2,4,6,8,10,12,14,16,18,20],函誼沫哈 index_a=2
議圷殆峙.
In [1]: a = np.array([2,4,6,8,10,12,14,16,18,20])
In [2]: a[2]
Out[2]: 6
6. NumPy(2)
[箭 2]: a = [2,4,6,8,10,12,14,16,18,20],函誼沫哈 index_a=2
欺 index_b=5 參式 index_a=-5 欺 index_b=-1 侭議圷殆峙,.
In [1]: a = np.array([2,4,6,8,10,12,14,16,18,20])
In [2]: a[2:5]
Out[2]: array([ 6, 8, 10])
In [3]: a[-5:-1]
Out[3]: array([12, 14, 16, 18])
7. NumPy(2)
俳頭: 紋Q了崔議圷殆
[圭隈]: [index_s:index_e:index_c], index_s:軟兵議沫哈,index_e:
Y崩議沫哈,index_c:紋Q侯及ラ沫哈議圷殆峙.
[箭 1]: a = [2,4,6,8,10,12,14,16,18,20],軟兵沫哈 0,Y崩沫哈
9,∨哈 2 議蔚桔議圷殆峙譲紋Q 66.
In [1]: a = np.array([2,4,6,8,10,12,14,16,18,20])
In [2]: a[0:9:2]=66
In [2]: print(a)
[66 4 66 8 66 12 66 16 66 20]
8. NumPy(2)
[箭 2]: a = [2,4,6,8,10,12,14,16,18,20],∬双議圷殆恂宜會.
In [1]: a = np.array([2,4,6,8,10,12,14,16,18,20])
In [2]: a[::-1]
Out[2]: array([20, 18, 16, 14, 12, 10, 8, 6, 4, 2])
In [3]: a[::1]
Out[3]: array([ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20])
9. NumPy(2)
亨旗: L侭嗤議圷殆
[圭隈]: for i in array:,邑^筈議圭塀輳L双賜裳議侭嗤圷殆.
[箭 1]: a = [2,4,6,8,10,12,14,16,18,20],\喘亨旗輳L侭嗤議
圷殆.
In [1]: a = np.array([2,4,6])
In [2]: for i in a:
print(i)
2
4
6
10. NumPy(2)
[箭 2]: a = [2,4][8,10],\喘亨旗輳L侭嗤議圷殆.
In [1]: a = np.array([[2,4],[8,10]])
In [2]: for i in a:
print(i)
[2 4]
[ 8 10]
In [3]: for i in a.flat:
print(i)
2
4
8
10
12. NumPy(2)
[箭 1]: a = [2,4][6,8], b = [10,12][14,16],\喘 np.vstack(),
才 np.concatenate(axis = 0) a 才 b 恂換岷圭鮑
In [1]: a = np.array([[2,4],[6,8]])
In [2]: b = np.array([[10,12],[14,16]])
In [3]: np.vstack((a, b))
Out[3]:
array([[ 2, 4],
[ 6, 8],
[10, 12],
[14, 16]])
22. NumPy(2)
shadow copy: 俐個圷殆峙才S業厚個圻兵裳議圷殆峙才S業.
[圭隈]: b = a
[箭 1]: \喘 shadow copy 秀匯仟議裳,K俐個圷殆峙才S業.
In [1]: a = np.arange(5)
In [2]: b = a
In [3]: b.shape = 5,1
In [4]: b[0] = 999
In [5]: print('a=',a)
24. NumPy(2)
deep copy: 俐個圷殆峙才S業K音厚個圻兵裳議圷殆峙才S業.
[圭隈]: b = a.copy()
[箭 1]: \喘 copy()秀匯仟議裳,K俐個圷殆峙才S業.
In [1]: a = np.arange(5)
In [2]: b_copy = a.copy()
In [3]: b_copy.shape = 5,1
In [4]: b_copy[0] = 999
27. NumPy(2)
bitwise_and: 茂侘市 And 荷恬.
[圭隈]: np.bitwise_and()
[箭 1]: a = 10, b = 15,\喘 np.bitwise_and a 才 b 恂屈M崙 and
議\麻.
In [1]: a = 10
In [2]: b = 15
In [3]: np.bitwise_and(a, b)
Out[4]: 10
28. NumPy(2)
bitwise_or: 茂侘市 Or 荷恬.
[圭隈]: np.bitwise_or()
[箭 1]: a = 10, b = 15,\喘 np.bitwise_or a 才 b 恂屈M崙 or
議\麻.
In [1]: a = 10
In [2]: b = 15
In [3]: np.bitwise_or(a, b)
Out[4]: 15
30. NumPy(2)
[箭 1]: a = 10,\喘 np.invert a 恂屈M崙郡鰡\麻.
In [1]: a = 10
In [2]: bin(a)
Out[2]: '0b1010'
In [3]: bin(np.invert(a))
Out[3]: '-0b1011'
In [4]: np.invert(a)
Out[4]: -11
37. NumPy(2)
char.add: 双M佩忖堪B俊
[圭隈]: np.char.add()
[箭 1]: a = I m, b= Benson,\喘 np.char.add a 才 b 恂忖堪B俊.
In [1]: a = 'I am'
In [2]: b = ' Benson'
In [3]: np.char.add(a,b)
Out[3]: array('I am Benson', dtype='<U11')
47. NumPy(2)
char.join: 參峺協議憲躔峺式B俊双嶄議耽圷殆忖圷.
[圭隈]: np.char.join()
[箭 1]: a = Hi!, I am Benson.,\喘 np.char.join a 忖堪嶄議耽
忖圷脅參-憲恂蛍侯式B俊.
In [1]: a = ['Hi!', 'I am Benson.']
In [2]: np.char.join('-', a)
Out[2]: array(['H-i-!', 'I- -a-m- -B-e-n-s-o-n-.'],
dtype='<U23')
48. NumPy(2)
[箭 2]: a = Hi!, I am Benson.,\喘 np.char.join a 忖堪嶄議及
匯圷殆議耽忖圷脅參/憲恂蛍侯式B俊, 及屈圷殆議耽忖圷脅參#憲
恂蛍侯式B俊.
In [1]: a = ['Hi!', 'I am Benson.']
In [2]: np.char.join(['/', '#'], a)
Out[2]: array(['H/i/!', 'I# #a#m# #B#e#n#s#o#n#.'],
dtype='<U23')
50. NumPy(2)
char.encode: 忖堪DQ撹エa瘁議忖圷才憲.
[圭隈]: np.char.encode()
[箭 1]: a = Hi!, I am Benson.,\喘 np.char.encode a 忖堪D
Q撹エa瘁議忖圷才憲,聞喘議頁 cp500 議エa.
In [1]: a = ['Hi!', 'I am Benson.']
In [2]: np.char.encode(a, 'cp500')
Out[2]: array([b'xc8x89O',
b'xc9@x81x94@xc2x85x95xa2x96x95K'],
dtype='|S12')
51. NumPy(2)
char.decode: 忖堪M佩盾a.
[圭隈]: np.char.decode()
[箭 1]: a = [b'xc8x89O',
b'xc9@x81x94@xc2x85x95xa2x96x95K'],\喘
np.char.decode a 忖堪DQ撹盾a瘁議忖憲,聞喘議頁 cp500 議エa.
In [1]: a = [b'xc8x89O',
b'xc9@x81x94@xc2x85x95xa2x96x95K']
In [2]: np.char.decode(a, 'cp500')
Out[2]: array(['Hi!', 'I am Benson.'], dtype='<U12')
55. NumPy(2)
savez: 謹裳崛猟周嶄,険n兆.npz.
[圭隈]: np.savez()
[箭 1]: a = [2,4,6,8,10], b = [1,3,4,5,9]才 c = [2,14,16,18,20],
邑^ np.savez ∬双 a,b,才 c 脅秘欺 np_savez_outfile.npz n宛嶄.
In [1]: a = np.array([2,4,6,8,10])
In [2]: b = np.array([1,3,5,7,9])
In [3]: c = np.array([2,14,16,18,20])
In [4]: np.savez('np_savez_outfile.npz', a, b, c)
72. NumPy(2)
linalg.det: 麻秘裳議佩双塀.
[圭隈]: np.linalg.det()
[箭 1]: a = [2, 2, 3],[3, 8, 5],[5, 6, 14],\喘 linalg.det
輦麻壓佩双塀.
In [1]: a = np.array([[2, 2, 3],[3, 8, 5],[5, 6, 14]])
In [2]: np.linalg.det(a)
Out[2]: 64.00000000000003
73. NumPy(2)
linalg.solve: 麻來圭殻塀議 x 盾.
[圭隈]: np.linalg.solve()
[箭 1]: a = [2, 4],[3, 5],b = [8, 14],\喘 linalg.solve 輦
麻 ax=b 議來圭殻塀議盾.
In [1]: a = np.array([[2, 4],[3, 5]])
In [2]: b = np.array([8, 14])
In [3]: x = np.linalg.solve(a, b)
In [4]: print(x)
[ 8. -2.]