我希望预先分配一个数组操作输出的内存空间,并需要知道要将其设置为什么数据类型。下面是我想要实现的函数,但是它的样子很丑。
import numpy as np
def array_operation(arr1, arr2):
out_shape = arr1.shape
# Get the dtype of the output, these lines are the ones I want to replace.
index1 = ([0],) * arr1.ndim
index2 = ([0],) * arr2.ndim
tmp_arr = arr1[index1] * arr2[index2]
out_dtype = tmp_arr.dtype
# All so I can do the following.
out_arr = np.empty(out_shape, out_dtype)
上述代码看起来相当不简洁。numpy是否有一个能够完成此任务的函数?