将二维数组中的每个值乘以另一个二维数组中的对应值

Multiply each value in a 2-D array by corresponding values in another 2-D array

如标题所示,我正在尝试将二维数组中的每个值乘以另一个二维数组中的相应值。我可以做到这一点,并为此编写了以下代码。但是我的问题是它花费的时间太长,因为每个二维数组包含 1000 个数组,其中包含 15289 个数字。我必须这样做三次,因为我有三个这样的二维数组。目前,完成所有这些操作需要一分钟(运行 以下代码大约需要 20 秒)。这太长了,因为我有 100 组数据 运行 通过我的整个脚本,每个脚本包含 3 个这样的二维数组。如果我能将这 20 秒缩短,那么在漫长的 运行 中会为我节省很多时间,因为其他一切 运行 都很顺利!

e_data = [[i*j for i,j in y] for y in np.dstack((e_data,sens_function))]    

e_data 是我的射电通量值(对于那里的任何射电天文学家),sens_function 是乘法中的另一个数组(这将使我的 e_data 成为我的单位要求)。非常感谢任何帮助或建议!

我认为您使用嵌套 for 循环和 dstack 过于复杂。您可以只使用 *(乘法)运算符。对于二维数组,它将执行逐元素乘法。请参阅以下示例:

e_data = np.arange(9).reshape(3,3)
print (arr1)
# [[0 1 2]
# [3 4 5]
# [6 7 8]]

sens_function = np.arange(9).reshape(3,3)
print (arr2)
# [[0 1 2]
#  [3 4 5]
# [6 7 8]]

result = e_data*sens_function
print (result)

# [[ 0  1  4]
# [ 9 16 25]
# [36 49 64]]

您正在执行 element-wise multiplication, which is a numpy method:

e_data = np.multiply(e_data, sens_function)