我有一个向量列表:
>>> import numpy as np
>>> num_dim, num_data = 10, 5
>>> data = np.random.rand(num_data, num_dim)
>>> data
array([[ 0.0498063 , 0.18659463, 0.30563225, 0.99681495, 0.35692358,
0.47759707, 0.85755606, 0.39373145, 0.54677259, 0.5168117 ],
[ 0.18034536, 0.25935541, 0.79718771, 0.28604057, 0.17165293,
0.90277904, 0.94016733, 0.15689765, 0.79758063, 0.41250143],
[ 0.80716045, 0.84998745, 0.17893211, 0.36206016, 0.69604008,
0.27249491, 0.92570247, 0.446499 , 0.34424945, 0.08576628],
[ 0.35311449, 0.67901964, 0.71023927, 0.03120829, 0.72864953,
0.60717032, 0.8020118 , 0.36047207, 0.46362718, 0.12441942],
[ 0.1955419 , 0.02702753, 0.76828842, 0.5438226 , 0.69407709,
0.20865243, 0.12783666, 0.81486189, 0.95583274, 0.30157658]])
从data
,我需要随机选择3个向量,我可以用:
>>> import random
>>> random.sample(data, 3)
[array([ 0.80716045, 0.84998745, 0.17893211, 0.36206016, 0.69604008,
0.27249491, 0.92570247, 0.446499 , 0.34424945, 0.08576628]), array([ 0.18034536, 0.25935541, 0.79718771, 0.28604057, 0.17165293,
0.90277904, 0.94016733, 0.15689765, 0.79758063, 0.41250143]), array([ 0.35311449, 0.67901964, 0.71023927, 0.03120829, 0.72864953,
0.60717032, 0.8020118 , 0.36047207, 0.46362718, 0.12441942])]
我已在http://docs.scipy.org/doc/numpy/reference/routines.random.html检查了文档,但我无法确定numpy
中是否存在random.sample()
这样的功能。
numpy.random.sample()
是否与random.sample()
相同?
random.sample()
中的numpy
是否相等?
答案 0 :(得分:7)
正如@ayhan所证实的那样,可以这样做:
>>> data[np.random.choice(len(data), size=3, replace=False)]
array([[ 0.80716045, 0.84998745, 0.17893211, 0.36206016, 0.69604008,
0.27249491, 0.92570247, 0.446499 , 0.34424945, 0.08576628],
[ 0.35311449, 0.67901964, 0.71023927, 0.03120829, 0.72864953,
0.60717032, 0.8020118 , 0.36047207, 0.46362718, 0.12441942],
[ 0.1955419 , 0.02702753, 0.76828842, 0.5438226 , 0.69407709,
0.20865243, 0.12783666, 0.81486189, 0.95583274, 0.30157658]])
来自docs:
numpy.random.choice(a,size = None,replace = True,p = None)
从给定的1-D数组生成随机样本
np.random.choice(data, size=3, replace=False)
从data
的索引列表中选择3个元素,而无需替换。
然后data[...]
对索引进行切片并检索使用np.random.choice
选择的索引。