Python Statistics (1)

1 Basic configuration

1.1 Basic module

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import stats
import math

1.2 Drawing settings

plt.rcParams["font.sans-serif"]='SimHei' #Solve the problem of Chinese garbled
characters plt.rcParams["axes.unicode_minus"]=False #Solve the problem that symbols cannot be displayed normally

2 Basic statistics

Insert picture description here

Note: When calculating the variance, n is used to calculate the variance by default. In order to get the sample variance, you need to set ddof=1,
such as:
np.mean([1,2,3]), np.std([1,2,3]), np.std([1,2,3],ddof=1)
Insert picture description here

3 Basic distribution

3.1 Discrete distribution

Insert picture description here

3.2 Example of discrete distribution

3.2.1 Bernoulli distribution

ff=stats.bernoulli(0.5)
ff.rvs(10)

3.2.2 Binomial distribution

Surely, p = (20,0.5)
ff = stats.binom (whether p)
ff.rvs (10)

3.2.3 Poisson distribution

lamda=2
ff=stats.poisson(lamda )
ff.pmf(0),ff.rvs(10)

3.3 Continuous distribution

Insert picture description here

3.4 Example of continuous distribution

3.4.1 Normal distribution

nv=stats.norm(3,2)
x=np.arange(-10,10,0.1)
y=nv.pdf(x)
y2=nv.cdf(x)
plt.plot(x,y,label=“pdf”)
plt.plot(x,y2,label=“cdf”)
plt.grid()
plt.legend()
nv.sf(0),nv.ppf(0.6),nv.isf(0.3),nv.rvs(10)
Insert picture description here

3.4.2 Uniform distribution

start,width=2,5
nv=stats.uniform(start,width)
x=np.arange(0,10,0.1)
y=nv.pdf(x)
y2=nv.cdf(x)
plt.plot(x,y,label=“pdf”)
plt.plot(x,y2,label=“cdf”)
plt.grid()
plt.legend()
nv.sf(0),nv.ppf(0.6),nv.isf(0.3),nv.rvs(10)
Insert picture description here

3.4.3 Exponential distribution

u=0
sita=2
nv=stats.expon(u,sita)
x=np.arange(0,10,0.1)
y=nv.pdf(x)
y2=nv.cdf(x)
plt.plot(x,y,label=“pdf”)
plt.plot(x,y2,label=“cdf”)
plt.grid()
plt.legend()
x0=2
math.exp(-x0/sita)/sita,nv.pdf(x0),nv.ppf(0.6),nv.isf(0.3),nv.rvs(10)
Insert picture description here

3.4.4 t distribution

n=10
fd=n-1
nv=stats.t(fd)
x=np.arange(-10,10,0.1)
y=nv.pdf(x)
y2=nv.cdf(x)
plt.plot(x,y,label=“pdf”)
plt.plot(x,y2,label=“cdf”)
plt.grid()
plt.legend()
nv.sf(0),nv.ppf(0.6),nv.isf(0.3),nv.rvs(10)
Insert picture description here

3.4.5 Chi-square distribution

n=10
fd=n-1
nv=stats.chi2(fd)
x=np.arange(-10,50,0.1)
y=nv.pdf(x)
y2=nv.cdf(x)
plt.plot(x,y,label=“pdf”)
#plt.plot(x,y2,label=“cdf”)
plt.grid()
plt.legend()
nv.sf(0),nv.ppf(0.6),nv.isf(0.3),nv.rvs(10)
Insert picture description here

3.4.6 F distribution

n1,n2=10,10
fd1,fd2=n1-1,n2-1
nv=stats.f(fd1,fd2)
x=np.arange(-2,10,0.1)
y=nv.pdf(x)
y2=nv.cdf(x)
plt.plot(x,y,label=“pdf”)
plt.plot(x,y2,label=“cdf”)
plt.grid()
plt.legend()
nv.sf(0),nv.ppf(0.6),nv.isf(0.3),nv.rvs(10)
Insert picture description here

3.4.7 Lognormal distribution

nv=stats.lognorm(scale=math.exp(1),s=0.3)
x=np.arange(-2,10,0.01)
y=nv.pdf(x)
y2=nv.cdf(x)
plt.plot(x,y,label=“pdf”)
plt.plot(x,y2,label=“cdf”)
plt.grid()
plt.legend()
nv.sf(0),nv.ppf(0.6),nv.isf(0.3),nv.rvs(10)
Insert picture description here

3.4.8 Weber distribution

nv=stats.exponweib(a=1,c=2)
x=np.arange(-2,10,0.1)
y=nv.pdf(x)
y2=nv.cdf(x)
plt.plot(x,y,label=“pdf”)
plt.plot(x,y2,label=“cdf”)
plt.grid()
plt.legend()
nv.sf(0),nv.ppf(0.6),nv.isf(0.3),nv.rvs(10)

Insert picture description here

Guess you like

Origin blog.csdn.net/skytering/article/details/109231494