numpy库中一维矩阵的一些坑点

来源:互联网 发布:韩国网络剧推荐 编辑:程序博客网 时间:2024/06/06 00:14
import numpy as npw = np.array(range(15)).reshape(3,5)b = np.array(range(5))c = b.reshape(1,b.shape[0])print w.shape,b.shape, c.shape#(3, 5) (5,) (1, 5)a = w -bd = w -cprint a, a.shape"""[[ 0  0  0  0  0] [ 5  5  5  5  5] [10 10 10 10 10]] (3, 5) """print d, d.shape"""[[ 0  0  0  0  0] [ 5  5  5  5  5] [10 10 10 10 10]] (3, 5) """#b += c#ValueError: non-broadcastable output operand with shape (5,) doesn't match the broadcast shape (1,5)c += bprint c, c.shape#[[0 2 4 6 8]] (1, 5)


可以看见b(5,)可以向c(1,5)或其他(n,5)传播,这个传播是单向的这个也有点意思 https://stackoverflow.com/questions/32109319/how-to-implement-the-relu-function-in-numpy