import numpy as np # 1: arr = np.array(range(32)).reshape(8, 4) print(arr) # 2: arr2 = np.linspace(1, 2, 3) print(arr2) # 3: arr3 = np.identity(9) # 9*9的方阵 arr31 = np.eye(9) print(arr3) # 4: arr4 = np.zeros(3) print(arr4) # 5: arr5 = np.random.randint(1, 10, (2, 2)) print(arr5)
import numpy as np arr = np.arange(10) # 1: print(arr[1], arr[2]) # 2: arr[4:7] = 12 print(arr) # 3: arr[5:] = 10 print(arr)
import numpy as np arr = np.arange(1, 10).reshape(3, 3) # 1: print(arr[0]) # 2: print(arr[1:])
import numpy as np arr = np.array([4, 5, 6]) # 1: print(type(arr)) # 2: print(arr.shape) # 3: print(arr[0])
import numpy as np b = np.array([[4, 5, 6], [1, 2, 3]]) # 1: print(b.shape) # 2: print(b[0][0], b[0][1], b[1][1]) print(b[0, 0], b[0, 1], b[1, 1])
import numpy as np # 1: a = np.zeros((3, 3), dtype=int) # 2: b = np.ones((4, 5)) # 3: c = np.eye(4) # 4: d = np.random.rand(3, 2) print(a) print(b) print(c) print(d)
import numpy as np a = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]]) b = a[0:2, 2:4] # 1: print(b[0, 0]) # 2: print(a[0, -1]) print(b[0, -1])
import numpy as np a = np.array([[1,2],[3,4],[5,6]]) print(a[0,0],a[1,1],a[2,0])
import numpy as np a = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]]) print(a[0,0],a[1,2],a[2,0],a[3,1])
[ 1, 6, 7, 11]
10.0
[4. 6.]
[3. 7.]
2.5
[2. 3.]
[1.5 3.5]
[[1 2 5 6]
[3 4 7 8]]
[[1 2]
[3 4]
[5 6]
[7 8]]
[[1 2]
[3 8]]
[[ 1 2]
[ 3 12]]
nan
False
False
nan
False
import numpy as np obj1 = np.zeros(10) obj1[4] = 1 print(obj1)
import numpy as np obj1 = np.array(range(10, 50)) print(obj1[::-1])
import numpy as np obj1 = np.random.random((10, 10)) print(np.max(obj1), np.min(obj1)) print(np.sort(obj1))
import numpy as np obj1 = np.zeros((10, 10), dtype=int) obj1[0] = 1 obj1[-1] = 1 obj1[:, 0] = 1 obj1[:, -1] = 1 print(obj1)
import numpy as np # 1: obj1 = np.array([range(5)]*5) print(obj1) # 2: obj2 = np.linspace(0, 1, 12) print(obj2) # 3: obj3 = np.random.rand(10) print(np.sort(obj3)) # 4: """ np.argwhere(条件)->好像列表推导式 只用np.argmax()只能返回第一个下标 """ arr = np.random.randint(1,10,10) all_index_max = np.argwhere(arr == np.max(arr)).reshape(-1) # 通过reshape(-1)转置 arr[all_index_max] = 0 print(arr)
import numpy as np arr = np.random.randint(0, 100, (5, 5)) print(arr) key = arr[:, 2] print(np.argsort(key)) print(arr[np.argsort(key)])
import numpy as np a = np.array([1, 2, 3, 4, 5]) b = np.zeros(17, dtype=int) # 3*4+5=17 b[::4] = a print(b)
import numpy as np m = np.random.randint(0, 5, (5, 5)) print(m) m[[1, 2]] = m[[2, 1]] # 交换第2行和第3行 print(m)
import numpy as np p = np.random.randint(0, 5, size=(5, 4)) all_mean = np.mean(p, axis=1).reshape(5, 1) print(p) print(p-all_mean)
import numpy as np x = np.zeros((8, 8), dtype=int) x[1::2, ::2] = 1 x[::2, 1::2] = 1 print(x)
import numpy as np x = np.random.rand(5, 5) max_x = np.max(x) min_x = np.min(x) print(max_x, min_x) print((x-max_x)/(max_x-min_x))
import numpy as np # 1: a = np.random.randn(10) print(np.where(a > 0, 1, -1)) # 2: x = np.array([[0,7,9,5,8,1,2,6,0,4]]) print(np.piecewise(x, [x<3, ((x>3)&(x<5)), x>7], [-1, 1, lambda x:x*4]))
a1:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]b1:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]a2:
[ 0 0 0 0 4 5 6 7 8 9 10 11]b2:
[[ 0 0 0 0]
[ 4 5 6 7]
[ 8 9 10 11]]a3:
[ 0 1 2 3 4 5 6 7 8 9 10 11]b3:
[[ 0 0 0 0]
[ 4 5 6 7]
[ 8 9 10 11]]
import numpy as np x = np.array([[0,1,2],[3,4,5],[6,7,8]]) b = np.append(x,[[7,8,9]],axis=0) # 插入一行 c = np.append(x,[[7],[8],[9]],axis=1) # 插入一列 print(b) print(c)