如何在numpy中将数组除以其他数组元素?

问题描述 投票:-1回答:4

我有两个数组,我希望一个数组的所有元素都被第二个数组除。例如,

In [24]: a = np.array([1,2,3])                                                      

In [25]: b = np.array([1,2,3])                                                      

In [26]: a/b                                                                        
Out[26]: array([1., 1., 1.])

In [27]: 1/b                                                                        
Out[27]: array([1.        , 0.5       , 0.33333333])

enter image description here

这不是我想要的答案,我想要的输出就像(我们可以看到a的所有元素都被b所除)

In [28]: c = []                                                                     

In [29]: for i in a: 
    ...:     c.append(i/b) 
    ...:                                                                            

In [30]: c                                                                          
Out[30]: 
[array([1.        , 0.5       , 0.33333333]),
 array([2.        , 1.        , 0.66666667]),
In [34]: np.array(c)                                                                
Out[34]: 
array([[1.        , 0.5       , 0.33333333],
       [2.        , 1.        , 0.66666667],
       [3.        , 1.5       , 1.        ]])

enter image description here

但是我不喜欢for循环,对于大数据来说它太慢了,所以numpy包中是否包含一个函数或解决此问题的任何好方法(更快)?

python arrays division
4个回答
0
投票

首先,您需要将a转换为2D数组(形状与输出相同),然后对要循环的尺寸重复上述步骤。然后矢量化除法将起作用。

>>> a.reshape(-1,1)
array([[1],
       [2],
       [3]])

>>> a.reshape(-1,1).repeat(b.shape[0], axis=1) 
array([[1, 1, 1],
       [2, 2, 2],
       [3, 3, 3]])


>>> a.reshape(-1,1).repeat(b.shape[0], axis=1) / b
array([[1.        , 0.5       , 0.33333333],
       [2.        , 1.        , 0.66666667],
       [3.        , 1.5       , 1.        ]])


# Transpose will let you do it the other way around, but then you just get 1 for everything
>>> a.reshape(-1,1).repeat(b.shape[0], axis=1).T
array([[1, 2, 3],
       [1, 2, 3],
       [1, 2, 3]])

>>> a.reshape(-1,1).repeat(b.shape[0], axis=1).T / b
array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]])

0
投票

在纯numpy中很简单,您可以使用broadcasting来计算两个向量的outer product(或任何其他外部运算):

import numpy as np

a = np.arange(1, 4)
b = np.arange(1, 4)

c = a[:,np.newaxis] / b

# array([[1.        , 0.5       , 0.33333333],
#        [2.        , 1.        , 0.66666667],
#        [3.        , 1.5       , 1.        ]])

[这有效,因为a[:,np.newaxis](3,)形阵列a的尺寸增加到(3, 1)形阵列,可用于所需的广播操作。


0
投票

这应该完成工作:

import numpy as np

a = np.array([1, 2, 3])
b = np.array([1, 2, 3])

print(a.reshape(-1, 1) / b)

输出:

[[ 1.          0.5         0.33333333]
 [ 2.          1.          0.66666667]
 [ 3.          1.5         1.        ]]

-1
投票

感谢所有回答此问题的人。到目前为止,我认为“重塑”看起来不错。以下是我的答案。

In [39]: a                                                                          
Out[39]: array([1, 2, 3])

In [40]: a_r = a.reshape(3,1)                                                       

In [41]: b                                                                          
Out[41]: array([1, 2, 3])

In [42]: a_r/b                                                                      
Out[42]: 
array([[1.        , 0.5       , 0.33333333],
       [2.        , 1.        , 0.66666667],
       [3.        , 1.5       , 1.        ]])
© www.soinside.com 2019 - 2024. All rights reserved.