有没有一种“pythonic”的方法来清洁下来-样本没有多个for循环?
下面的示例是我希望去掉的for循环的类型。
最低工作实例:
import numpy as np
unsampled_array = [1,3,5,7,9,11,13,15,17,19]
number_of_samples = 7
downsampled_array = []
downsampling_indices = np.linspace(0, len(unsampled_array)-1, number_of_samples).round()
for index in downsampling_indices:
downsampled_array.append(unsampled_array[int(index)])
print(downsampled_array)结果:
>>> [ 1 5 7 9 13 17 19]发布于 2019-02-25 02:31:48
您需要函数np.ix_,如下所示:
import numpy as np
unsampled_array = np.array([1,3,5,7,9,11,13,15,17,19])
number_of_samples = 5
downsampling_indices = np.linspace(0, len(unsampled_array)-1, number_of_samples).round()
downsampling_indices = np.array(downsampling_indices, dtype=np.int64)
indices = np.ix_(downsampling_indices)
downsampled_array = unsampled_array[indices]
print(downsampled_array)发布于 2019-02-25 02:33:48
如果您想要“实际”下采样,其中每个值都是k值的平均值,则可以使用
unsampled_array.reshape(-1, k).mean(1) 确保unsampled_array是np.array。在你的例子中,k=2。这会给你:
2.6.10.14.18。
* Update:如果您只想获取每个k项的第一项,可以使用以下代码:
unsampled_array.reshape(-1, 2)[:, 0]看看这个情节:

https://stackoverflow.com/questions/54858623
复制相似问题