Z-Score(标准分):公众号每日收入分析

Z-Score 是统计学里最经典的标准化手段,具备简单、可解释等特性,适用于风控、时序告警、制造业设备监控、机器学习相关应用的场景中。

什么是 Z-Score

Z-Score,又叫Z 分数 / 标准分,它把原始数值,转换成“距离均值有多少个标准差”,实现无量纲化。

无量纲化:去掉指标自带的单位,让不同量纲的数据可以放在一起比较、计算。

Z-Score 计算公式为:

$$ z = \frac{x - \mu}{\sigma} $$
  • $x$:原始观测值
  • $\mu$:总体均值
  • $\sigma$:总体标准差

总体标准差无法计算时,使用样本标准差估计。

Z-Score 的解读:

  • $z = 0$:当前值刚好等于均值
  • $z > 0$:当前值高于均值
  • $z < 0$:当前值低于均值
  • $|z|$ 越大,偏离群体中心越远

每日收入 Z-Score 解析

开通微信公众号流量主也有 1 个月了,结合 Z-Score,我想分析下每日收入的高低,以下最近 29 日的收入数据:

2026-11-01 ~ 2026-11-29

1
2
3
4
5
6
7
8
earnings = [
    0.50, 0.28, 0.29, 0.17, 0.24, 
    0.36, 0.13, 0.38, 0.46, 0.60,
    0.05, 0.13, 0.07, 0.12, 0.05,
    0.08, 0.12, 0.07, 0.11, 0.06,
    0.01, 0.01, 0.11, 0.08, 0.25,
    0.13, 0.08, 0.05, 0.02,
]
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import statistics

import matplotlib.pyplot as plt

plt.rcParams["font.sans-serif"] = ["PingFang SC", "Arial Unicode MS", "SimHei", "sans-serif"]
plt.rcParams["axes.unicode_minus"] = False

days = list(range(1, len(earnings) + 1))

mu = statistics.mean(earnings)
sigma = statistics.pstdev(earnings)
median = statistics.median(earnings)
q1, _, q3 = statistics.quantiles(earnings, n=4, method="inclusive")

z_scores = [(x - mu) / sigma for x in earnings]
rolling7 = [
    statistics.mean(earnings[max(0, i - 6):i + 1])
    for i in range(len(earnings))
]

high_days = [(d, x) for d, x, z in zip(days, earnings, z_scores) if z >= 2]

fig, axes = plt.subplots(2, 1, figsize=(11, 7), constrained_layout=True)
fig.suptitle("每日收入水平分析", fontsize=14)

axes[0].plot(days, earnings, marker="o", label="每日收入")
axes[0].plot(days, rolling7, color="orange", linewidth=2, label="7 日均值")
axes[0].axhline(mu, color="green", linestyle="--", label=f"均值 {mu:.3f}")
axes[0].fill_between(
    days,
    [mu - sigma] * len(days),
    [mu + sigma] * len(days),
    color="green",
    alpha=0.1,
    label="±1σ 区间",
)
axes[0].scatter(
    [d for d, _ in high_days],
    [x for _, x in high_days],
    color="red",
    zorder=5,
    label="高异常日",
)
axes[0].set_title("每日收入与 7 日均值")
axes[0].set_xlabel("天数")
axes[0].set_ylabel("收入")
axes[0].grid(alpha=0.3)
axes[0].legend()

axes[1].bar(
    days,
    z_scores,
    color=["tab:red" if z >= 2 else "tab:blue" for z in z_scores],
)
axes[1].axhline(0, color="black", linewidth=0.8)
axes[1].axhline(2, color="red", linestyle="--", linewidth=1, label="+2σ")
axes[1].axhline(-2, color="red", linestyle="--", linewidth=1)
axes[1].set_title("每日收入 Z-score")
axes[1].set_xlabel("天数")
axes[1].set_ylabel("z-score")
axes[1].grid(alpha=0.3)
axes[1].legend()

plt.show()

从图中可以看出:

  1. 超过 $1\sigma$ 有 5 天,算日收入高于平均水平
  2. 超过 $2\sigma$ 有 2 天,分别是 2026-11-01 和 2026-11-10,算日收入比较可观的
  3. 其它日期都是便秘,一点点,^_^T

上述示例,是针对同一类数据进行比较分析,没有考虑不同数据之间的单位差异或业务场景。

小结

  1. 介绍 Z-Score 的概念、计算公式、解读
  2. 解析每日收入 Z-Score,分析异常日,结论:收入一般
  3. 不同数据类型间对比,可使用 Z-Score 进行标准化,再进行对比分析(本文中未体现,举一反三)