如何在 python 中替换 3d 数组中的样本?

How to replace the samples in a 3d array in python?

我有一个维度为 (1000, 300, 3) 的 3d 数组,my_3d_array。

如果这个数组然后用修改后的版本替换旧版本,我想修改每个示例。换句话说,我想以第一个例子 (300, 3) (我们称它为旧样本)通过调用一个名为 my_function 的函数对其进行一些修改,然后获得的新样本将替换旧样本my_3d_array,依此类推。 请问怎么办?

我不太确定这是你想要的,但你可以使用正常的索引符号:

# just an example array
my_3d_array = np.random.random((1000,300,3))

# extract the old sample
old_sample = my_3d_array[0,:,:]

# transform the sample
new_sample = my_function(old_sample)

# write the sample back to the original array
my_3d_array[0,:,:] = new_sample

如果您想对所有样本进行相同的转换,向量化 my_function 以一次处理所有样本而不是使用 python 循环可能会更好。