python random模块中seed函数的详解_numpy.random模块常用函数解析

本文详细解析了numpy.random模块中的rand、randn、randint等常用函数,用于生成不同分布的随机数。同时,介绍了seed函数的作用,用于设置随机数生成的初始状态,确保重复运行时能获取相同随机序列。
摘要由CSDN通过智能技术生成

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

1. numpy.random.rand(d0, d1, ..., dn)

Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1)

按照给定形状产生一个多维数组,每个元素在0到1之间

注意: 这里定义数组形状时,不能采用tuple

import numpy as np

np.random.rand(2, 3)

array([[ 0.44590044, 0.36234046, 0.51609462],

[ 0.45733218, 0.80836224, 0.31628453]])

2. numpy.random.randn(d0, d1, ..., dn)

generates an array of shape (d0, d1, ..., dn), filled with random floats sampled from a univariate “normal” distribution of mean 0 and variance 1

按照给定形状产生一个多维数组,数组中的元素服从标准正态分布

若要产生服从N(mu, sigma^2)分布的样本, 使用sigma * np.random.randn(...) + mu

例如产生 2 * 4 samples from N(3, 6.25):

2.5 * np.random.randn(2, 4) + 3

array([[ 2.90478558, 6.05670578, 6.21539068, 3.3955507 ],

[ 0.11594363, 3.17433693, 5.35625762, 1.4824643 ]])

3. numpy.random.randint(low, high=None, size=None, dtype='l')

Return random integers from low (inclusive) to high (exclusive).

按照给定的形状和范围产生随机整数

np.random.randint(0, 10, size=(2, 4))

array([[2, 7, 2, 1],

[3, 2, 4, 1]])

4. numpy.random.random_integers(low, high=None, size=None)

Random integers of type np.int between low and high, inclusive.

np.random.random_integers(1, 10, size=(2, 5))

array([[ 3, 3, 8, 4, 5],

[ 2, 7, 8, 10, 2]])

5. numpy.random.random_sample(size=None)

6. numpy.random.random(size=None)

7. numpy.random.ranf(size=None)

8. numpy.random.sample(size=None)

Return random floats in the half-open interval [0.0, 1.0).

以上四种方式都是生成[0,1)之间的浮点数

To sample Unif[a, b), b > a multiply the output of random_sample by (b-a) and add a:

(b - a) * random_sample() + a

import numpy as np

print('random_sample:\n', np.random.random_sample((2, 3)))

print('random:\n', np.random.random((2, 3)))

print('ranf:\n', np.random.ranf((2, 3)))

print('sample:\n', np.random.sample((2, 3)))

random_sample:

[[ 0.87996593 0.2706701 0.42158973]

[ 0.91952234 0.99470239 0.07363656]]

random:

[[ 0.44572326 0.23595379 0.1061901 ]

[ 0.48362249 0.4270327 0.12281262]]

ranf:

[[ 0.07180002 0.25542854 0.55630057]

[ 0.38181471 0.91512916 0.04020929]]

sample:

[[ 0.80390231 0.0024602 0.95974309]

[ 0.32902852 0.62796713 0.42254831]]

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

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

如a是一个int数, 则产生的数组的元素都在np.arange(a)中

如a是一个1-D array-like, 则产生的数组的元素都在a中

print('1:\n', np.random.choice(5))

print('2:\n', np.random.choice(5, 2, p=[0.1, 0.4, 0.3, 0.1, 0.1]))

print('3:\n', np.random.choice(5, (2, 3)))

print('4:\n', np.random.choice([1, 3, 4, 6], (2, 5), p=[0.1, 0.3, 0.1, 0.5]))

1:

4

2:

[1 4]

3:

[[2 1 4]

[0 2 3]]

4:

[[3 6 1 6 1]

[3 3 3 3 1]]

10. numpy.random.seed(None)

设置相同的seed,每次生成的随机数相同。如果不设置seed,则每次会生成不同的随机数

np.random.seed(2)

np.random.rand(2, 3)

array([[ 0.4359949 , 0.02592623, 0.54966248],

[ 0.43532239, 0.4203678 , 0.33033482]])

