1. 基于random模块

  • random.sample(population, k)

从 population 中不放回抽取 k 个元素

  • population:可以为可迭代是的数据对象,如listset
  • k:随机抽取的样本数量
  • 不放回抽样
import random
N = range(10)
m = 3
a = random.sample(N, m)
print(a)
# 从0~9的序列中采样了3个样本
Out: [0, 5, 8]

2. 基于numpy模块

  • numpy.random.choice(a, size=None, replace=True, p=None)
Help on built-in function choice:
choice(...) method of numpy.random.mtrand.RandomState instancechoice(a, size=None, replace=True, p=None)Generates a random sample from a given 1-D array.. versionadded:: 1.7.0.. note::New code should use the ``choice`` method of a ``default_rng()``instance instead; see `random-quick-start`.Parameters----------a : 1-D array-like or intIf an ndarray, a random sample is generated from its elements.If an int, the random sample is generated as if a were np.arange(a)size : int or tuple of ints, optionalOutput shape.  If the given shape is, e.g., ``(m, n, k)``, then``m * n * k`` samples are drawn.  Default is None, in which case asingle value is returned.replace : boolean, optionalWhether the sample is with or without replacementp : 1-D array-like, optionalThe probabilities associated with each entry in a.If not given the sample assumes a uniform distribution over allentries in a.Returns-------samples : single item or ndarrayThe generated random samplesRaises------ValueErrorIf a is an int and less than zero, if a or p are not 1-dimensional,if a is an array-like of size 0, if p is not a vector ofprobabilities, if a and p have different lengths, or ifreplace=False and the sample size is greater than the populationsizeSee Also--------randint, shuffle, permutationGenerator.choice: which should be used in new codeNotes-----Sampling random rows from a 2-D array is not possible with this function,but is possible with `Generator.choice` through its ``axis`` keyword.Examples--------Generate a uniform random sample from np.arange(5) of size 3:>>> np.random.choice(5, 3)array([0, 3, 4]) # random>>> #This is equivalent to np.random.randint(0,5,3)Generate a non-uniform random sample from np.arange(5) of size 3:>>> np.random.choice(5, 3, p=[0.1, 0, 0.3, 0.6, 0])array([3, 3, 0]) # randomGenerate a uniform random sample from np.arange(5) of size 3 withoutreplacement:>>> np.random.choice(5, 3, replace=False)array([3,1,0]) # random>>> #This is equivalent to np.random.permutation(np.arange(5))[:3]Generate a non-uniform random sample from np.arange(5) of size3 without replacement:>>> np.random.choice(5, 3, replace=False, p=[0.1, 0, 0.3, 0.6, 0])array([2, 3, 0]) # randomAny of the above can be repeated with an arbitrary array-likeinstead of just integers. For instance:>>> aa_milne_arr = ['pooh', 'rabbit', 'piglet', 'Christopher']>>> np.random.choice(aa_milne_arr, 5, p=[0.5, 0.1, 0.1, 0.3])array(['pooh', 'pooh', 'pooh', 'Christopher', 'piglet'], # randomdtype='<U11')

[Python] 随机抽样相关推荐

  1. python随机抽样_掌握python中的随机抽样

    python随机抽样 Python provides many useful tools for random sampling as well as functions for generating ...

  2. python随机抽样numpy_随机抽样 (`numpy.random`) | NumPy 中文

    # 随机抽样 (numpy.random) Numpy的随机数例程使用 BitGenerator 和 Generator 的组合来生成伪随机数以创建序列,并使用这些序列从不同的统计分布中进行采样: B ...

  3. python随机抽样numpy_python numpy之np.random的随机数函数使用介绍

    np.random的随机数函数(1) 函数 说明 rand(d0,d1,..,dn) 根据d0‐dn创建随机数数组,浮点数, [0,1),均匀分布 randn(d0,d1,..,dn) 根据d0‐dn ...

  4. python分布采样_基于分布的python随机抽样

    在进入主题之前,让我们先看一下python的默认采样方法>>> import random >>> c=[1,2,3,100,101,102,103,104,105 ...

  5. python随机抽取样本1500个_(python)随机抽样

    随机抽样法就是调查对象总体中每个部分都有同等被抽中的可能,是一种完全依照机会均等的原则进行的抽样调查,被称为是一种"等概率".随机抽样有四种基本形式,即简单随机抽样.等距抽样.类型 ...

  6. 随机抽样的python实现

    目录 1  随机抽样 1.1  离散型随机变量 1.1.1  二项分布 1.1.2  泊松分布 1.1.3  超几何分布 1.2  连续型随机变量 1.2.1  均匀分布 1.2.2  正态分布 1. ...

  7. python之Numpy随机抽样

    随机抽样 前言 一.随机模块 二.离散型随机变量 二项分布 计算期望和方差 泊松分布 超几何分布 三.连续型随机变量 均匀分布 四.正态分布 五.指数分布 其他随机函数 前言 numpy.random ...

  8. 【深度学习】python实现等距随机抽样

    在实际的样本抽帧需求中,往往有这样一种需求,例如,从20帧到75帧,抽取29帧数据,如果使用python自带的randint 函数,往往抽取的数据间隔是不一致的,这就需要等距随机抽样. 等距随机抽样就 ...

  9. python进行excel随机抽样

    最近需要对多个excel中数万条数据进行随机抽样,采用python进行. excel读取和写入使用openpyxl第三方库. 随机抽样使用random.sample函数. (抽取遇到的最大问题是不知道 ...

最新文章

  1. 你见过最差的算法工程师什么样?
  2. 搭建本地YUM源服务器
  3. javascript删除元素节点
  4. 基于Hadoop生态技术构建阿里搜索离线系统
  5. 攻防演练中的业务逻辑漏洞及检测思路
  6. Cookie中不能有空格_PHP 使用cookie
  7. html5列表菜单特效,HTML5 SVG汉堡包菜单按钮分段动画特效
  8. 帝国cms listinfo.php,帝国CMS动态列表应用之在列表中显示指定的会员组会员发布的信息...
  9. Kubernetes-存活探针(liveness probe)(十六)
  10. 华为云服务产品之精华问题大集烩(四)
  11. python-requests数据驱动延伸
  12. C++ ---------------- 成员函数指针揭秘
  13. 解决12c安装过程中的各种报错
  14. Python 办公自动化之全网最强最详细PDF 文件操作手册
  15. 英语连读 Word Connections
  16. 解决CSDN上传资源出现报错:“该资源已存在,请重新上传”
  17. Visual Studio内存泄露检测工具
  18. 全球最受欢迎电商平台有哪些?这些平台怎么快速增加销量?
  19. IAssemblyDoc Interface 学习笔记
  20. gif文件太大怎么办?gif动图如何在线压缩?

热门文章

  1. 例题 9-27 方块消除(Blocks, UVa10559)
  2. java递归获取所有的子级节点
  3. 刘鹏教授在淮安市应急管理局作报告
  4. python实现摩斯密码解密
  5. Android 插件开发实现
  6. 2022鲁大师评测沟通会开启汽车评测新赛道
  7. impala常见错误
  8. 人脸识别、深度学习优秀设计(毕业专业指导)
  9. 情感分类——Attention(前篇续)
  10. 三运放差分放大电路分析_★三运放差分放大电路