-1

Let's say I have two arrays: SMALL_ARRAY and LARGE_ARRAY The LARGE_ARRAY contains values that are similar in value (not necessarily the same) I would like to get a subarray of the LARGE_ARRAY that:

= Has same size as SMALL_ARRAY

= Has similar values (similar distribution) as SMALL_ARRAY

let's assume small = [1,2,3,4] and large = [100,1.8,32,4.1,5,55,34,2.9,1.1,99]

I would like my new array to be [1.1,1.8,2.9,4.1]

so it has same size and similar elements to small

Any assistance please? Many thanks

french
  • 13
  • 1
  • 5
  • welcome, to stackoverflow! can you post the needed section from your code for better understanding/testing – Ari Gold Oct 11 '16 at 10:09
  • Please clarify what you mean by "array" (Python has a list of lists and numpy arrays), "similar in value," and "similar distribution." – Rory Daulton Oct 11 '16 at 10:30
  • let's assume it's a numpy array let's assume small = [1,2,3,4] and large = [100,1.8,32,4.1,5,55,34,2.9,1.1,99] I would like my new array to be [1.1,1.8,2.9,4.1] so it has same size and similar elements to small – french Oct 11 '16 at 11:12

2 Answers2

0

Based on 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)
Community
  • 1
  • 1
Sardorbek Imomaliev
  • 14,861
  • 2
  • 51
  • 63
0

numpy.random.choice is your friend if you want to subsample uniformly at random:

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
Paul Brodersen
  • 11,221
  • 21
  • 38
  • let's assume small = [1,2,3,4] and large = [100,1.8,32,4.1,5,55,34,2.9,1.1,99] I would like my new array to be [1.1,1.8,2.9,4.1] so it has same size and similar elements to small – french Oct 11 '16 at 11:16