Python 在numpy中随机选择索引位置

Python 在numpy中随机选择索引位置,python,numpy,Python,Numpy,从所需的中我如何才能随机选择(不重复出现)3个索引位置 答案应该是索引位置,作为要求的子集。 我的审判: import numpy as np data = np.array([[0,1,2,3,4,7,6,7,8,9,10], [3,3,3,4,7,7,7,8,11,12,11], [3,3,3,5,7,7,7,9,11,11,11], [3,4,3,6,7,7,7,10,11,17,11], [4,5,6,7,7,9,10,11,11,11,11]])

从所需的我如何才能随机选择(不重复出现)3个索引位置

答案应该是索引位置,作为要求的子集。

我的审判:

import numpy as np
data  = np.array([[0,1,2,3,4,7,6,7,8,9,10], 
    [3,3,3,4,7,7,7,8,11,12,11],  
    [3,3,3,5,7,7,7,9,11,11,11],
    [3,4,3,6,7,7,7,10,11,17,11],
    [4,5,6,7,7,9,10,11,11,11,11]])
required = np.where(data==11)
print required

(array([1, 1, 2, 2, 2, 3, 3, 4, 4, 4, 4], dtype=int64), array([ 8, 10,  8,  9, 10,  8, 10,  7,  8,  9, 10], dtype=int64))
有什么办法可以解决这个问题吗?

这就可以了:

result = np.random.choice(required, 3, replace=False)
print result

ValueError: a must be 1-dimensional
len(coords)
是coords列表的长度(元素数)。假设
len(coords)
为5。我将它作为一个整数传递给np.random.choice,无需替换。因此,现在它必须从[0,1,2,3,4]中选择数字。假设在3次拾取之后,结果是[0,3,1]。然后,我将这些值作为coords本身的索引传递给coords列表,作为回报,coords列表将为名为
data
的2D数组保存过滤后的x和y索引。[编辑:进一步细化]
import numpy as np
data  = np.array([[0,1,2,3,4,7,6,7,8,9,10], 
     [3,3,3,4,7,7,7,8,11,12,11],  
     [3,3,3,5,7,7,7,9,11,11,11],
     [3,4,3,6,7,7,7,10,11,17,11],
     [4,5,6,7,7,9,10,11,11,11,11]])
required = np.where(data==11)

coords = zip(required[0], required[1]) #Create pairs of indices as tuples
for i in np.random.choice(len(coords), 3, replace=False): #Pick random index values for coords
    print coords[i] #May want to do something other than printing here.