Python Numpy从2个数组中选择元素
我需要从2个数组中选择n个项,以便索引相同。例如,我需要从x中随机选择两个项目,从y中选择元素,使y选择的索引与x相同:Python Numpy从2个数组中选择元素,python,numpy,numpy-random,Python,Numpy,Numpy Random,我需要从2个数组中选择n个项,以便索引相同。例如,我需要从x中随机选择两个项目,从y中选择元素,使y选择的索引与x相同: x = np.asarray([0.1123,0.223,0.8873]) y = np.asarray([1,1,2]) x_chosen = np.random.choice(x,(2,)) 如果说x_selected最后是:x_selected=[0.1123,0.223]…那么我需要: y_chosen = [1,1] 我目前有一个解决方案…但我希望使用基本的
x = np.asarray([0.1123,0.223,0.8873])
y = np.asarray([1,1,2])
x_chosen = np.random.choice(x,(2,))
如果说x_selected
最后是:x_selected=[0.1123,0.223]…
那么我需要:
y_chosen = [1,1]
我目前有一个解决方案…但我希望使用基本的numpy或scipy函数,而不是我自己的基本上只有3行的函数,以保持我在该项目中的函数在范围内…我不希望在我的主代码中创建这种一次性变量:
x_index = np.asarray([i for i in range(len(x))])
x_chosen_indices = np.random.choice(x_index,(2,))
x_chosen = x[x_chosen_indices]
y_chosen = y[x_chosen_indices]
或者,我可以堆叠、选择和分割…但我想这仍然会留下一个一次性函数,我必须坚持在某个地方…或者4-5行没有意义的代码…你可以使用
numpy.random.permutation
创建一个无序的索引数组。然后,您可以选择任意多个随机选择的常用元素:
import numpy as np
x = np.asarray([0.1123,0.223,0.8873])
y = np.asarray([1,1,2])
indices= np.random.permutation(np.size(x, 0))
x_chosen = x[indices[:2]]
y_chosen = y[indices[:2]]
print(x_chosen)
>>>[ 0.8873 0.1123]
print(y_chosen)
>>>[2 1]
您可以使用
numpy.random.permutation
创建索引的无序数组。然后,您可以选择任意多个随机选择的常用元素:
import numpy as np
x = np.asarray([0.1123,0.223,0.8873])
y = np.asarray([1,1,2])
indices= np.random.permutation(np.size(x, 0))
x_chosen = x[indices[:2]]
y_chosen = y[indices[:2]]
print(x_chosen)
>>>[ 0.8873 0.1123]
print(y_chosen)
>>>[2 1]
首先选择指数如何:
import numpy as np
choice = np.random.choice(np.array(range(len(x))), 2)
然后根据它们进行选择:
x_chosen, y_chosen = x[choice], y[choice]
首先选择指数如何:
import numpy as np
choice = np.random.choice(np.array(range(len(x))), 2)
然后根据它们进行选择:
x_chosen, y_chosen = x[choice], y[choice]
这里有两种我认为可能有效的方法。第一种方法与上面的方法大致相同,但它确实去掉了一行:
index = np.random.choice(np.arange(len(x)), 2, replace=False)
x_chosen = x[index]
y_chosen = y[index]
In [15]: x_chosen
Out[15]: array([ 0.8873, 0.223 ])
我不确定这是否是您想要的,但它是一个一行程序,将为您提供(x,y)元组:
这里有两种我认为可能有效的方法。第一种方法与上面的方法大致相同,但它确实去掉了一行:
index = np.random.choice(np.arange(len(x)), 2, replace=False)
x_chosen = x[index]
y_chosen = y[index]
In [15]: x_chosen
Out[15]: array([ 0.8873, 0.223 ])
我不确定这是否是您想要的,但它是一个一行程序,将为您提供(x,y)元组:
使用
np.random.randint()
查找索引:
x = np.asarray([0.1123,0.223,0.8873])
y = np.asarray([1,1,2])
indices = np.random.randint(0, x.size, 2)
>>> x[indices]
array([ 0.1123, 0.223 ])
>>> y[indices]
array([1, 1])
编辑
正如B.M评论中提到的,使用np.random.choice(..,replace=False)
避免多次使用同一索引:
indices = np.random.choice(x.size, 2, replace=False)
使用
np.random.randint()
查找索引:
x = np.asarray([0.1123,0.223,0.8873])
y = np.asarray([1,1,2])
indices = np.random.randint(0, x.size, 2)
>>> x[indices]
array([ 0.1123, 0.223 ])
>>> y[indices]
array([1, 1])
编辑
正如B.M评论中提到的,使用np.random.choice(..,replace=False)
避免多次使用同一索引:
indices = np.random.choice(x.size, 2, replace=False)
choice()
:np.random.choice(10,3)
-->array([3,0,3])
你可以用np.random.choice(len(x),2,replace=False)避免这种情况。choice()
np.random.choice(10,3)-->数组([3,0,3])
你可以用np random.choice(len避免这种情况,2,replace=False)。