numpy中的随机数模块
https://www.cnblogs.com/td15980891505/p/6198036.html
numpy.random模块中提供啦大量的随机数相关的函数。
1 numpy中产生随机数的方法
1)rand() 产生[0,1]的浮点随机数,括号里面的参数可以指定产生数组的形状
2)randn() 产生标准正太分布随机数,参数含义与random相同
3)randint() 产生指定范围的随机数,最后一个参数是元祖,他确定数组的形状
|
1
2
3
4
5
6
7
8
9
10
11
12
|
import numpy as npfrom numpy import random as nr#只显示小数点后两位np.set_printoptions(precision = 2)r1 = nr.rand(3,4)r2 = nr.randn(5,4)r3 = nr.randint(0,10,size = (4,3))print r1print r2print r3 |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[[ 0.34 0.51 0.65 0.57] [ 0.97 0.16 0.62 0.37] [ 0.23 0.78 0.77 0.46]][[-0.69 -1.24 -0.32 1.07] [ 0.05 -1.97 1.01 -1.59] [ 1.51 -1.21 1.02 -0.19] [ 1.49 -0.42 0.64 0.07] [-0.1 1.11 0.24 -0.18]][[9 6 7] [1 9 7] [4 9 6] [3 9 0]](Pdb) |
2 常用分布
1)normal() 正太分布
2)uniform() 均匀分布
3)poisson() 泊松分布
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# -*- coding: utf-8 -*-"""Spyder EditorThis is a temporary script file."""import numpy as npfrom numpy import random as nr#只显示小数点后两位np.set_printoptions(precision = 2)#第一个参数是均值,第二个参数是标准差r1 = nr.normal(100,10,size = (3,4))print r1#前两个参数分别是区间的初始值和终值r2 = nr.uniform(0,10,size = (3,4))print r2#第一个参数为指定的lanbda系数r3 = nr.poisson(2.0,size = (3,4))print r3 |
|
1
2
3
4
5
6
7
8
9
10
|
[[ 100.67 98.39 99.36 103.37] [ 98.23 95.11 107.57 111.23] [ 97.26 75.21 110.4 112.53]][[ 2.42 6.81 9.96 3.15] [ 9.28 4.4 7.87 5.19] [ 3.47 2.92 4.5 2.58]][[3 1 5 0] [1 0 4 3] [3 1 2 1]](Pdb) |
3 乱序和随机抽取
permutation()随机生成一个乱序数组,当参数是n时,返回[0,n)的乱序,他返回一个新数组。而shuffle()则直接将原数组打乱。choice()是从指定的样本中随机抽取。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# -*- coding: utf-8 -*-"""Spyder EditorThis is a temporary script file."""import numpy as npfrom numpy import random as nr#只显示小数点后两位np.set_printoptions(precision = 2)#返回打乱数组,原数组不变r1 = nr.randint(10,100,size = (3,4))print r1print nr.permutation(r1)print r1print nr.permutation(5)# 使用shuffle打乱数组顺序x = np.arange(10)nr.shuffle(x)print x#xhoice()函数从指定数组中随机抽取样本#size参数用于指定输出数组的大小#replace参数为True时,进行可重复抽取,而False表示进行不可重复的抽取。默认为Truex = np.array(10)c1 = nr.choice(x,size = (2,3))print c1c2 = nr.choice(x,5,replace = False)print c2 |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[[78 22 43 70] [46 87 12 32] [11 56 89 79]][[11 56 89 79] [78 22 43 70] [46 87 12 32]][[78 22 43 70] [46 87 12 32] [11 56 89 79]][4 1 2 0 3][3 4 9 5 8 2 7 0 6 1][[4 7 9] [9 1 7]][5 3 2 8 4](Pdb) |
numpy中的随机数模块的更多相关文章
- 【numpy】新版本中numpy(numpy>1.17.0)中的random模块
numpy是Python中经常要使用的一个库,而其中的random模块经常用来生成一些数组,本文接下来将介绍numpy中random模块的一些使用方法. 首先查看numpy的版本: import nu ...
- 在Pycharm中导入第三方模块库(诸如:matplotlib、numpy等)
在Pycharm中导入第三方模块库 一.打开pycharm: 二.点击菜单上的“file” -> “setting”: 三.步骤二完成后出现界面如下所示.选中你的项目(比如thisyan Pro ...
- 【转载】python 模块 - random生成随机数模块
随机数种子 要每次产生随机数相同就要设置种子,相同种子数的Random对象,相同次数生成的随机数字是完全相同的: random.seed(1) 这样random.randint(0,6, (4,5)) ...
- numpy中的np.random.mtrand.RandomState
1 RandomState 的应用场景概述 在训练神经网络时,苦于没有数据,此时numpy为我们提供了 “生产” 数据集的一种方式. 例如在搭建神经网络(一)中的 4.3 准备数据集 章节中就是采用n ...
- Python中生成随机数
目录 1. random模块 1.1 设置随机种子 1.2 random模块中的方法 1.3 使用:生成整形随机数 1.3 使用:生成序列随机数 1.4 使用:生成随机实值分布 2. numpy.ra ...
- Python numpy 中常用的数据运算
Numpy 精通面向数组编程和思维方式是成为Python科学计算大牛的一大关键步骤.——<利用Python进行数据分析> Numpy(Numerical Python)是Python科学计 ...
- Python中的random模块,来自于Capricorn的实验室
Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...
- Python中的random模块
Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...
- Python 3 中的json模块使用
1. 概述 JSON (JavaScript Object Notation)是一种使用广泛的轻量数据格式. Python标准库中的json模块提供了JSON数据的处理功能. Python中一种非常常 ...
随机推荐
- PM过程能力成熟度2级
当PM意识到自己不再是程序员后,就会在项目管理方面,逐渐达到过程能力成熟度1级.尽管这种亲身经历会带给PM管理的信心,但从项目的层面来说,整体还是混沌的,PM在经历过1级的阶段性胜利后,将面临更多的问 ...
- WordCount
一.Gitee地址:https://gitee.com/zjgss99/WordCount 二.项目分析: 对程序设计语言源文件统计字符数.单词数.行数,统计结果以指定格式输出到默认文件中,以及其他扩 ...
- ORACLE中关于表的一些特殊查询语句
1: 如何判断字段的值里面:那些数据包含小写字母或大小字母 判断字段NAME的值里面有小写字母的记录 方式1: SELECT NAME FROM TEST WHERE regexp_like(NAME ...
- 播放器的书签--推荐使用Potplayer
VLC Player https://www.vlchelp.com/skipping-and-playing-audio-and-video-portions-in-vlc/ PotPlayer ...
- Python基础——0前言
python虽然这几年才兴起,但是已经是一门“老”语言了. python的诞生历史也很有趣.Python的创始人为Guido van Rossum(龟叔).1989年圣诞节期间,在阿姆斯特丹,Guid ...
- mysql的函数与储存过程与pymysql的配合使用
现在mysql上定义一个函数,一个储存过程 函数: delimiter \\ CREATE FUNCTION f2 ( num2 INT, num1 INT ) RETURNS INT BEGIN D ...
- RESTful API规范
1. 域名 应该尽量将API部署在专用的域名下. https://api.example.com 如果确定API简单,不会有进一步的括在,可以考虑放在主域名之下. https://example.or ...
- 【English】20190328
Emotions情绪 [ɪ'moʊʃənz] Run Your Life for Teens影响你的青少年生活[ti:nz] Don't Let Your Emotions Run Your Lif ...
- supervisor管理php-fpm
/etc/php-fpm.conf,设置daemonize = no,默认是yes
- 【转】VUE 爬坑之旅-- 如何对公共JS,CSS进行统一管理,全局调用
原文:https://blog.csdn.net/zgh0711/article/details/78664262 vue 中,将页面分为了各个组件,我们写好组件,就可以将这个组件运用到其他各个页面中 ...