ndarray 的各种索引和切片
import numpy as np
一维数组索引和切片
arr = np.arange(10)
print(arr[5])
print(arr[3:8])
print(arr[:])
5
[3 4 5 6 7]
[0 1 2 3 4 5 6 7 8 9]
arr[2:5] = 12
print(arr)
[ 0 1 12 12 12 5 6 7 8 9]
二维数组
arr = np.array([[1,2,3],[3,4,5],[6,7,8]])
print(arr)
print()
print(arr[2])
print()
print(arr[0][0])
print()
print(arr[0,0])
[[1 2 3]
[3 4 5]
[6 7 8]]
[6 7 8]
1
1
多维数组
arr = np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
print(arr)
print(arr.shape)
print(arr[0])
print(arr[0,0])
print(arr[0][0][0])
[[[ 1 2 3]
[ 4 5 6]]
[[ 7 8 9]
[10 11 12]]]
(2, 2, 3)
[[1 2 3]
[4 5 6]]
[1 2 3]
1
数组的copy
arr = np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
old_values = arr[0].copy()
values = arr[0]
arr[0] = 2
print(old_values)
[[1 2 3]
[4 5 6]]
print(arr)
[[[ 2 2 2]
[ 2 2 2]]
[[ 7 8 9]
[10 11 12]]]
arr[0] = old_values
print(arr)
[[[ 1 2 3]
[ 4 5 6]]
[[ 7 8 9]
[10 11 12]]]
切片和索引
arr = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(arr[:2]) #打印真正的1,2行
[[1 2 3]
[4 5 6]]
print(arr[:2,1:]) #12行,23列
[[2 3]
[5 6]]
print(arr[:,:1]) #第一列
[[1]
[4]
[7]]
arr[:,1:2] = 0
print(arr)
[[1 0 3]
[4 0 6]
[7 0 9]]
布尔数组
arr = np.random.randn(7,4)
str_arr = np.array(['a','c','b','a','d','e','c'])
print(str_arr == 'a')
print(str_arr == 'b')
[ True False False True False False False]
[False False True False False False False]
利用布尔数组作为索引取值
print(arr)
[[ 0.21610951 0.95843796 0.17142308 1.7343341 ]
[ 1.87321275 0.13157334 1.26847987 0.10746907]
[ 0.26667955 -1.26403189 1.13455612 -1.13179328]
[ 0.31517531 -0.19478254 0.43399553 -0.98840003]
[ 0.7935211 -0.07746844 0.00948928 -0.06383993]
[ 0.68035449 -0.05042796 -0.54446476 -1.02342964]
[-1.00130873 0.9020401 -1.07449253 0.37424268]]
print(arr[str_arr == 'a'])
[[ 0.21610951 0.95843796 0.17142308 1.7343341 ]
[ 0.31517531 -0.19478254 0.43399553 -0.98840003]]
print(arr[str_arr == 'b'])
[[ 0.26667955 -1.26403189 1.13455612 -1.13179328]]
print(arr[str_arr == 'a',:2])
[[ 0.21610951 0.95843796]
[ 0.31517531 -0.19478254]]
print(arr[str_arr == 'a',:1])
[[ 0.21610951]
[ 0.31517531]]
print(arr[~(str_arr == 'a')])
[[ 1.87321275 0.13157334 1.26847987 0.10746907]
[ 0.26667955 -1.26403189 1.13455612 -1.13179328]
[ 0.7935211 -0.07746844 0.00948928 -0.06383993]
[ 0.68035449 -0.05042796 -0.54446476 -1.02342964]
[-1.00130873 0.9020401 -1.07449253 0.37424268]]
mask_arr = (str_arr == 'a')|(str_arr == 'b')
print(mask_arr)
[ True False True True False False False]
print(arr[mask_arr])
[[ 0.21610951 0.95843796 0.17142308 1.7343341 ]
[ 0.26667955 -1.26403189 1.13455612 -1.13179328]
[ 0.31517531 -0.19478254 0.43399553 -0.98840003]]
arr[str_arr == 'b'] = 0
print(arr)
[[ 0.21610951 0.95843796 0.17142308 1.7343341 ]
[ 1.87321275 0.13157334 1.26847987 0.10746907]
[ 0. 0. 0. 0. ]
[ 0.31517531 -0.19478254 0.43399553 -0.98840003]
[ 0.7935211 -0.07746844 0.00948928 -0.06383993]
[ 0.68035449 -0.05042796 -0.54446476 -1.02342964]
[-1.00130873 0.9020401 -1.07449253 0.37424268]]
花式索引
arr = np.empty((8,4))
for i in range(8):
arr[i] = i
print(arr)
[[ 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.]]
print(arr[[4,2,5,7]])
[[ 4. 4. 4. 4.]
[ 2. 2. 2. 2.]
[ 5. 5. 5. 5.]
[ 7. 7. 7. 7.]]
print(arr[[-3,-7,-5]])
[[ 5. 5. 5. 5.]
[ 1. 1. 1. 1.]
[ 3. 3. 3. 3.]]
print(arr[[1,5,7,2],[0,3,1,2]])
[ 1. 5. 7. 2.]
选取区域
print(arr[[1,5,7,2]][:,[0,3,1,2]])
[[ 1. 1. 1. 1.]
[ 5. 5. 5. 5.]
[ 7. 7. 7. 7.]
[ 2. 2. 2. 2.]]
arr[np.ix_([1,5,7,2],[0,3,1,2])]
array([[ 1., 1., 1., 1.],
[ 5., 5., 5., 5.],
[ 7., 7., 7., 7.],
[ 2., 2., 2., 2.]])