如何使用numpy从列表中随机选择n个元素?

编程入门 行业动态 更新时间:2024-10-18 08:37:47
本文介绍了如何使用numpy从列表中随机选择n个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个向量列表:

>>>将 numpy 导入为 np>>>num_dim, num_data = 10, 5>>>数据 = np.random.rand(num_data, num_dim)>>>数据数组([[ 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 个向量,我可以这样做:

>>>随机导入>>>随机样本(数据,3)[数组([ 0.80716045, 0.84998745, 0.17893211, 0.36206016, 0.69604008,0.27249491, 0.92570247, 0.446499, 0.34424945, 0.08576628]), 数组([ 0.18034536, 0.25935541, 0.71,6,56,70.570.771870.08576628])0.90277904, 0.94016733, 0.15689765, 0.79758063, 0.41250143]), 数组([ 0.35311449, 0.67901964, 0.71058063, 0.41250143])0.60717032, 0.8020118, 0.36047207, 0.46362718, 0.12441942])]

我在 docs.scipy 查看了文档/doc/numpy/reference/routines.random.html 我无法弄清楚 numpy 中是否有这样的功能 random.sample().

numpy.random.sample() 与 random.sample() 是不是一样的?

numpy 中是否有 random.sample() 的等价物?

解决方案

正如@ayhan 所确认的,可以这样做:

>>>数据[np.random.choice(len(data), size=3, replace=False)]数组([[ 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]])

来自文档:

numpy.random.choice(a, size=None, replace=True, p=None)

从给定的一维数组中生成随机样本

np.random.choice(data, size=3, replace=False) 从 data 的索引列表中选择 3 个没有替换的元素.>

然后 data[...] 对索引进行切片并检索使用 np.random.choice 选择的索引.

I have a list of vectors:

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

From the data, I need to randomly pick 3 vectors, I could do it with:

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

I've checked the docs at docs.scipy/doc/numpy/reference/routines.random.html and I couldn't figure out whether there is such a functionality in numpy as random.sample().

Is it right that the numpy.random.sample() isn't the same as random.sample()?

Is there an equivalence of random.sample() in numpy?

解决方案

As @ayhan confirmed, it can be done as such:

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

From the docs:

numpy.random.choice(a, size=None, replace=True, p=None)

Generates a random sample from a given 1-D array

The np.random.choice(data, size=3, replace=False) selects 3 elements from the list of indices of the data without replacement.

Then data[...] slices the index and retrieve the indices selected with np.random.choice.

更多推荐

如何使用numpy从列表中随机选择n个元素?

本文发布于:2023-11-29 12:39:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646418.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   元素   列表中   numpy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!