以下这段代码的输出结果为()
import numpy as np a = np.repeat(np.arange(5).reshape([1,-1]),10,axis = 0)+10.0 b = np.random.randint(5, size= a.shape) c = np.argmin(a*b, axis=1) b = np.zeros(a.shape) b[np.arange(b.shape[0]), c] = 1 print b
import numpy as np a = np.repeat(np.arange(5).reshape([1,-1]),10,axis = 0)+10.0 b = np.random.randint(5, size= a.shape) c = np.argmin(a*b, axis=1) b = np.zeros(a.shape) b[np.arange(b.shape[0]), c] = 1 print b
Hello World!
一个 shape = (5,10) 的随机整数矩阵
一个 shape = (5,10) 的 one-hot 矩阵
一个 shape = (10,5) 的 one-hot 矩阵
#生成数组[0,1,2,3,4] np.arange(5) #原数组共有x个元素,reshape([n,-1])意思是将原数组重组为n行x/n列的新数组 #所以数组共有5个元素,重组为1行5列的数组 reshape([1,-1]) #因为axis=0,所以是沿着横轴方向重复,增加行数 #所以原数组增加10行 repeat(np.arange(5).reshape([1,-1]), 10, axis = 0) #数组每个元素都+10 a = repeat(np.arange(5).reshape([1,-1]), 10, axis = 0) + 10
a = np.repeat(np.arange(5).reshape([1,-1]),10,axis = 0)+10.0
b = np.random.randint(5, size= a.shape)
c = np.argmin(a*b, axis=1)
b = np.zeros(a.shape)
b[np.arange(b.shape[0]), c] = 1