task07 随机抽样

离散型随机变量

二项分布
二项分布可以用于只有一次实验只有两种结果,各结果对应的概率相等的多次实验的概率问题。比如处理猜10次拳赢6次的概率等类似的问题。

numpy.random.binomial(n,p,size)产生size个符合(n,p)的二项分布随机数
即,相当于进行size次实验,每次实验都投掷n枚硬币/每次实验都将一枚硬币投掷n次,记录size次实验中,正面朝上分别为0-n次的实验次数;其中每次试验的成功概率为p

import numpy as np
from scipy import stats
import matplotlib.pyplot as plt

np.random.seed(20200605)
n = 2# 做某件事情的次数,这里是投两次硬币
p = 0.5#做某件事情成功的概率,在这里即投硬币为正面的概率
size = 50000
x = np.random.binomial(n, p, size)
'''或者使用binom.rvs
#使用binom.rvs(n, p, size=1)函数模拟一个二项随机变量,可视化地表现概率
y = stats.binom.rvs(n, p, size=size)#返回一个numpy.ndarray
'''
print(np.sum(x == 0) / size)  # 0.25154
print(np.sum(x == 1) / size)  # 0.49874
print(np.sum(x == 2) / size)  # 0.24972

plt.hist(x, density=True)
plt.xlabel('随机变量:硬币为正面次数')
plt.ylabel('50000个样本中出现的次数')
plt.show()
#它返回一个列表,列表中每个元素表示随机变量中对应值的概率
s = stats.binom.pmf(range(n + 1), n, p)
print(np.around(s, 3))
# [0.25 0.5  0.25]

task07 随机抽样

泊松分布

泊松分布主要用于估计某个时间段某事件发生的概率
numpy.random.poisson(lam=1.0, size=None) Draw samples from a Poisson distribution.
表示对一个泊松分布进行采样,size表示采样的次数,lam表示一个单位内发生事件的平均值,函数的返回值表示一个单位内事件发生的次数。
【例】假定某航空公司预定票处平均每小时接到42次订票电话,那么10分钟内恰好接到6次电话的概率是多少?

import numpy as np
from scipy import stats
import matplotlib.pyplot as plt

np.random.seed(20200605)
lam = 42 / 6# 平均值:平均每十分钟接到42/6次订票电话
size = 50000
x = np.random.poisson(lam, size)
'''或者
#模拟服从泊松分布的50000个随机变量
x = stats.poisson.rvs(lam,size=size)
'''
print(np.sum(x == 6) / size)  # 0.14988

plt.hist(x)
plt.xlabel('随机变量:每十分钟接到订票电话的次数')
plt.ylabel('50000个样本中出现的次数')
plt.show()
#用poisson.pmf(k, mu)求对应分布的概率:概率质量函数 (PMF)
x = stats.poisson.pmf(6, lam)
print(x)  # 0.14900277967433773

超几何分布
在超几何分布中,各次实验不是独立的,各次实验成功的概率也不等。
numpy.random.hypergeometric(ngood, nbad, nsample, size=None) Draw samples from a Hypergeometric distribution.
表示对一个超几何分布进行采样,size表示采样的次数,ngood表示总体中具有成功标志的元素个数,nbad表示总体中不具有成功标志的元素个数,ngood+nbad表示总体样本容量,nsample表示抽取元素的次数(小于或等于总体样本容量),函数的返回值表示抽取nsample个元素中具有成功标识的元素个数。

连续型随机变量

均匀分布

numpy.random.uniform(low=0.0, high=1.0, size=None) Draw samples from a uniform distribution.
Samples are uniformly distributed over the half-open interval [low, high) (includes low, but excludes high). In other words, any value within the given interval is equally likely to be drawn by uniform .

作为uniform()的特列,可以得到[0,1)之间的均匀分布的随机数。
numpy.random.rand(d0, d1, …, dn) Random values in a given shape.
Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1).

作为uniform的另一特例,可以得到[low,high)之间均匀分布的随机整数。
numpy.random.randint(low, high=None, size=None, dtype=‘l’) Return random integers from low (inclusive) to high (exclusive).
Return random integers from the “discrete uniform” distribution of the specified dtype in the “half-open” interval [low, high). If high is None (the default), then results are from [0, low).
若high 不为None 时,取[low,high)之间随机整数,否则取值[0,low)之间随机整数。

正态分布

numpy.random.randn(d0, d1, …, dn) Return a sample (or samples) from the “standard normal” distribution.
【例】根据指定大小产生满足标准正态分布的数组(均值为0,标准差为1)。

import numpy as np
import matplotlib.pyplot as plt
from scipy import stats

np.random.seed(20200614)
size = 50000
x = np.random.randn(size)
y1 = (np.sum(x < 1) - np.sum(x < -1)) / size
y2 = (np.sum(x < 2) - np.sum(x < -2)) / size
y3 = (np.sum(x < 3) - np.sum(x < -3)) / size
print(y1)  # 0.68596
print(y2)  # 0.95456
print(y3)  # 0.99744

plt.hist(x, bins=20)
plt.show()

y1 = stats.norm.cdf(1) - stats.norm.cdf(-1)
y2 = stats.norm.cdf(2) - stats.norm.cdf(-2)
y3 = stats.norm.cdf(3) - stats.norm.cdf(-3)
print(y1)  # 0.6826894921370859
print(y2)  # 0.9544997361036416
print(y3)  # 0.9973002039367398

task07 随机抽样

还可以指定分布以及所需参数来进行随机,例如高斯分布中的mu和sigma。

numpy.random.normal(loc=0.0, scale=1.0, size=None) Draw random samples from a normal (Gaussian) distribution.
normal()为创建均值为 loc(mu),标准差为 scale(sigma),大小为 size 的数组。

指数分布
指数分布描述时间发生的时间长度间隔。

其它随机函数

随机从序列中获取元素
numpy.random.choice(a, size=None, replace=True, p=None) Generates a random sample from a given 1-D array.
从序列中获取元素,若a为整数,元素取值从np.range(a)中随机获取;若a为数组,取值从a数组元素中随机获取。该函数还可以控制生成数组中的元素是否重复replace,以及选取元素的概率为P。

对数据集进行洗牌操作
数据一般都是按照采集顺序排列的,但是在机器学习中很多算法都要求数据之间相互独立,所以需要先对数据集进行洗牌操作。

numpy.random.shuffle(x) Modify a sequence in-place by shuffling its contents.
This function only shuffles the array along the first axis of a multi-dimensional array. The order of sub-arrays is changed but their contents remains the same.

对x进行重排序,如果x为多维数组,只沿第 0 轴洗牌,改变原来的数组,输出为None。

numpy.random.permutation(x) Randomly permute a sequence, or return a permuted range.
If x is a multi-dimensional array, it is only shuffled along its first index.

permutation()函数的作用与shuffle()函数相同,可以打乱第0轴的数据,但是它不会改变原来的数组。

上一篇:【Oracle Database】统计信息自动收集任务


下一篇:mysql 统计信息记录