# 计算K百分位数
import numpy as np
x = np.array([-35, 10, 20, 30, 40, 50, 60, 100])
k = 25
xk = np.percentile(x, k, interpolation='linear')
Nx = x.shape[0]
indices = 1 + (Nx - 1) * k/100.0
print(indices, xk)
# 计算与IQR有关的概率 (IQR:四分位数极差)
import scipy.stats
Q1 = scipy.stats.norm(0, 1).ppf(0.25)
Q3 = scipy.stats.norm(0, 1).ppf(0.75)
Upperfence = scipy.stats.norm(0, 1).cdf(Q3 + 1.5 * (Q3 - Q1))
Lowerfence = scipy.stats.norm(0, 1).cdf(Q1 - 1.5 * (Q3 - Q1))
probUL = round(Upperfence - Lowerfence, 4)
probOutliers = 1 - probUL
print('Q1 - u = {:.4f}, Q3 - u = {:.4f}'.format(Q1, Q3))
print('IQP = Q3 - Q1 = {:.4f}'.format(Q3 - Q1))
print('Q3+1.5 x IQR-u = {:.4f}'.format((Q3 + 1.5)*(Q3 - Q1)))
print('Q1 - 1.5 * TQR-u = {:.4f}'.format(Q1 - 1.5 * (Q3 - Q1)))
print('P(Q1 - 1.5 * IQR < x < Q3 + 1.5 * IQR = {:.4F}'.format(probUL))
print('在上下限之外的概率={:.4f}'.format(100 * probOutliers))
# 离散系数计算
import numpy as np
Adult_gruop = np.array([177, 169, 171, 171, 173, 175, 170, 173, 169, 172, 173, 175, 179, 176, 166, 170, 167, 171, 171, 169])
Childer_group = np.array([72, 76, 72, 70, 69, 76, 77, 72, 68, 74, 72, 70, 71, 73, 75, 71, 72, 72, 71, 67])
print('成人组标准差:{:.2f}, 幼儿组标准差:{:.2f}'.format(np.std(Adult_gruop, ddof=1), np.std(Childer_group, ddof=1)))
print('成人组均值:{:.2f}, 幼儿组均值:(:.2f)'.format(np.mean(Adult_gruop), np.mean(Childer_group)))
print('成人组离散系数:{:.2f}, 幼儿组离散系数:{:.2f}'.format(np.std(Adult_gruop, ddof=1)/ np.mean(Adult_gruop), np.std(Childer_group, ddof=1)/np.mean(Childer_group)))
# 使用Python画箱形图
import matplotlib.pyplot as plt
data = [-35, 10, 20, 30, 40, 50, 60, 106]
flierprops = {'marker':'o', 'markerfacecolor':'red', 'color':'black'}
plt.grid(True, linestyle='-.', color = 'black', linewidth = '0.4')
plt.boxplot(data, notch=False, flierprops=flierprops) #notch:是否是凹口的形式展现箱线图,默认非凹口;filerprops:设置异常值的属性,如异常点的形状、大小、填充色等
plt.show()
# 正态分布
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as sts
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
samples = np.around(np.random.normal(loc=0.0, scale=1.0, size=580000), 2)
plt.figure(num=1, dpi=300)
plt.ylabel('频数', size=14)
plt.hist(samples, bins=1300, range=(-5, 5))
n_mean = np.round(np.mean(samples), 2)
n_median = np.round(np.median(samples), 2)
n_mode = sts.mode(samples)
n_Skewness, n_kurtosis = sts.describe(samples)[4:]
plt.text(-5, 2100, '均值:{:.2f}, 中位数:{:.2f}, 众数:{:.2f}'.format(n_mean, n_median, n_mode.mode[0]), size=8)
plt.text(-5, 2000, '偏度:{:.4f}, 峰度:{:.4f}'.format(n_Skewness, n_kurtosis), size=8)
plt.show()
# 用散点图直观判断相关性
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
np.random.seed(1)
x = np.random.randint(0, 100, 50)
y1 = 0.8 * x + np.random.normal(0, 15, 50)
y2 = 100 - 0.7 * x + np.random.normal(0, 15, 50)
y3 = np.random.randint(0, 100, 50)
# corrcoef()相关系数
r1 = np.corrcoef(x, y1)
r2 = np.corrcoef(x, y2)
r3 = np.corrcoef(x, y3)
# scatter:散点图
fig = plt.figure()
plt.subplot(131)
plt.scatter(x, y1, color='k')
plt.subplot(132)
plt.scatter(x, y2, color='k')
plt.subplot(133)
plt.scatter(x, y3, color='k')
print('r1: ', r1)
print('r2: ', r2)
print('r3:', r3)
plt.show()