python random.sample()方法,随机不重复抽样

random.sample()方法

如果要深入去研究机器学习或深度学习的算法,那么就大概率会遇到python模块random中的方法sample(),该函数可以用于对一个样本数据集进行随机的而且是不重复的抽样,并形成新的序列。是不是跟统计学的方法很相似!random.sample()的语法如下:


语法

random.sample(population, k)

参数解析表:

参数描述
population数据集,可以是有序的也可以是无序的python序列,比如列表list、元组tuple、集合set等
k抽样次数,与返回值中的元素个数息息相关,注意k值,也就是抽样次数不能大于序列的元素个数,否则python抛出ValueError

返回值

新的python列表list,不会修改原来的python序列。


random.sample()实例代码

>>> import random
>>> list1 = list(range(11))
>>> list1
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> random.sample(list1,5)
[10, 1, 8, 2, 6]
>>> tup = tuple(range(100))
>>> random.sample(tup,15)
[34, 31, 26, 7, 54, 91, 97, 4, 46, 22, 86, 3, 10, 14, 8]
>>> set1 = set(range(1000))
>>> random.sample(set1, 8)
[25, 41, 746, 940, 21, 382, 261, 130]
>>> random.sample(list1,12) #如果k抽样次数大于序列的元素个数,python抛出ValueError
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/anaconda3/lib/python3.7/random.py", line 321, in sample
    raise ValueError("Sample larger than population or is negative")
ValueError: Sample larger than population or is negative

全栈后端 / Python库 :









Copyright © 2022-2024 笨鸟工具 x1y1z1.com All Rights Reserved.