python示例使用较小数组的大型数组

时间:2016-10-11 10:06:46

标签: python arrays

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

=与SMALL_ARRAY

的大小相同

=具有与SMALL_ARRAY

类似的值(类似分布)

让我们假设小= [1,2,3,4]和大= [100,1.8,32,4.1,5,55,34,2.9,1.1,99]

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

因此它具有与小

相同的大小和相似的元素

请帮忙吗? 非常感谢

2 个答案:

答案 0 :(得分:0)

基于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)

答案 1 :(得分:0)

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