import numpy as np data = np.array( [1,2,3]) print(data)
import numpy as np data = np.array( [1,2,3]) print(data . max())
import numpy as np data1 = np. ones(3) data2 = np. zeros(3) data3 = np. random.random(3) data4 = np. array([1,2]) data5 = np. ones(2)
import numpy as np data = np . array([1,2]) ones = np . ones(2) s = data + ones print( s )
import numpy as np data = np . array([1,2]) print( data.sum())
import numpy as np data = np.array([[1,2,3,4,5],[2,3,4,6,5]]) one = np . ones(1) print(data + one)
import numpy as np data = np.array([1,2]) x = data * 1.6 print(x)
import numpy as np one = np.ones((3,2)) print(one)
import numpy as np one = np.zeros((3,2)) print(one)
import numpy as np one = np.random.random((3,2)) print(one)
import numpy as np data = np.array([1,2,3]) one = np.array([[1,100,10000], [10,1000,100000]]) s = data * one print( s )
import numpy as np a = np.array([1,1,1,1]) b = np.array([[1],[1],[1],[1]]) a+b array([[2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2]]) # 这叫python的广播机制 c = np.array([[1,1,1,1]]) c+b array([[2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2]])
W = np.array([[1,1,1],[2,2,2]]) W[:,1] array([1, 2])
W[:,1] = np.array([5,5]) W array([[1, 5, 1], [2, 5, 2]])
import numpy as np matrix = [ [1,2,3,4], [5,6,7,8], [9,10,11,12] ] p1 = np.delete(matrix, 1, 0) print(’>>>>p1>>>>\n’,p1) p2 = np.delete(matrix, 1, 1) print(’>>>>p2>>>>\n’,p2) p3 = np.delete(matrix, 1) print(’>>>>p3>>>>\n’,p3) p4 = np.delete(matrix, [0,1], 1) print(’>>>>p4>>>>\n’,p4)
import numpy as np matrix = [ [1,2,3,4], [5,6,7,8], [9,10,11,12] ] import numpy as np matrix = [ [1,2,3,4], [5,6,7,8], [9,10,11,12] ] q1 = np.insert(matrix, 1, [1,1,1,1], 0) # 第0维度(行)第1行添加[1,1,1,1] print(’>>>>q1>>>>\n’,q1) q2 = np.insert(matrix, 0, [1,1,1], 1) # 第1维度(列)第0列添加1,1,1 print(’>>>>q2>>>>\n’,q2) q3 = np.insert(matrix, 3, [1,1,1,1], 0) # 第0维度(行)第3行添加[1,1,1,1] print(’>>>>q3>>>>\n’,q3)
import numpy as np a = np.array([[1,1,1],[2,2,2],[0,3,6]]) a array([[1, 1, 1], [2, 2, 2], [0, 3, 6]])
import numpy as np y1 = np.linspace(-10.0,10.0) # 默认生成50个数据 y1 array([-10. , -9.59183673, -9.18367347, -8.7755102 , -8.36734694, -7.95918367, -7.55102041, -7.14285714, -6.73469388, -6.32653061, -5.91836735, -5.51020408, -5.10204082, -4.69387755, -4.28571429, -3.87755102, -3.46938776, -3.06122449, -2.65306122, -2.24489796, -1.83673469, -1.42857143, -1.02040816, -0.6122449 , -0.20408163, 0.20408163, 0.6122449 , 1.02040816, 1.42857143, 1.83673469, 2.24489796, 2.65306122, 3.06122449, 3.46938776, 3.87755102, 4.28571429, 4.69387755, 5.10204082, 5.51020408, 5.91836735, 6.32653061, 6.73469388, 7.14285714, 7.55102041, 7.95918367, 8.36734694, 8.7755102 , 9.18367347, 9.59183673, 10. ])
import numpy as np x = np.array([[1,2,3],[4,5,6],[1,2,3]]) x.flatten() array([1, 2, 3, 4, 5, 6, 1, 2, 3]) # 拉平 x.ravel() array([1, 2, 3, 4, 5, 6, 1, 2, 3]) x.ravel(‘F’) array([1, 4, 1, 2, 5, 2, 3, 6, 3]) # 按列拉平 x.flatten(‘F’) array([1, 4, 1, 2, 5, 2, 3, 6, 3]) x.flatten()[1] = 20 x array([[1, 2, 3], [4, 5, 6], [1, 2, 3]]) x.ravel()[1] = 20 x array([[ 1, 20, 3], [ 4, 5, 6], [ 1, 2, 3]]) x.reshape(1,-1) array([[1, 2, 3, 4, 5, 6, 1, 2, 3]]) x = np.array([1,2,3,6,7,8]) ***>>> x[None,:] array([[1, 2, 3, 6, 7, 8]]) x[:,None] array([[1], [2], [3], [6], [7], [8]]) x[np.newaxis, :] array([[1, 2, 3, 6, 7, 8]])
import numpy as np x = np.array([[1,2,3],[4,5,6]]) np.zeros_like(x) # 生成一个和x大小相同的全零矩阵 array([[0, 0, 0], [0, 0, 0]]) 1 2 3 4 5
import numpy as np n = np.random.rand(3,4) n array([[0.11502462, 0.3503468 , 0.6206656 , 0.35172063], [0.66367565, 0.10195004, 0.22708003, 0.9318615 ], [0.77946053, 0.81804715, 0.2272248 , 0.17736476]])
np.pad(A,((3,2),(2,3)),‘constant’,constant_values = (0,0)) array([[ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 95, 96, 0, 0, 0], [ 0, 0, 97, 98, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0]])
np.pad(b, ((0,0),(1,1),(1,1)), ‘constant’, constant_values = 0) array([[[0, 0, 0, 0], [0, 1, 2, 0], [0, 3, 4, 0], [0, 0, 0, 0]],
import numpy as np c = np.array([1,2,5,4]) c[:,np.newaxis] array([[1], [2], [5], [4]]) c[np.newaxis,:] array([[1, 2, 5, 4]])