Advertisement

Python之数据分析(中位数、波动范围、极差、离差、方差、标准差)

阅读量:

文章目录

      • 一、中位数
      • 二、波动范围与极差
      • 三、离差、方差与标准差

一、中位数

1、中位数
将多个样本按照大小顺序排列,居于中间位置的元素为中位数

2、经典求法
1)A:样本集

2)L:样本数

3)M = (A[(L-1)/2] + A[L/2]) / 2

4、Numpy求法
1)对数组进行排序:np.msort(数组)

2)求中位数:M = numpy.median(数组)

5、练习

复制代码
    import numpy as np
    
    # 导入数据
    highest_prices, lowest_prices, closing_prices = np.loadtxt(
    '0=数据源/beer_price.csv', delimiter=',',
    usecols=(2, 3, 4),unpack=True
    )
    
    # 收盘价的中位数(手动求法)
    sorted_prices = np.msort(closing_prices)
    L = sorted_prices.size
    # 运用公式M = (A[(L-1)/2] + A[L/2]) / 2
    M1 = (sorted_prices[int((L-1)/2)] + sorted_prices[int(L/2)]) /2
    print(M1)
    
    
    # 用Numpy的median来求收盘价的中位数
    M2 = np.median(closing_prices)
    print(M2)
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    代码解释

二、波动范围与极差

1、价格波动范围 = 最高的最高价 - 最低的最低价
range = a.max() - b.min()

2、极差 = 一个数组的最大值 - 该数组的最小值

  • 经典求法:ptp = a.max() - a.min()
  • Numpy求法:numpy.ptp()

3、练习

复制代码
    import numpy as np
    
    # 导入数据
    highest_prices, lowest_prices, closing_prices = np.loadtxt(
    '0=数据源/beer_price.csv', delimiter=',',
    usecols=(2, 3, 4),unpack=True
    )
    
    # 价格波动范围
    range = highest_prices.max() - lowest_prices.min()
    print(range)
    
    # 极差
    ptp1 = highest_prices.max() - highest_prices.min()
    ptp2 = np.ptp(highest_prices)
    print(ptp1, ptp2, sep='\n')
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    代码解释

三、离差、方差与标准差

1、均值

  • 样本:S = [s1, s2, …, sn]
  • 均值:m = (s1 + s2 + … + sn) / n

2、离差: 每一个样本数据相对于平均值的偏离程度,是一个数组

  • 经典求法:D = [s1-m, s2-m, …, sn-m]

3、方差: 离差平方和的平均值,是一个数

  • 通用计算方式:v等于括号内各个(s_i减去m的平方)之和除以n。
    • 注意:
      • 在此公式中使用/n表示的是针对整个数据集计算出的总体方差与总标准偏差。
      • 如果我们使用的是(n−1),则对应于基于样本来计算的样本方差与样本标准偏差。
      • 当样本数量足够大时(即当n很大),这两种情况下的结果几乎相同;因此,默认情况下numpy会采用整体数据集进行计算。

4、标准差: 方差的平方根,也叫方均根离差,是一个数

  • 经典求法:standard deviation is equal to the square root of variance
    • std = sqrt(v)——>v represents the variance computed in the previous step
    • v is the variance obtained in the preceding step
  • Numpy求法:numpy function numpy.std takes an array as input
    • std = numpy.std(数组)
  • 计算样本标准差:
    • standard deviation is calculated as np.std(array, ddof=1)
    • where np denotes numpy library and array represents the input data
    • note: this calculation uses sample standard deviation by setting ddof=1

5、练习

复制代码
    import numpy as np
    
    # 导入数据
    closing_prices = np.loadtxt(
    '0=数据源/beer_price.csv', delimiter=',',
    usecols=(4),unpack=True
    )
    
    # 计算均值、 离差、 方差、 标准差
    mean_price = np.mean(closing_prices)  # 平均值
    devs = closing_prices - mean_price  # 离差
    var = (devs ** 2).mean()  # 方差:离差平方和的平均值
    std1 = np.sqrt(var)  # 标准差
    print(std1)
    
    # 用numpy来直接算标准差
    std2 = np.std(closing_prices)
    print(std2)
    
    # 计算样本标准差
    std3 = np.std(closing_prices, ddof=1)
    print(std3)
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    代码解释
11

全部评论 (0)

还没有任何评论哟~