user3235542
user3235542

Reputation:

randomly select the data from the dictionary form

import numpy as np
data= np.array([[0,1,2,3,4,7,6,7,8,9,10], 
        [10,3,10,4,7,7,7,8,11,12,11],  
        [10,10,3,5,7,7,7,9,11,11,11],
        [3,4,3,6,7,7,7,10,11,11,11],
        [4,5,6,7,7,9,10,11,11,11,11]], dtype='float')

my_groups = ['Group_A', 'Group_B', 'Group_C']
my_values = [7, 10, 11]
my_data ={}
for x, y in zip(my_groups, my_values):
    my_data[x] = np.where(data==y)
print my_data

#{'Group_C': (array([1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4], dtype=int64), array([ 8, 10,  8,  9, 10,  8,  9, 10,  7,  8,  9, 10], dtype=int64)), 'Group_B': (array([0, 1, 1, 2, 2, 3, 4], dtype=int64), array([10,  0,  2,  0,  1,  7,  6], dtype=int64)), 'Group_A': (array([0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4], dtype=int64), array([5, 7, 4, 5, 6, 4, 5, 6, 4, 5, 6, 3, 4], dtype=int64))}

Now, I want to randomly select three index positions for each group and put them into dictionary form:

samples = {}
for x,y in zip(my_groups,my_data):
    for i in np.random.choice(len(my_data), 3, replace=True):
        samples[x] = np.array(my_data[x][i], dtype=np.int64)

print samples

Looking for good ideas. I could not make the samples working.

EDIT: This is for consistency check:

import numpy as np
data= np.array([[0,1,2,3,4,7,6,7,8,9,10], 
        [10,3,10,4,7,7,7,8,300,12,11],  
        [300,10,100,5,7,7,7,9,200,11,11],
        [3,4,3,6,7,200,7,100,11,11,11],
        [4,5,6,7,7,9,10,11,11,11,11]], dtype='float')

my_groups = ['Group_A', 'Group_B', 'Group_C']
my_values = [100, 200, 300]
my_data ={}
for x,y in zip(my_groups, my_values):
    my_data[x] = np.where(data==y)
print my_data

samples = {}
for x,y in my_data.iteritems():
    idx_choice = np.random.choice(len(y[0]),2, replace=False)
    samples[x] = (y[0][idx_choice],y[1][idx_choice])
print samples

samples = {}
for x, y in my_data.iteritems():
    samples[x] = [(y[0][i],y[1][i]) for i in np.random.choice(len(y[0]),2,replace=False)]
print samples

Upvotes: 2

Views: 56

Answers (1)

Acorbe
Acorbe

Reputation: 8391

If I understood correctly, you want to randomly select three pairs of indexes (as returned by np.where) for each group you have determined.

This can be easily done e.g. via a list comprehension.

Consider this

 samples = {}
 for x,y in my_data.iteritems():
     samples[x] = [(y[0][i],y[1][i]) for i in np.random.choice(len(y[0]),3)]

output

 samples
 {'Group_A': [(2, 5), (3, 6), (1, 6)],
  'Group_B': [(3, 7), (3, 7), (2, 0)],
  'Group_C': [(2, 10), (2, 8), (2, 10)]}

EDIT: Alternatively, you might want an output closer to what returned by np.where. In that case you can do

samples = {}
for x,y in my_data.iteritems():
    idx_choice = np.random.choice(len(y[0]),3)
    samples[x] = (y[0][idx_choice],y[1][idx_choice])

which gives

samples
{'Group_A': (array([0, 3, 3]), array([7, 4, 5])),
'Group_B': (array([1, 1, 2]), array([2, 2, 1])),
'Group_C': (array([2, 3, 2]), array([9, 8, 9]))}

Upvotes: 2

Related Questions