使用 numpy.random.choice随机采样:

说明

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

示例:

>>> np.random.choice(5, 3)
array([0, 3, 4]) >>> np.random.choice(5, 3, p=[0.1, 0, 0.3, 0.6, 0])
array([3, 3, 0]) >>> np.random.choice(5, 3, replace=False)
array([3,1,0]) >>> np.random.choice(5, 3, replace=False, p=[0.1, 0, 0.3, 0.6, 0])
array([2, 3, 0]) >>> 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'],

1、按照指定概率采样:

#按照分布采样
def randomExample():
d1=np.array([0.2,0.3,0.5]) index=[]
for num in range(100000):
r = random.uniform(0, 1)
for i in range(1,4):
if r < sum(d1[0:i]):
index.append(i-1)
break
print(index)
print(sum(np.array(index)==0))
print(sum(np.array(index)==1))
print(sum(np.array(index)==2))

结果为:

  # [1,2,2,1,2,1,1,1,2,2,1,2,0,2,1,0,2,1,...
#
#
#

2、可以从一个int数字或1维array里随机选取内容,并将选取结果放入n维array中返回。

def randomByPro():
arr=np.random.choice(5, 3, p=[0.1, 0, 0.3, 0, 0.6])
print(arr)

结果为:

# array([3, 3, 0])

使用 numpy.random.choice随机采样的更多相关文章

  1. numpy.random.rand()/randn()/randint()/normal()/choice()/RandomState()

    这玩意用了很多次,但每次用还是容易混淆,今天来总结mark一下~~~ 1. numpy.random.rand(d0,d1,...,dn) 生成一个[0,1)之间的随机数或N维数组 np.random ...

  2. Python中的随机采样和概率分布(一)

    Python(包括其包Numpy)中包含了了许多概率算法,包括基础的随机采样以及许多经典的概率分布生成.我们这个系列介绍几个在机器学习中常用的概率函数.先来看最基础的功能--随机采样. 1. rand ...

  3. numpy.random 常用函数详解之简单随机数篇(Simple random data)

    1.numpy.random.rand(d0,d1,d2,...,dn) 参数:d0,d1,d2,...,dn 须是正整数,用来描述生成随机数组的维度.如(3,2)代表生成3行2列的随机数组. 返回值 ...

  4. np.random.random()函数 参数用法以及numpy.random系列函数大全

    原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/9751471.html 1.np.random.random()函数参数 np.random.r ...

  5. Python学习——numpy.random

    numpy.random.rand numpy.random模块作用是生成随机数,其中numpy.random.rand(d0, d1, ..., dn):生成一个[0,1)之间的随机浮点数或N维浮点 ...

  6. numpy.random模块常用函数解析

    numpy.random模块中常用函数解析 numpy.random模块官方文档 1. numpy.random.rand(d0, d1, ..., dn)Create an array of the ...

  7. 【转】np.random.random()函数 参数用法以及numpy.random系列函数大全

    转自:https://www.cnblogs.com/DOMLX/p/9751471.html 1.np.random.random()函数参数 np.random.random((1000, 20) ...

  8. [转]numpy.random.randn()用法

    在python数据分析的学习和应用过程中,经常需要用到numpy的随机函数,由于随机函数random的功能比较多,经常会混淆或记不住,下面我们一起来汇总学习下. import numpy as np ...

  9. NumPy的随机函数子库——numpy.random

    NumPy的随机函数子库numpy.random 导入模块:import numpy as np 1.numpy.random.rand(d0,d1,...,dn) 生成一个shape为(d0,d1, ...

随机推荐

  1. Dictionary 的几种遍历方法

    Dictionary 的几种遍历方法 Dictionary<string, int>dic = newDictionary<string, int>(); 方法1 foreac ...

  2. MySQL数据库localhost的root用户登陆遭遇失败

    问题:Access denied for user 'root'@'localhost' (using password: YES)打开MySQL目录下的my.ini文件(Linux的话是/etc/m ...

  3. CHtmlEditCtrl (3): More HTML Editor Options

    In this version of our HTML Editor, we'll create a floating source view/edit window and we'll implem ...

  4. 阿里云centos安装ftp与svn过程

    1.下载xshell或者secureCRT 2.登录centos或者服务器 3.安装vsftpd [root@xxx]# yum install vsftpd //安装vsftpd [root@xxx ...

  5. C#调用MySQL数据库(使用MySql.Data.dll连接)mysql-connector-net-6.10.4.msi

    下载地址:http://dev.mysql.com/downloads/connector/net/ 安装指导 1.安装:mysql-connector-net-6.10.4.msi 其下载地址:ht ...

  6. RocketMQ【未完成】

    RocketMQ 是一款快速地.可靠地.分布式.容易使用的消息中间件,由 alibaba 开发,其前身是 metaq,metaq 的第一个版本是可以看成是linkedin的kafka(scala)的j ...

  7. Spring+hibernate+struts错题集

    1.严重: Exception starting filter struts2 java.lang.ClassNotFoundException: org.apache.struts2.dispatc ...

  8. CMUSphinx Learn - Basic concepts of speech

    Basic concepts of speech Speech is a complex phenomenon. People rarely understand how is it produced ...

  9. Postgresql中的数据类型大全

    一.数值类型: 下面是PostgreSQL所支持的数值类型的列表和简单说明: 名字 存储空间 描述 范围 smallint 2 字节 小范围整数 -32768 到 +32767 integer 4 字 ...

  10. TortoiseGit使用指南

    http://www.360doc.com/content/13/0424/17/9171956_280649187.shtml