基于int-array python从位数组中选择两行
我有两个数组,一个是Int,一个是bit基于int-array python从位数组中选择两行,python,python-3.x,numpy,for-loop,Python,Python 3.x,Numpy,For Loop,我有两个数组,一个是Int,一个是bit s = [ [1] x = [ [1 0 0 0 0] [4] [1 1 1 1 0] [9] [0 1 1 1 0] [0] [0 0 1 0 0] [3] ] [0 1 1 0 0]] 我想找到s random给定中最小的两个元素,然后根据s数组从x random给定中选择并打印两行,
s = [ [1] x = [ [1 0 0 0 0]
[4] [1 1 1 1 0]
[9] [0 1 1 1 0]
[0] [0 0 1 0 0]
[3] ] [0 1 1 0 0]]
我想找到s random给定中最小的两个元素,然后根据s数组从x random给定中选择并打印两行,
例如,s[i]中的最小元素是s[3]=0,s[0]=1,因此我要选择x[3][0 01 0]和x[0][1 0]
import numpy as np
np.set_printoptions(threshold=np.nan)
s= np.random.randint(5, size=(5))
x= np.random.randint (2, size=(5, 5))
print (s)
print (x)
我尽了最大努力使用for循环,但没有成功,任何建议都将不胜感激。您可以使用从s中找出两个最小元素的索引,并将其用作子集x的行索引:
请分享您在for循环上的努力,以便我们可以帮助您修复或更正它。我也很喜欢,但不太清楚,否则我将分享它,非常感谢您的帮助
s
# array([3, 0, 0, 1, 2])
x
# array([[1, 0, 0, 0, 1],
# [1, 0, 1, 1, 1],
# [0, 0, 1, 0, 0],
# [1, 0, 0, 1, 1],
# [0, 0, 1, 0, 1]])
x[s.argpartition(2)[:2], :]
# array([[1, 0, 1, 1, 1],
# [0, 0, 1, 0, 0]])