gpt4 book ai didi

python - 随机选择numpy中的索引位置

转载 作者:太空宇宙 更新时间:2023-11-03 18:26:48 25 4
gpt4 key购买 nike

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

如何从必需中随机选择(不重复出现)仅 3 个指数位置?

答案应该是索引位置作为所需的子集。

我的试用:

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

ValueError: a must be 1-dimensional

有什么办法可以解决这个问题吗???

最佳答案

这可以完成工作:

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.

关于python - 随机选择numpy中的索引位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23025920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com