python3 scipy 入门

正态分布:

'''
Scipy中的stats模块包含了多种常用的数据统计函数,包括连续和离散两种随机变量。
对于连续随机变量,可有如下操作:
rvs:随机变量进行取值,通过size给定大小
pdf:概率密度函数
cdf:累计分布函数
sf:生存函数,1-CDF
ppf:百分点函数,累计分布函数的反函数
isf:生存函数的反函数
stats:返回期望和方差(mean()、var())
'''
from scipy import stats
from scipy.stats import norm
import numpy as np
import pylab as pl

X = norm()  # 默认参数,loc=0,scale=1
Y = norm(loc=1.0, scale=2.0)  # 平移1.0,缩小2.0
Z = norm(loc=-1.0, scale=0.5)  # 平移1.0,缩小2.0

t = np.arange(-10, 10, 0.01)
pl.plot(t, X.pdf(t), label="$X$", color="red")
pl.plot(t, Y.pdf(t), "b--", label="$Y$")
pl.plot(t, Z.pdf(t), label="$Z$", color="yellow")
pl.show()

《python3 scipy 入门》

 

获取标准正态分布函数的值

X = norm()  # 默认参数,loc=0,scale=1
t = np.arange(-1, 1, 0.01)

for i in range(len(t)):
    print(t[i],norm.cdf(t[i]))

《python3 scipy 入门》

 

期望,方差,标准差

print(X.mean(), X.std(), X.var())

 

分位数

# 为了找到一个分部的中心,我们可以使用分位数函数ppf,其是cdf的逆。
print(norm.ppf(0.5))

 

多个随机变量的取值,并没有产生5个随机变量,只是取了5个值

print(norm.rvs(size=5))

[-1.2066922   1.3791005   1.1342611   2.5763324   0.07096698]

 

正态分布的均值和标准差

# Shifting and Scaling
# 位移与缩放(线性变换)
# All continuous distributions take loc and scale as keyword parameters to adjust the location and scale of the distribution, e.g. for the standard normal distribution the location is the mean and the scale is the standard deviation.
# 所有连续分布可以操纵loc以及scale参数作为修正location和scale的方式。作为例子,标准正态分布的location是均值而scale是标准差。

X = norm(loc=2, scale=4)
print(X.mean(), X.var(), X.std())
2.0 16.0 4.0

 

连续随机变量对象的方法

 

rvs(*args, **kwds)Random variates of given type.产生服从这种分布的一个样本,对随机变量进行随机取值,可以通过size参数指定输出的数组大小。
pdf(x, *args, **kwds)Probability density function at x of the given RV.随机变量的概率密度函数。产生对应x的这种分布的y值。
logpdf(x, *args, **kwds)Log of the probability density function at x of the given RV.
cdf(x, *args, **kwds)Cumulative distribution function of the given RV.随机变量的累积分布函数,它是概率密度函数的积分(也就是x时p(X<x)的概率)。产生对应x的这种分布的累积分布函数的值。
logcdf(x, *args, **kwds)Log of the cumulative distribution function at x of the given RV.
sf(x, *args, **kwds)Survival function (1 – cdf) at x of the given RV.随机变量的生存函数,它的值是1-cdf(t)。
logsf(x, *args, **kwds)Log of the survival function of the given RV.
ppf(q, *args, **kwds)Percent point function (inverse of cdf) at q of the given RV.累积分布函数的反函数。q=0.01时,ppf就是p(X<x)=0.01时的x值。
isf(q, *args, **kwds)Inverse survival function (inverse of sf) at q of the given RV.
moment(n, *args, **kwds)n-th order non-central moment of distribution.
stats(*args, **kwds)Some statistics of the given RV.计算随机变量的期望值和方差
entropy(*args, **kwds)Differential entropy of the RV.
expect([func, args, loc, scale, lb, ub, …])Calculate expected value of a function with respect to the distribution.
median(*args, **kwds)Median of the distribution.
mean(*args, **kwds)Mean of the distribution.
std(*args, **kwds)Standard deviation of the distribution.
var(*args, **kwds)Variance of the distribution.
interval(alpha, *args, **kwds)Confidence interval with equal areas around the median.
__call__(*args, **kwds)Freeze the distribution for the given arguments.
fit(data, *args, **kwds)Return MLEs for shape, location, and scale parameters from data.对一组随机取样进行拟合,找出最适合取样数据的概率密度函数的系数。如stats.norm.fit(x)就是将x看成是某个norm分布的抽样,求出其最好的拟合参数(mean, std)。
fit_loc_scale(data, *args)Estimate loc and scale parameters from data using 1st and 2nd moments.
nnlf(theta, x)Return negative loglikelihood function.

 

离散随机变量,抛硬币模型测试

# 离散随机变量,抛硬币模型测试
for n in range(1000, 10000):
    # x 表示离散随机变量的取值,p表示对应的取值概率
    x = [0, 1]
    p = [0.5, 0.5]
    # 创建随机变量
    coin = stats.rv_discrete(values=(x, p))
    # 获得实验结果
    res = coin.rvs(size=n)
    # s是正面出现的次数
    s = sum(res)
    # p 是正面出现的概率
    p = s / n
    print(p)

 《python3 scipy 入门》
 

正态分布拟合

X = norm.rvs(loc=1.0, scale=2.0, size=10000)
Y = norm.rvs(size=10000)  # 标准正态分布
# 可以使用fit()
# 方法对随机取样序列x进行拟合,返回的是与随机取样值最吻合的随机变量的参数,均值和标准差
print(stats.norm.fit(X))  # 得到随机序列的期望值和标准差
# (1.0017857642988532, 1.9730569004868777)
print(stats.norm.fit(Y))  # 得到随机序列的期望值和标准差
# (0.0060040024384516713, 1.0038393637253209)

 

    原文作者:python入门
    原文地址: https://my.oschina.net/ahaoboy/blog/1581518
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