Python办公自动化 --- python数据分析


随机数

是随机产生的数字

可以是均匀分布的随机数、

标准正态分布的随机数、

正态分布的随机数、

随机抽样的随机数、

随机整数、随机浮点数、

按权重产生随机数,等等

那么,如何使用这些随机数呢?


python内置的random库


>>> import random as r


random.random()函数

返回 [0.0, 1.0) 之间的1个浮点数。对于左闭[、右开)的区间,随机数可能会是0,但不可能是1。

>>> r.random()

0.580336831229011

输出一个随机浮点数。

>>> round(r.random(),4)

0.4319

将产生的浮点数保留指定个数的小数点位数。


random.randint(a , b) 函数

返回[a, b]之间的1个随机整数。左右都是闭区间,所以随机数可能是a,也可能是b。等同于random.randrange(a,b+1)。

>>> r.randint(2,10)

10

>>> r.randint(2,10)

5

>>> r.randint(2,10)

2


random.randrange()函数

返回[a, b)之间的1个随机整数。左闭右开区间,所以随机数可能是a,但不可能是b。

>>> r.randrange(2,10)

7

>>> r.randrange(2,10)

2

>>> r.randrange(2,11,3)

8 这里参数3是步长,先按步长3产生2(含)到11(不含)的整数序列,再从产生的序列中随机抽取1个元素。如果没有第3个参数3,则默认是1。


random.uniform(a, b)函数

返回[a, b]之间的服从均匀分布的1个随机浮点数。

注意,两边都是闭区间。

>>> r.uniform(2,10)

8.928253460320967


random.choice()函数

从序列中随机取出一个元素,序列可以是列表、元组、字符串、迭代器,但不能是字典、集合。

>>> r.choice(['book','dog','love'])

'book'

>>> r.choice(('book','dog','love'))

'love'

>>> r.choice('compute')

'u'

>>> r.choice(range(10))

3


random.choices()函数

>>> r.choices(['dog','cat','pig'], weights=[6,2,2],k=5)

['dog', 'dog', 'pig', 'cat', 'dog']

从序列中按权重提取5个元素。

注意:参数权重weights的元素个数与序列元素个数必须相同。

权重的元素就是序列元素的权重值,如6就是dog的权重。

权重相对越大,被提取的可能性就越大。


random.shuffle() 函数

将序列中的元素顺序进行随机打乱。

>>> res = [1,2,3,4,5,6]

>>> r.shuffle(res)

>>> res

[1, 6, 3, 5, 2, 4]

注意,是在原序列中进行打乱,即直接在原序列上打乱元素顺序,而不是返回打乱后的新序列。


random.sample()函数

从序列中随机取出n个元素。

>>> res = [1,2,3,4,5,6]

>>> r.sample(res,2)

[2, 6]


高斯分布(正态分布)

>>> r.gauss(1,2)

5.182450065445453

返回均值为1标准差为2的正态分布的一个随机数。等同>>> r.normalvariate(1,2),但是比它稍快。


第3库numpy的子库random

>>> import numpy as np


np.random.rand()函数

返回[0,1)之间的随机浮点数。

>>> np.random.rand(3).round(2)

array([0.55, 0.93, 0.21])

一维列表

>>> np.random.rand(2,3).round(2)

array([[0.42, 0.09, 0.01],

[0.69, 1. , 0.43]])

二维列表

>>> np.random.rand(2,3,4).round(2)

array([[[0.26, 0.05, 0.05, 0.83],

[0. , 0.43, 0.37, 0.62],

[0.24, 0.14, 0.88, 0.84]],


[[0.18, 0.01, 0.58, 0.06],

[0.21, 0.2 , 0.66, 0.87],

[0.07, 0.42, 0.75, 0.96]]])

三维列表


np.random.randn()函数

返回标准正态分布的随机数。

>>> np.random.randn(2).round(3)

array([-1.886, -0.383])

>>> np.random.randn(2,3).round(3)

array([[ 0.505, -2.098, 0.011],

[-1.491, 0.302, 0.046]])

>>> np.random.randn(2,3,4).round(3)

array([[[ 0.646, -0.825, 0.337, 1.325],

[-1.005, -0.817, 0.265, 0.101],

[-0.104, -0.138, 0.131, -0.553]],


[[-1.323, 0.819, -0.582, -1.434],

[-0.621, -1.315, 0.97 , 0.307],

[ 1.047, 0.76 , 0.347, -0.133]]])


np.random.randint(a,b,size)函数

返回[a,b)之间的随机整数,返回的个数由size决定。

>>> np.random.randint(2,10,3)

array([9, 4, 9])

>>> np.random.randint(2,10,(2,3))

array([[6, 8, 8],

[8, 7, 4]])

返回2行3列(2,3)的随机整数。


np.random.random_samplet()函数

同np.random.random()函数

同np.random.randf()函数

返回[0.0,1.0)之间的随机浮点数。

>>> np.random.random_sample(2).round(3)

array([0.273, 0.572])

>>> np.random.random_sample((2,4)).round(3)

array([[0.604, 0.652, 0.861, 0.162],

[0.692, 0.726, 0.315, 0.422]])



np.random.choice()函数

>>> np.random.choice(5)

1 从[0,5)随机抽取一个整数

>>> np.random.choice(5,2)

array([1, 3]) 从[0,5)随机抽取2个整数

>>> np.random.choice(5,2)

array([4, 4]) 从[0,5)随机抽取2个整数,可以是重复的整数

>>> np.random.choice([10,20,30,40],6)

array([20, 20, 10, 20, 30, 20])

>>> np.random.choice([10,20,30,40],3,replace=False)

array([30, 20, 10])

设置参数replace为False,不能抽取重复的整数。

>>> np.random.choice([10,20,30,40],3,p=[0.7,0.1,0.2,0])

array([30, 20, 10])

参数p必须与序列的元素个数相同,p是序列每个元素被抽取的概率。


np.random.shuffle()函数

>>> lb = [1,2,3,4,5,6]

>>> np.random.shuffle(lb)

>>> lb

[5, 6, 2, 1, 3, 4]

打乱lb序列的元素顺序。


np.random.normal(a,b,size)函数

返回均值为a、标准差为b的正态分布的随机数size个。

>>> np.random.normal(0,1,3).round(3)

array([-0.571, -1.028, 0.184])

np.random.uniform(a,b,size)函数

返回1到3之间的均匀分布的随机数5个。

>>> np.random.uniform(1,3,5).round(3)

array([1.64 , 1.521, 2.902, 2.898, 1.638])

除了以上2个分布函数外,np.random子库还有很多其它的分布函数,具体可以查询numpy的文档(https://numpy.org/doc/)。

总结


以上就是python常用的2个随机数库

基本上能够满足我们对随机数的绝大部分需求


更多python办公自动化案例和经验

请继续关注我们的公众号和头条号

后期会持续进行更新

“白领服务工作室”的系列视频课如下:

Python办公自动化---Python入门课程

Python办公自动化---Python进阶课程

Python办公自动化---正则表达式

Python办公自动化---数据分析

Python办公自动化---网络爬虫

Python办公自动化---Excel表格专栏

Python办公自动化---Pdf专栏

Python办公自动化---Word专栏

Python办公自动化---图像专栏

Excel/WPS表格 --- 数据处理

如需学习以上视频课程,敬请留言!

作者 | 小白

来源 | 原创

编辑 | 白领服务工作室