如何在(500x5000)矩阵中添加(500x5000)和(5000x1)矩阵?

时间:2017-03-10 16:58:30

标签: python numpy

我正在关注iPython笔记本中的教程。我的意图是计算(X ^ 2 - X_train)^ 2,将结果存储在dists中。以下代码似乎有效。我不明白它是如何运作的。

为什么(2 * inner_prod + train_sum)添加不同大小的矩阵会产生500x5000矩阵?

如何在dist的计算中处理矩阵?

    test_sum = np.sum(np.square(X), axis=1) # summed each example #(500x1)
    train_sum = np.sum(np.square(self.X_train), axis=1) # summed each example #(5000x1)
    inner_prod = np.dot(X, self.X_train.T) #matrix multiplication for 2-D arrays (500x3072)*(3072x5000)=(500x5000)
    print inner_prod.shape
    print X.shape
    print self.X_train.T.shape
    print test_sum.shape
    print test_sum.size
    print train_sum.shape
    print train_sum.size
    print test_sum.reshape(-1,1).shape
    # how... does reshaping work???
    print (2*inner_prod+train_sum).shape
    dists = np.sqrt(np.reshape(test_sum,(-1,1)) - 2 * inner_prod + train_sum) # (500x1) - 2*(500x5000) + (5000x1) = (500x5000)
    print dists.shape

印刷语句提供以下内容:

(500L, 5000L)
(500L, 3072L)
(3072L, 5000L)
(500L,)
500
(5000L,)
5000
(500L, 1L)
(500L, 5000L)
(500L, 5000L)

1 个答案:

答案 0 :(得分:1)

print train_sum.shape           # (5000,)
print train_sum.size
print test_sum.reshape(-1,1).shape    # (5000,1)
# how... does reshaping work???
print (2*inner_prod+train_sum).shape

test_sum.reshape(-1,1)返回具有新形状(但共享数据)的新数组。它不会重塑test_sum本身。

所以加法广播确实:

(500,5000) + (5000,) => (500,5000)+(1,5000)=>(500,5000)

如果它已经完成重塑,那么你就会出错。

(500,5000) + (5000,1) => error

In [68]: np.ones((500,5000))+np.zeros((5000,1))
ValueError: operands could not be broadcast together with shapes (500,5000) (5000,1) 

实际上只有一种方法可以添加(500,5000)数组和(5000,)数组,以及没有(有效)重组的数据。

train_sum.shape = (-1,1)已采取行动,但并未像reshape那样频繁使用。坚持改造,但使用正确。