Ensure the randomness of the array
np.random.seed(1)
norm_dist = stats.norm(loc, scale)
among , loc = Array mean , scale = Array variance
sample = norm_dist.rvs(size=10)
size Is the number of samples
When the number of samples is relatively small <30, Use t Distribution
stats.t.interval(0.95,df,mu,se)
df= len(sample)-1 # freedom
mu= np.mean(sample) # mean value
std = np.std(sample,ddof=1) # Sample standard variance
se = std/ np.sqrt(len(sample) # Standard error
0.95 = Confidence level
The library used
np = import numpy as np
stats = from scipy import stats
np. Distribution name .rvs: Generate random numbers that obey the specified distribution
# Introduce libraries for calculation
import numpy as np
import pandas as pd
import scipy as sp
from scipy import stats
# Set random seeds
np.random.seed(1)
# Set normal distribution
norm_dist=stats.norm(loc=5,scale=0.09)
# Take samples
sample = norm_dist.rvs(size=20)
# freedom
df=len(sample)-1
# mean value
mu=np.mean(sample)
# Standard deviation , Calculate the sample with ddof=1, Overall use ddof=0
std = np.std(sample,ddof=1)
# Standard error
se = std/np.sqrt(len(sample))
# confidence interval
interval = stats.t.interval(0.95,df,mu,se)
Reference resources 《 use python Start learning statistics 》