42 lines
1.4 KiB
Python
42 lines
1.4 KiB
Python
"""端到端最小流程:读数据 → 清洗 → 描述 → bootstrap → 出图。
|
||||
|
|
|
|||
|
|
体量刻意压到最小,只用来确认"环境 + 包 + 出图"这条链路是通的。
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
from __future__ import annotations
|
|||
|
|
|
|||
|
|
import numpy as np
|
|||
|
|
|
|||
|
|
import mcm_toolkit as mcm
|
|||
|
|
|
|||
|
|
|
|||
|
|
def main() -> None:
|
|||
|
|
mcm.set_seed(20260101)
|
|||
|
|
|
|||
|
|
# 1) 造一份带缺失和离群点的数据(真实赛题里这步是 load_table)
|
|||
|
|
rng = np.random.default_rng(20260101)
|
|||
|
|
heights = rng.normal(170, 8, size=300)
|
|||
|
|
heights[[3, 77]] = [260.0, -10.0] # 两个录入错误
|
|||
|
|
|
|||
|
|
# 2) 找出离群点(约定:只标不删,删不删是人来决定)
|
|||
|
|
mask = np.asarray(mcm.data.detect_outliers(heights, method="iqr"))
|
|||
|
|
print(f"检出离群点 {mask.sum()} 个")
|
|||
|
|
|
|||
|
|
clean = heights[~mask]
|
|||
|
|
|
|||
|
|
# 3) 描述统计
|
|||
|
|
print(mcm.stats.describe(clean))
|
|||
|
|
|
|||
|
|
# 4) 中位数的 bootstrap 置信区间(统计量没有解析分布时的通用答案)
|
|||
|
|
ci = mcm.bootstrap_ci(clean, statistic=np.median, n_resamples=10000, random_state=20260101)
|
|||
|
|
print(f"中位数 = {np.median(clean):.2f}, 95% CI = [{ci.params['low']:.2f}, {ci.params['high']:.2f}]")
|
|||
|
|
|
|||
|
|
# 5) 出图(默认同时写 png + pdf,300 dpi)
|
|||
|
|
mcm.viz.mcm_style()
|
|||
|
|
fig, ax = mcm.viz.plot(clean, kind="hist", title="身高分布", xlabel="cm")
|
|||
|
|
paths = mcm.viz.save_figure(fig, "figures/quickstart_hist")
|
|||
|
|
print("已写出:", [str(p) for p in paths])
|
|||
|
|
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
main()
|