描述性统计与推断统计

使用 Python 分析调查数据

EbunOluwa Andrew

Data Scientist

描述性统计

  • 用于描述调查数据的基本度量
  • 例如:均值、中位数、众数、极差、标准差等
  • .describe()

由 Lukas 拍摄 - 图表特写

1 Photo by Lukas
使用 Python 分析调查数据

.describe() 函数

data.describe()
|      | year     | satisfaction_rating
|------|----------|--------------------
| count|       42 |                  42
| mean | 2012.381 |            7192.857
| std  |    4.196 |             945.178
| min  |     2006 |                5500
| 25%  |     2009 |                6325
| 50%  |   2012.5 |                7400
| 75%  |     2016 |                8000
| max  |     2019 |                8600
data.describe(include = np.object)
|        | category    |
|--------|-------------|
| count  | 42          |
| unique | 3           |
| top    | Residential |
| freq   | 14          |
使用 Python 分析调查数据

解读 .describe()

  • 离群值 = 最大值 > 均值和中位数
  • 不合理值 = 与常理不符的值
|       | year     | satisfaction_rating |
|-------|----------|---------------------|
| count |       42 |                  42 |
| mean  | 2012.381 |            7192.857 |
| std   |    4.196 |             945.178 |
| min   |     2006 |                5500 |
| 25%   |     2009 |                6325 |
| 50%   |   2012.5 |                7400 |
| 75%   |     2016 |                8000 |
| max   |     2019 |                8600 |
使用 Python 分析调查数据

解读 .describe()

  • Top = 众数 = 出现次数最多的类别
  • Freq = 该类别出现的次数
|        | category    |
|--------|-------------|
| count  | 42          |
| unique | 3           |
| top    | Residential |
| freq   | 14          |
使用 Python 分析调查数据

对 electric_satisfaction 使用 .describe()

import pandas as pd

electric_satisfaction = pd.read_csv("austin-energy-customer-satisfaction.csv")
使用 Python 分析调查数据

对 electric_satisfaction 使用 .describe()

electric_satisfaction.describe()
|      | year     | satisfaction_rating
|------|----------|--------------------
| count|       42 |                  42
| mean | 2012.381 |            7192.857
| std  |    4.196 |             945.178
| min  |     2006 |                5500
| 25%  |     2009 |                6325
| 50%  |   2012.5 |                7400
| 75%  |     2016 |                8000
| max  |     2019 |                8600
  • satisfaction_rating 存在离群值
  • 第50百分位 = 中位数
使用 Python 分析调查数据

对 electric_satisfaction 使用 .describe()

|        | category    |
|--------|-------------|
| count  | 42          |
| unique | 3           |
| top    | Residential |
| freq   | 14          |
  • 众数 = Residential 受访者
使用 Python 分析调查数据

推断统计

  • 判断数据能否推广到总体
  • 样本量 < 总体规模 -> 抽样误差
  • 估计总体参数
    • 置信区间
      • norm.interval() 函数

Pexels 上 Andrea Piacquadio 拍摄 - 手持灯泡的女子

1 Photo by Andrea Piacquadio on Pexels
使用 Python 分析调查数据

norm.interval() 函数

  • 适用于大样本
  • 假设样本均值的抽样分布近似正态
import scipy.stats
scipy.stats.norm.interval(alpha, loc, scale)
  • alpha = 置信水平
  • loc = 样本均值
  • scale= 样本标准误
使用 Python 分析调查数据

解读 electric_satisfaction 的 norm.interval()

electric_satisfaction = pd.read_csv(
  "austin-energy-customer-satisfaction.csv")

conf_interval = st.norm.interval(
  alpha = 0.99,
  loc = np.mean(electric_satisfaction.satisfaction),
  scale=st.sem(electric_satisfaction.satisfaction))

print(conf_interval)
(6817.187361704269, 7568.526924010017)
使用 Python 分析调查数据

¡Vamos a practicar!

使用 Python 分析调查数据

Preparing Video For Download...