np.random.seed(2)

np.random.rand(2, 3)

array([[ 0.4359949 , 0.02592623, 0.54966248],

[ 0.43532239, 0.4203678 , 0.33033482]])

np.random.rand(2, 3)

array([[ 0.20464863, 0.61927097, 0.29965467],

[ 0.26682728, 0.62113383, 0.52914209]])

numpy.random之常用函数

在实际开发中,我们经常会使用随机函数,比如交叉验证,构造测试数据等.下面,是我常用的几个生成随机样本的函数: 1,rand(n1,n2,…,nn) 每一维度都是[0.0,1.0)半闭半开区间上的随机分 ...

numpy模块常用函数解析

https://blog.csdn.net/lm_is_dc/article/details/81098805 numpy模块以下命令都是在浏览器中输入. cmd命令窗口输入:jupyter note ...

操作 numpy 数组的常用函数

操作 numpy 数组的常用函数 where 使用 where 函数能将索引掩码转换成索引位置: indices = where(mask) indices => (array([11, 12, ...

AR模块常用函数

--AR模块常用函数 FUNCTION get_fnd_user_name ( p_user_id IN NUMBER ) return VARCHAR2 IS CURSOR c_user_name ...

$python正则表达式系列(2)——re模块常用函数

本文主要介绍正则re模块的常用函数. 1. 编译正则 import re p = re.compile(r'ab*') print '[Output]' print type(p) print p p ...

Python 常用模块系列学习(1)--random模块常用function总结--简单应用--验证码生成

random模块--random是一个生成器 首先: import random    #导入模块 print (help(random))    #打印random模块帮助信息 常用function ...

numpy.random模块用法总结

from numpy import random numpy.random.uniform(low=0.0, high=1.0, size=None) 生出size个符合均分布的浮点数,取值范围为[l ...

Path模块部分常用函数解析——NodeJS

官网地址:https://nodejs.org/api/path.html path.resolve([...paths])# Added in: v0.3.4 参数[...paths]: 

tensorflow常用函数解析

一.tf.transpose函数的用法 tf.transpose(input, [dimension_1, dimenaion_2,..,dimension_n]):这个函数主要适用于交换输入张量的不 ...

随机推荐

checkbox的全选、反选、删除(适配器)

package com.example.adapter; import java.util.List; import com.example.ay.R;import com.example.vo.Fl ...

Android wakelock机制

Wake Lock是一种锁的机制, 只要有人拿着这个锁,系统就无法进入休眠,可以被用户态程序和内核获得. 这个锁可以是有超时的或者是没有超时的,超时的锁会在时间过去以后自动解锁. 如果没有锁了或者 ...

Installing MySQL Connector/Python using pip v1.5

The latest pip versions will fail on you when the packages it needs to install are not hosted on PyP ...

java设计模式和设计原则

一.创建型模式 1.抽象工厂模式(Abstract factory pattern): 提供一个接口, 用于创建相关或依赖对象的家族, 而不需要指定具体类.2.生成器模式(Builder patter ...

ajax例子

Linux shell编程 4 ---- shell中的循环

1 for循环 1 for语句的结构 for variable in values; do statement done 2 for循环通常是用来处理一组值,这组值可以是任意的字符串的集合 3 for ...

Qt之美(一):d指针/p指针详解

Translated  by  mznewfacer   2011.11.16 首先,看了Xizhi Zhu 的这篇Qt之美(一):D指针/私有实现,对于很多批评不美的同路人,暂且不去评论,只是想支持 ...

简单的 "双缓冲" 绘图的例子(研究一下)

所谓双缓冲就是先画到内存画布(如: TBitmap), 然后再转帖到目的地. 譬如下面小程序: procedure TForm1.FormCreate(Sender: TObject); begin ...

轻量级验证码生成插件webutil-licenseImage

轻量级验证码生成插件webutil-licenseImage源码与实例应用   webutil-licenseImage 插件内置4种验证码样式,支持用户扩展.自定义样式实现简单验证码. 源码脱管地址 ...

Repeated Substring Pattern Leetcode

Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值