import numpy as np
1. Basic Index
get a single element
a = np.diag(np.arange(3))
print(a[1,1])
1
get a set of elements
print(a[1])
[0 1 0]
modify element
a[2,1] = 10
print(a)
[[ 0 0 0]
[ 0 1 0]
[ 0 10 2]]
Two, fancy index
a = np.arange(10,1,-1)
a
array([10, 9, 8, 7, 6, 5, 4, 3, 2])
1. Use a list of integers to access elements
b = a[[3,3,-3,7]]
print(b)
[7 7 4 3]
The array obtained using a list of integers does not share memory with the original array
b[0]=100
print(b)
print(a)
[100 7 4 3]
[10 9 8 7 6 5 4 3 2]
2. Access elements using integer arrays
b = a[np.array([4,4,2,1])]
b
array([6, 6, 8, 9])
3. Use boolean array to access elements
mask = [a%3 == 0]
print(mask)
a[mask]
[array([False, True, False, False, True, False, False, True, False], dtype=bool)]
array([9, 6, 3])
3. Slicing
a = np.arange(10)
print(a)
[0 1 2 3 4 5 6 7 8 9]
Take the elements in [3,5)
a[3:5]
array([3, 4])
Take the elements in [0,5)
a[:5]
array([0, 1, 2, 3, 4])
Take all elements from 2nd to 2nd to last
a[2:-2]
array([2, 3, 4, 5, 6, 7])
From the 2nd element to the 2nd last element, all elements with a step size of 2
a[2:-2:2]
array([2, 4, 6])
All elements from the 2nd last element to the 2nd element with a step size of -2
a[-2:2:-2]
array([8, 6, 4])
inverted array
a[::-1]
array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
Fourth, copy and view
1. View: The array obtained by slicing is the view of the original array and shares memory with the original array
a = np.arange(10)
b = a[::2]
np.may_share_memory(a,b)
True
Two arrays share memory, modify one of the arrays, the other will be modified at the same time
b[0] = 999
print(b)
print(a)
[999 2 4 6 8]
[999 1 2 3 4 5 6 7 8 9]
2. Copy: use the copy function
c = a[::2].copy()
c[0] = -999
print(c)
print(a)
[-999 2 4 6 8]
[999 1 2 3 4 5 6 7 8 9]
5. Indexes on multidimensional arrays
1. Create a two-dimensional array by broadcasting
a = np.arange(0,60,10).reshape(-1,1) # 一个6*1的二维数组
b = np.arange(0,6) # 一个6个元素的一维数组
print(a)
print(b)
[[ 0]
[10]
[20]
[30]
[40]
[50]]
[0 1 2 3 4 5]
Example of broadcasting: add a[0] to the elements in b in turn
print(a[0])
print(b)
a[0]+b
[0]
[0 1 2 3 4 5]
array([0, 1, 2, 3, 4, 5])
Creating a 2D Array Using Broadcasting
c = a+b
print(c)
[[ 0 1 2 3 4 5]
[10 11 12 13 14 15]
[20 21 22 23 24 25]
[30 31 32 33 34 35]
[40 41 42 43 44 45]
[50 51 52 53 54 55]]
2. Convert a one-dimensional array to a two-dimensional array (convert n-dimension to m-dimension, m>n)
print(b[None,:]) #1*6的二维数组
print(b[:,None]) #6*1的二维数组
[[0 1 2 3 4 5]]
[[0]
[1]
[2]
[3]
[4]
[5]]
3. Use tuples to access multidimensional arrays
c[(0,1,2,4,4),(1,2,1,3,4)] #取(0,1)、(1,2)、(2,1)、(4,3)、(4,4)
array([ 1, 12, 21, 43, 44])
4. Use slices to access multidimensional arrays
print(c[0,3:5]) # 第0行的第3、4个元素,及c[0,3]、c[0,4]
print(c[4:,4:]) # 第4行到最后行与第4列到最后列交叉的所有元素
print(c[:,2]) # 第2列的所有元素
[3 4]
[[44 45]
[54 55]]
[ 2 12 22 32 42 52]
6. Get an element by index and reshape it
1. The shape of the index determines the shape of the array obtained by indexing
a = np.arange(10)
print(a)
idx = np.array([[3,4],[9,7]])
print(idx.shape) #索引的形状
a[idx] # 将数组a中的a[3]、a[4]、a[9]、a[7]按指定的形状重新组合
[0 1 2 3 4 5 6 7 8 9]
(2, 2)
array([[3, 4],
[9, 7]])
2. Reshape all axes to the same shape, and the array of elements obtained is also the same shape
x = np.array([[0,0],
[3,3]])
y = np.array([[1,2],
[4,2]])
c[x,y] #将c[0,1]、c[0,2]、c[3,4]、c[3,2]组织成2*2的二维数组
array([[ 1, 2],
[34, 32]])