简体   繁体   English

numpy矩阵和列数组的元素乘法

[英]Elementwise multiplication of numpy matrix and column array

Using numpy, I want to multiple a matrix x by a column array y , elementwise: 使用numpy,我想将矩阵x按元素排列的列数组y相乘:

   x = numpy.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
   y = numpy.array([1, 2, 3])
   z = numpy.multiply(x, y)
   print z

This gives the output as if y is a row array: 这给出的输出就像y是一个行数组一样:

   [[ 1  4  9]
    [ 4 10 18]
    [ 7 16 27]]

However, I want the output as if y is a column array: 但是,我希望输出就像y是一个列数组:

   [[ 1  2  3]
    [ 8 10 12]
    [21 24 27]]

So how can I manipulate y to achieve this? 那么我如何操纵y来实现呢? If I use: 如果我使用:

y = numpy.transpose(y)

then y remains the same shape. y保持相同形状。

您可以使用reshape

y = y.reshape(-1,1)

Enclose it in another list to make it 2D: 将其包含在另一个列表中以使其为二维:

>>> y2 = numpy.transpose([y])
>>> y2
array([[1],
       [2],
       [3]])
>>> numpy.multiply(x, y2)
array([[ 1,  2,  3],
       [ 8, 10, 12],
       [21, 24, 27]])

The reason you can't transpose y is because it's initialized as a 1-D array. 不能转置y的原因是因为它已初始化为一维数组。 Transposing an array only makes sense in two (or more) dimensions. 转置数组仅在两个(或多个)维中有意义。

To get around these mixed-dimension issues, numpy actually provides a set of convenience functions to sanitize your inputs: 为了解决这些混合尺寸问题, numpy实际上提供了一组便利功能来清理您的输入:

y = np.array([1, 2, 3])
y1 = np.atleast_1d(y)  # Converts array to 1-D if less than that
y2 = np.atleast_2d(y)  # Converts array to 2-D if less than that
y3 = np.atleast_3d(y)  # Converts array to 3-D if less than that

I also think np.column_stack falls under this convenience category, as it puts together 1-D and 2-D arrays as columns like you would expect, rather than having to figure out the right series of reshapes and stacks. 我还认为np.column_stack属于此类便利类别,因为它像您期望的那样将一维和二维数组放在一起作为列,而不必找出正确的变形和堆栈系列。

y1 = np.array([1, 2, 3])
y2 = np.array([2, 4, 6])
y3 = np.array([[2, 6], [2, 4], [7, 7]])
y = np.column_stack((y1, y2, y3))

I think these functions aren't as well known as they should be, and I find them much easier, more flexible, and safer than manually fiddling with reshape or array dimensions. 我认为这些功能并不像应有的那样知名,而且我发现它们比手动摆弄整形或数组尺寸要容易,灵活和安全得多。 They also avoid making copies when possible, which can be a small performance speedup. 他们还避免在可能的情况下进行复制,这可能会降低性能。


To answer your question, you should use np.atleast_2d to convert your array to a 2-D array, then transpose it. 要回答您的问题,您应该使用np.atleast_2d将数组转换为二维数组,然后转置它。

y = np.atleast_2d(y).T    

The other way to quickly do it without worrying about y is to transpose x then transpose the result back. 快速进行此操作而无需担心y的另一种方法是转置x然后将结果转回。

z = (x.T * y).T

Though this can obfuscate the intent of the code. 虽然这会混淆代码的意图。 It is probably faster though if performance is important. 如果性能很重要,它可能会更快。


If performance is important, that can inform which method you want to use. 如果性能很重要,则可以告知您要使用哪种方法。 Some timings on my computer: 我的计算机上的一些时间安排:

%timeit x * np.atleast_2d(y).T
100000 loops, best of 3: 7.98 us per loop

%timeit (x.T*y).T
100000 loops, best of 3: 3.27 us per loop

%timeit x * np.transpose([y])
10000 loops, best of 3: 20.2 us per loop

%timeit x * y.reshape(-1, 1)
100000 loops, best of 3: 3.66 us per loop

The y variable has a shape of (3,). y变量的形状为(3,)。 If you construct it this way: 如果以这种方式构造它:

y = numpy.array([1, 2, 3], ndmin=2) y = numpy.array([1,2,3],ndmin = 2)

...it will have a shape of (1,3), which you can transpose to get the result you want: ...它的形状为(1,3),您可以对其进行转置以获得所需的结果:

y = numpy.array([1, 2, 3], ndmin=2).T z = numpy.multiply(x, y) y = numpy.array([1,2,3],ndmin = 2).T z = numpy.multiply(x,y)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM