1.numpy數(shù)組的特點(diǎn)在于怔揩,大小相等的數(shù)組之間捉邢,任何算數(shù)運(yùn)算都會將運(yùn)算應(yīng)用到元素級。
請看下面的例子:
[python] view plain copy
arr = np.array([[1.,2.,3.],[4.,5.,6.]])
arr
輸出:
array([[ 1., 2., 3.], [ 4., 5., 6.]])
[python] view plain copy
arr * arr
輸出:
array([[ 1., 4., 9.], [ 16., 25., 36.]])
[python] view plain copy
arr - arr
輸出:
array([[ 0., 0., 0.], [ 0., 0., 0.]])
[python] view plain copy
1 / arr
輸出:
array([[ 1. , 0.5 , 0.33333333], [ 0.25 , 0.2 , 0.16666667]])
[python] view plain copy
arr ** 0.5
輸出:
array([[ 1. , 1.41421356, 1.73205081], [ 2. , 2.23606798, 2.44948974]])
2.數(shù)據(jù)索引:Python是從0的位置開始
[python] view plain copy
arr = np.arange(10)
arr
輸出:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
[python] view plain copy
arr[5]
輸出:
5
[python] view plain copy
arr[5:8]
輸出:
array([5, 6, 7])
3.索引位置可以用來更改數(shù)組值:
[python] view plain copy
arr[5:8] = 12
輸出:
array([ 0, 1, 2, 3, 4, 12, 12, 12, 8, 9])
4.從上面可以看出沧踏,跟列表最重要的區(qū)別是歌逢,數(shù)組切片是原始數(shù)組的視圖。這意味著數(shù)據(jù)不會被復(fù)制翘狱,任何修改都會直接反映到源數(shù)組上:
[python] view plain copy
arr_slice = arr[5:8]
arr_slice[1] = 12345
[python] view plain copy
arr
輸出:
array([ 0, 1, 2, 3, 4, 12, 12345, 12, 8, 9])
[python] view plain copy
arr_slice[:] = 64
輸出:
array([ 0, 1, 2, 3, 4, 64, 64, 64, 8, 9])
5.如果你不要改變原始數(shù)組秘案,需要復(fù)制一份,可以這樣操作:
[python] view plain copy
arr_copy = arr[5:8].copy()
6.對于高維數(shù)組潦匈,索引位置上的元素不再是標(biāo)量而是低一維的數(shù)組阱高,比如:
[python] view plain copy
arr2d = np.array([[1,2,3],[4,5,6],[7,8,9]])
[python] view plain copy
arr2d[2]
輸出:
array([7, 8, 9])
7.對于高維數(shù)組,索引元素需要進(jìn)行遞歸訪問茬缩,可以傳入一個以逗號隔開的索引列表來獲取單個元素赤惊。
[python] view plain copy
arr2d[0][2]
輸出:
3
[python] view plain copy
arr2d[0,2]
輸出:
3以上兩種方法是等價(jià)的。
8.二維數(shù)組還比較容易理解凰锡,下面看一個三維數(shù)組的例子:
[python] view plain copy
arr3d = np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
arr3d
輸出:
array([[[ 1, 2, 3], [ 4, 5, 6]], [[ 7, 8, 9], [10, 11, 12]]])
這是一個(2×2×3)的數(shù)組未舟,可以通過運(yùn)行arr3d.shape進(jìn)行驗(yàn)證。
[python] view plain copy
arr3d[0]
輸出:
array([[1, 2, 3], [4, 5, 6]])
[python] view plain copy
old_values = arr3d[0].copy()
arr3d[0]= 42
arr3d
輸出:
array([[[42, 42, 42], [42, 42, 42]], [[ 7, 8, 9], [10, 11, 12]]])
[python] view plain copy
arr3d[0]= old_values
arr3d
輸出:
array([[[ 1, 2, 3], [ 4, 5, 6]], [[ 7, 8, 9], [10, 11, 12]]])
[python] view plain copy
arr3d[1,0]
輸出:
array([7, 8, 9])
9.再看幾個二維數(shù)組的索引例子掂为,用剛才創(chuàng)建的那個arr2d數(shù)組裕膀,注意索引位置包括開頭但不包括結(jié)尾,比如[0:2]是索引1勇哗、2位置昼扛,但沒有3。
[python] view plain copy
arr2d
輸出:
array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
[python] view plain copy
arr2d[:2]
輸出:
array([[1, 2, 3], [4, 5, 6]])
注意沒有第三個元素欲诺,也就是arr2d[2]
[python] view plain copy
arr2d[:2,1:]
輸出:
array([[2, 3], [5, 6]])
[python] view plain copy
arr2d[1,:2]
輸出:array([4, 5]
[python] view plain copy
arr2d[2,:1]
輸出:
array([7])
[python] view plain copy
arr2d[:,:1]
輸出:
array([[1], [4], [7]])
[python] view plain copy
arr2d[:2,1:] = 0
arr2d
輸出:
array([[1, 0, 0], [4, 0, 0], [7, 8, 9]])
可以借助下圖來理解:
[python] view plain copy
names = np.array(['Bob','Joe','Will','Bob','Will','Joe','Joe'])
names
輸出:
array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'], dtype='<U4')
生產(chǎn)一組隨機(jī)數(shù)組
[python] view plain copy
import numpy.random
data = numpy.random.randn(7,4)
data
輸出:
array([[ 0.98990731, -0.26694924, 1.17834132, -0.21712539], [ 1.10877471, 0.04496048, 0.99851866, -0.65930485], [ 0.20479382, -0.09918733, -0.51451222, 1.43864803], [-0.30217902, 1.20441035, -0.88069583, -0.31243213], [-1.47970158, 0.08452533, -1.56347609, 1.88383865], [ 0.45802479, 0.84710767, -1.87281658, 0.90212452], [ 0.57361001, 0.61528872, 1.25977742, -0.06899717]])
判斷抄谐,產(chǎn)生一個布爾型數(shù)組
[python] view plain copy
names == 'Bob'
輸出:
array([ True, False, False, True, False, False, False], dtype=bool)
這個布爾型數(shù)組可以用于數(shù)組索引[python] view plain copy
data[names == 'Bob']
輸出:array([[ 0.98990731, -0.26694924, 1.17834132, -0.21712539], [-0.30217902, 1.20441035, -0.88069583, -0.31243213]])
可以將布爾型數(shù)組跟切片、整數(shù)混合使用
[python] view plain copy
data[names == 'Bob',2:]
輸出:
array([[ 1.17834132, -0.21712539], [-0.88069583, -0.31243213]])
[python] view plain copy
data[names == 'Bob',3]
輸出:
array([-0.21712539, -0.31243213])
也可以用不等號(扰法!=)進(jìn)行或者符號(-)對條件進(jìn)行否定索引
[python] view plain copy
names != 'Bob'
輸出:
array([False, True, True, False, True, True, True], dtype=bool)
[python] view plain copy
data[-(names == 'Bob')]
輸出:
array([[ 1.10877471, 0.04496048, 0.99851866, -0.65930485], [ 0.20479382, -0.09918733, -0.51451222, 1.43864803], [-1.47970158, 0.08452533, -1.56347609, 1.88383865], [ 0.45802479, 0.84710767, -1.87281658, 0.90212452], [ 0.57361001, 0.61528872, 1.25977742, -0.06899717]])
11.也可以使用多個布爾型條件進(jìn)行索引蛹含,這在實(shí)際操作中十分常見
[python] view plain copy
mask = (names == 'Bob')|(names == 'Will')
mask
輸出:
array([ True, False, True, True, True, False, False], dtype=bool)
[python] view plain copy
data[mask]
輸出:
array([[ 0.98990731, -0.26694924, 1.17834132, -0.21712539], [ 0.20479382, -0.09918733, -0.51451222, 1.43864803], [-0.30217902, 1.20441035, -0.88069583, -0.31243213], [-1.47970158, 0.08452533, -1.56347609, 1.88383865]])
把小于0的數(shù)都變?yōu)?
[python] view plain copy
data[data < 0] = 0
data
輸出:
array([[ 0.98990731, 0. , 1.17834132, 0. ], [ 1.10877471, 0.04496048, 0.99851866, 0. ], [ 0.20479382, 0. , 0. , 1.43864803], [ 0. , 1.20441035, 0. , 0. ], [ 0. , 0.08452533, 0. , 1.88383865], [ 0.45802479, 0.84710767, 0. , 0.90212452], [ 0.57361001, 0.61528872, 1.25977742, 0. ]])
[python] view plain copy
data[names != 'Joe'] = 7
data
輸出:
array([[ 7. , 7. , 7. , 7. ], [ 1.10877471, 0.04496048, 0.99851866, 0. ], [ 7. , 7. , 7. , 7. ], [ 7. , 7. , 7. , 7. ], [ 7. , 7. , 7. , 7. ], [ 0.45802479, 0.84710767, 0. , 0.90212452], [ 0.57361001, 0.61528872, 1.25977742, 0. ]])
11.花式索引:
花式索引(Fancy indexing)是一個Numpy術(shù)語,它指的是利用整數(shù)數(shù)組進(jìn)行索引塞颁。
先建立一個8×4的empty數(shù)組
[python] view plain copy
arr = np.empty((8,4))
arr
輸出:
array([[ 2.27764263e-321, 0.00000000e+000, 3.65717707e-316, 0.00000000e+000], [ 0.00000000e+000, 2.50223420e-315, 0.00000000e+000, 4.42980404e-316], [ 0.00000000e+000, 3.61995693e-316, 2.50223420e-315, 4.43565180e-316], [ 2.50097935e-315, 4.59507256e-316, 0.00000000e+000, 0.00000000e+000], [ 0.00000000e+000, 0.00000000e+000, 4.42208160e-316, 0.00000000e+000], [ 3.65629171e-316, 1.98465379e-317, 2.50224669e-315, 2.63675246e-316], [ 0.00000000e+000, 0.00000000e+000, 0.00000000e+000, 0.00000000e+000], [ 2.50224384e-315, 0.00000000e+000, 0.00000000e+000, 4.59490260e-316]])
給其賦值
[python] view plain copy
for i in range(8):
arr[i] = i
arr
輸出:
array([[ 0., 0., 0., 0.], [ 1., 1., 1., 1.], [ 2., 2., 2., 2.], [ 3., 3., 3., 3.], [ 4., 4., 4., 4.], [ 5., 5., 5., 5.], [ 6., 6., 6., 6.], [ 7., 7., 7., 7.]])
以特定的順序選取行子集挣惰,只需傳入一個用于指定順序的整數(shù)列表或ndarray:
[python] view plain copy
arr[[4,3,0,6]]
輸出:
array([[ 4., 4., 4., 4.], [ 3., 3., 3., 3.], [ 0., 0., 0., 0.], [ 6., 6., 6., 6.]])
使用負(fù)數(shù)索引會從末尾開始:
[python] view plain copy
arr[[-3,-5,-7]]
輸出:
array([[ 5., 5., 5., 5.], [ 3., 3., 3., 3.], [ 1., 1., 1., 1.]])
使用reshape函數(shù)改變行列數(shù)
[python] view plain copy
arr = np.arange(32).reshape((8,4))
arr
輸出:
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]])
[python] view plain copy
arr[[1,5,7,2],[0,3,1,2]]
輸出:
array([ 4, 23, 29, 10])
可以看出卧斟,最終選出的元素是(1,0)、(5,3)憎茂、(7,1)、(2,2)
[python] view plain copy
arr[[1,5,7,2]][:,[0,3,1,2]]
輸出:
array([[ 4, 7, 5, 6], [20, 23, 21, 22], [28, 31, 29, 30], [ 8, 11, 9, 10]])
另外一個辦法是使用np.ix_函數(shù)锤岸,它可以將兩個一維整數(shù)數(shù)組轉(zhuǎn)換為一個用于選取方形區(qū)域的索引器:
[python] view plain copy
arr[np.ix_([1,5,7,2],[0,3,1,2])]
輸出:
array([[ 4, 7, 5, 6], [20, 23, 21, 22], [28, 31, 29, 30], [ 8, 11, 9, 10]])
注意:花式索引跟切片不一樣竖幔,它總是將數(shù)據(jù)復(fù)制到新數(shù)組中。