numpy教程:随机数模块numpy.random
来源:互联网 发布:dw软件图标 编辑:程序博客网 时间:2024/05/18 05:17
http://blog.csdn.net/pipisorry/article/details/39508417
随机数种子
RandomState
RandomState exposes a number of methods for generating random numbersdrawn from a variety of probability distributions.
使用示例
prng = np.random.RandomState(123456789) # 定义局部种子
prng.rand(2, 4)
prng.chisquare(1, size=(2, 2)) # 卡方分布
prng.standard_t(1, size=(2, 3)) # t 分布
prng.poisson(5, size=10) # 泊松分布
[概率与统计分析]
[class numpy.random.RandomState]
random.seed()
random.seed(123456789) # 种子不同,产生的随机数序列也不同,随机数种子都是全局种子
要每次产生随机数相同就要设置种子,相同种子数的Random对象,相同次数生成的随机数字是完全相同的;
random.seed(1)
这样random.randint(0,6, (4,5))每次都产生一样的4*5的随机矩阵
This method is called when RandomState is initialized. It can be called again to re-seed the generator.
[numpy.random.seed]
关于种子的介绍可参见[Java - 常用函数Random函数]
皮皮blog
numpy.random模块
linspace(start, end, num): 如linspace(0,1,11)结果为[0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1];
arange(n): 产生一个从0到n-1的向量,如arange(4)结果为[0,1,2,3]
简单随机生成数据相关函数
rand(d0, d1, ..., dn)Random values in a given shape.randn(d0, d1, ..., dn)Return a sample (or samples) from the “standard normal” distribution.randint(low[, high, size, dtype])Return random integers from low (inclusive) to high (exclusive).random_integers(low[, high, size])Random integers of type np.int between low and high, inclusive.random_sample([size])Return random floats in the half-open interval [0.0, 1.0).random([size])Return random floats in the half-open interval [0.0, 1.0).产生随机矩阵,如random.random([2,3])产生一个2x3维的随机数ranf([size])Return random floats in the half-open interval [0.0, 1.0).sample([size])Return random floats in the half-open interval [0.0, 1.0).choice(a[, size, replace, p])Generates a random sample from a given 1-D arraybytes(length)Return random bytes.[Simple random data¶]
np.random模块使用示例
np.random.rand(a, b)
from numpy import random
x = random.rand(2, 3)print(x)[[ 0.1169922 0.08614147 0.17997144] [ 0.5694889 0.43067372 0.62135592]]
x, y = random.rand(2, 3)print(x)print(y)[ 0.60527337 0.78765269 0.71884661][ 0.67420571 0.946359 0.7632273 ][numpy - 基本数据类型、多维数组ndarray及函数操作]
np.random.randint(a, b, size=(c, d))
raw_user_item_mat = random.randint(0, 10, size=(3,4)) #指定生成随机数范围和生成的多维数组大小print(raw_user_item_mat)[[3 6 2 8] [3 1 2 4] [9 4 5 0]]
高级随机生成数据函数
二项分布函数
超几何分布
在产品质量的不放回抽检中,若N件产品中有M件次品,抽检n件时所得次品数X=k,则P(X=k)=C(M,k)·C(N-M,n-k)/C(N,n), C(a b)为古典概型的组合形式,a为下限,b为上限,此时我们称随机变量X服从超几何分布(hypergeometric distribution)。
(1)超几何分布的模型是不放回抽样。
(2)超几何分布中的参数是M,N,n上述超几何分布记作X~H(N,n,M)。
#使用hypergeometric函数初始化游戏的结果矩阵。该函数的第一个参数为罐中普通球的数量,第二个参数为“倒霉球”的数量,第三个参数为每次采样(摸球)的数量。共进行超几何分布size次。返回size个抽样结果,也就是普通球(正品)的数目。
连续分布
连续分布可以用PDF(Probability Density Function,概率密度函数)来描述。随机变量落在某一区间内的概率等于概率密度函数在该区间的曲线下方的面积。随机数可以从正态分布中产生,它们的直方图能够直观地刻画正态分布。
import numpy as np
import matplotlib.pyplot as plt
#使用NumPy random模块中的normal函数产生指定数量的随机数。
N=10000
normal_values = np.random.normal(size=N) #lz一般使用stats.norm.rvs(loc=0, scale=0.1, size=10)来生成高斯分布随机数[Scipy教程 - 统计函数库scipy.stats]
#绘制分布直方图和理论上的概率密度函数(均值为0、方差为1的正态分布)曲线。dummy, bins, dummy = plt.hist(normal_values, np.sqrt(N), normed=True, lw=1)
sigma = 1
mu = 0
plt.plot(bins, 1/(sigma * np.sqrt(2 * np.pi)) * np.exp( - (bins -mu)**2 / (2 * sigma**2) ),lw=2) #lz提示,也可以使用scipy.stat.norm.pdf来生成非随机的高斯分布图[Scipy教程 - 统计函数库scipy.stats]
random应用实例
random_index = np.ones_like(class_labels, dtype=bool)random_index[np.random.choice(range(len(data_arr)), n1, replace=False)] = FalseD1 = data_arr[random_index]D1_left = data_arr[~random_index]
from:http://blog.csdn.net/pipisorry/article/details/39508417
ref: Python模块:生成随机数模块random
- numpy教程:随机数模块numpy.random
- numpy教程:随机数模块numpy.random
- numpy教程:随机数模块numpy.random
- numpy生成随机数random模块
- Numpy之random模块
- numpy的random模块
- numpy的random模块
- numpy的random模块
- numpy random 模块
- Numpy之random模块
- numpy的random模块
- numpy中的random模块
- numpy的random模块
- numpy的random模块
- numpy的random模块
- numpy中的随机数模块
- NumPy random模块的使用
- python numpy中随机数生成包random
- Windows编程革命简史
- 自定义函数
- hdu 2818 Building Block
- Nginx/LVS/HAProxy负载均衡软件的优缺点详解
- 大型系统演进缩影(一)
- numpy教程:随机数模块numpy.random
- VS连接mysql可能出现的问题
- NYOJ 题目715 Adjacent Bit Counts(动态规划)
- 连载《一个程序员的成长历程》-10.在项目中忍受病魔的折磨
- 函数
- Eclipse安装WindowBuilder提示错误:An error occurred while collecting items to be installed,解决方法
- UIAutomator创建一个Note的实例
- Linux之ssh tunnel
- Java面试参考指南——同步