Python样本大数组使用较小的数组



假设我有两个数组:SMALL_ARRAY和LARGE_ARRAYLARGE_ARRAY包含值相似的值(不一定相同)我想获得LARGE_ARRAY的子数组:

=与SMALL_ARRAY大小相同

=与SMALL_ARRAY

具有相似的值(相似的分布)

假设小=(1、2、3、4)和大=(1.8,100年32岁,4.1,5,55岁,34岁,2.9,1.1,99]

我希望我的新数组为[1.1,1.8,2.9,4.1]

有相同的大小和相似的元素

需要帮忙吗?非常感谢

基于https://stackoverflow.com/a/8914682/3627387

LARGE_ARRAY = [100,1.8,32,4.1,5,55,34,2.9,1.1,99]
SMALL_ARRAY = [1,2,3,4]
similar = []
for i in SMALL_ARRAY:
    diff_list = [(abs(i - x), x) for x in LARGE_ARRAY]
    diff_list.sort()
    similar.append(diff_list[0][1])
print(similar)

numpy.random.choice是你的朋友,如果你想均匀地随机抽样:

import numpy as np
# 'initialise' small so we know its shape
# obviously you can skip that step if you already know the shape
m, n = 5, 10
small = np.zeros((m, n))
# get a sample from a large array
large = np.random.randn(100,1000)
samples = np.random.choice(large.ravel(), size=m*n)
#  small
small = samples.reshape(small.shape)
print small

最新更新