将numpy数组的列与另一个数组相加和相乘

时间:2022-02-08 06:56:54

I have a 2D numpy array x and and 1D numpy array y:

我有一个2D numpy数组x和1D numpy数组y:

import numpy as np
x = np.arange(12).reshape((4, 3))
y = np.array(([1.0,2.0,3.0,4.0])

I want to multiply / add the column vector y.reshape((4,1)) to each column of x. I attempted the following:

我想将列向量y.(4,1)乘/加到x的每一列。

y1 = y.reshape((4,1))    
y1 * x 

yields

收益率

array([[ 0., 1., 2.], 
       [ 6., 8., 10.], 
       [ 18., 21., 24.], 
       [ 36., 40., 44.]])

which is what I wanted. I also found

这就是我想要的。我还发现

array([[ 1., 2., 3.], 
       [ 5., 6., 7.], 
       [ 9., 10., 11.], 
       [ 13., 14., 15.]])

with y1 + x.
I would like to know if there is a better (more efficient) way to achieve the same thing!

用y + x,我想知道是否有更好(更有效)的方法来实现同样的目标!

2 个解决方案

#1


2  

NumPy supports this via broadcasting. Your code used broadcasting and it's the most efficient way to do things. I normally write it as:

NumPy通过广播支持这一点。您的代码使用了广播,这是最有效的方法。我通常这样写:

>>> x * y[..., np.newaxis]
array([[  0.,   1.,   2.],
       [  6.,   8.,  10.],
       [ 18.,  21.,  24.],
       [ 36.,  40.,  44.]])

To see that it is equivalent:

看它是等价的:

>>> z =  y[..., np.newaxis]
>>> z.shape
(4, 1)

You can also see that NumPy doesn't copy any data, it just changes the iteration over the same memory internally

您还可以看到NumPy没有复制任何数据,它只是在内部对相同内存的迭代进行了更改。

>>> z.base is y
True

Read more here

点击这里了解更多内容

#2


2  

You can use np.add and np.multiply :

你可以用np。添加和np。繁殖:

>>> np.add(y1,x)
array([[  1.,   2.,   3.],
       [  5.,   6.,   7.],
       [  9.,  10.,  11.],
       [ 13.,  14.,  15.]])
>>> np.multiply(y1,x)
array([[  0.,   1.,   2.],
       [  6.,   8.,  10.],
       [ 18.,  21.,  24.],
       [ 36.,  40.,  44.]])

OR for in-place changing you can use iadd and imul methods of a numpy ndarray :

或者对于就地更改,您可以使用iadd和imul方法的numpy ndarray:

>>> x.__iadd__(y1)
array([[ 1,  2,  3],
       [ 5,  6,  7],
       [ 9, 10, 11],
       [13, 14, 15]])
>>> x = np.arange(12).reshape((4, 3))
>>> x.__imul__(y1)
array([[ 0,  1,  2],
       [ 6,  8, 10],
       [18, 21, 24],
       [36, 40, 44]])

But note that :

但注意:

In place operations will perform the calculation using the precision decided by the data type of the two operands, but will silently downcast the result (if necessary) so it can fit back into the array. Therefore, for mixed precision calculations, A {op}= B can be different than A = A {op} B. For example, suppose a = ones((3,3)). Then, a += 3j is different than a = a + 3j: while they both perform the same computation, a += 3 casts the result to fit back in a, whereas a = a + 3j re-binds the name a to the result.

在适当的位置上,操作将使用两个操作数的数据类型决定的精度来执行计算,但将无声地将结果降下来(如果需要),以便它能够重新适合于数组。因此,对于混合精度计算,A {op}= B可以与A = A {op} B不同,例如,假设A = 1(3,3)。然后,a += 3j与a = a + 3j不同:当它们执行相同的计算时,a += 3将结果转换回a中,而a = a + 3j将名称a重新绑定到结果中。

Read more about arithmetic-and-comparison-operations

阅读更多关于arithmetic-and-comparison-operations

#1


2  

NumPy supports this via broadcasting. Your code used broadcasting and it's the most efficient way to do things. I normally write it as:

NumPy通过广播支持这一点。您的代码使用了广播,这是最有效的方法。我通常这样写:

>>> x * y[..., np.newaxis]
array([[  0.,   1.,   2.],
       [  6.,   8.,  10.],
       [ 18.,  21.,  24.],
       [ 36.,  40.,  44.]])

To see that it is equivalent:

看它是等价的:

>>> z =  y[..., np.newaxis]
>>> z.shape
(4, 1)

You can also see that NumPy doesn't copy any data, it just changes the iteration over the same memory internally

您还可以看到NumPy没有复制任何数据,它只是在内部对相同内存的迭代进行了更改。

>>> z.base is y
True

Read more here

点击这里了解更多内容

#2


2  

You can use np.add and np.multiply :

你可以用np。添加和np。繁殖:

>>> np.add(y1,x)
array([[  1.,   2.,   3.],
       [  5.,   6.,   7.],
       [  9.,  10.,  11.],
       [ 13.,  14.,  15.]])
>>> np.multiply(y1,x)
array([[  0.,   1.,   2.],
       [  6.,   8.,  10.],
       [ 18.,  21.,  24.],
       [ 36.,  40.,  44.]])

OR for in-place changing you can use iadd and imul methods of a numpy ndarray :

或者对于就地更改,您可以使用iadd和imul方法的numpy ndarray:

>>> x.__iadd__(y1)
array([[ 1,  2,  3],
       [ 5,  6,  7],
       [ 9, 10, 11],
       [13, 14, 15]])
>>> x = np.arange(12).reshape((4, 3))
>>> x.__imul__(y1)
array([[ 0,  1,  2],
       [ 6,  8, 10],
       [18, 21, 24],
       [36, 40, 44]])

But note that :

但注意:

In place operations will perform the calculation using the precision decided by the data type of the two operands, but will silently downcast the result (if necessary) so it can fit back into the array. Therefore, for mixed precision calculations, A {op}= B can be different than A = A {op} B. For example, suppose a = ones((3,3)). Then, a += 3j is different than a = a + 3j: while they both perform the same computation, a += 3 casts the result to fit back in a, whereas a = a + 3j re-binds the name a to the result.

在适当的位置上,操作将使用两个操作数的数据类型决定的精度来执行计算,但将无声地将结果降下来(如果需要),以便它能够重新适合于数组。因此,对于混合精度计算,A {op}= B可以与A = A {op} B不同,例如,假设A = 1(3,3)。然后,a += 3j与a = a + 3j不同:当它们执行相同的计算时,a += 3将结果转换回a中,而a = a + 3j将名称a重新绑定到结果中。

Read more about arithmetic-and-comparison-operations

阅读更多关于arithmetic-and-comparison-operations