20 残差图 residplot

20 残差图 residplot

这类图是什么

回归残差对自变量的散点,可叠加 lowess 趋势线。

看什么:检验模型是否够用:残差均匀散布说明线性假设成立。

5 个案例

20-1 气温—负荷回归的残差:残差应无明显结构

残差在零线两侧均匀散布说明线性模型够用;若出现弯曲或喇叭形,说明该换非线性模型或存在异方差。

气温—负荷回归的残差:残差应无明显结构

20-2 效率—负载率回归的残差:看是否有系统性偏离

效率—负载率回归的残差:看是否有系统性偏离

20-3 辐照度—出力回归的残差:异方差性检查

辐照度—出力回归的残差:异方差性检查

20-4 电压—电流回归的残差:检验线性假设

电压—电流回归的残差:检验线性假设

20-5 风速—出力二次回归的残差:模型是否够用

风速—出力二次回归的残差:模型是否够用

代码

5 个函数取自 code/第四批_回归与矩阵261107.py。关键约束:不调用 sns.set_theme(),否则 font.family 被改成 Arial,中文会变方框;版式由 code/公共配置261107.py 里的 setup_cn_style() 与 save() / save_grid() 统一接管。

# 摘自 code/第四批_回归与矩阵261107.py

def rs_01():
    """气温—负荷回归的残差:残差应无明显结构。

    残差在零线两侧均匀散布说明线性模型够用;若出现弯曲或喇叭形,
    说明该换非线性模型或存在异方差。
    """
    fig, ax = new_fig(left=0.19, bottom=0.21)
    rng = np.random.default_rng(2001)
    t = rng.normal(28, 8, 180)
    p = 380 + 9.5 * (t - 20) + rng.normal(0, 52, 180)
    df = pd.DataFrame({"气温": t, "负荷": p})
    sns.residplot(data=df, x="气温", y="负荷", ax=ax, color=C1,
                  scatter_kws={"s": 14, "edgecolor": "white"},
                  line_kws={"lw": 0.9, "color": C2})
    ax.set_xlabel("气温 " + IT["T"] + " / °C")
    ax.set_ylabel("残差 / kW")
    save(fig, "20_残差图", "20_残差图_气温负荷回归")

def rs_02():
    """效率—负载率回归的残差:看是否有系统性偏离。"""
    fig, ax = new_fig(left=0.19, bottom=0.21)
    rng = np.random.default_rng(2002)
    b = rng.uniform(0.15, 1.0, 170)
    eta = 98.4 - 1.5 * (1 - b) ** 1.4 - 0.5 * b ** 2 + rng.normal(0, 0.07, 170)
    df = pd.DataFrame({"负载率": b, "效率": eta})
    sns.residplot(data=df, x="负载率", y="效率", ax=ax, color=C3,
                  scatter_kws={"s": 14, "edgecolor": "white"},
                  line_kws={"lw": 0.9, "color": C2})
    ax.set_xlabel("负载率 " + IT["beta"])
    ax.set_ylabel("残差 / %")
    save(fig, "20_残差图", "20_残差图_效率负载率回归")

def rs_03():
    """辐照度—出力回归的残差:异方差性检查。"""
    fig, ax = new_fig(left=0.19, bottom=0.21)
    rng = np.random.default_rng(2003)
    g = rng.uniform(0, 1050, 190)
    p = 0.90 * g + rng.normal(0, 0.03 * g + 8, 190)
    df = pd.DataFrame({"辐照度": g, "出力": p})
    sns.residplot(data=df, x="辐照度", y="出力", ax=ax, color=C1, lowess=True,
                  scatter_kws={"s": 13, "edgecolor": "white"},
                  line_kws={"lw": 1.0, "color": C2})
    ax.set_xlabel("辐照度 " + IT["G"] + " / (W/m²)")
    ax.set_ylabel("残差 / kW")
    save(fig, "20_残差图", "20_残差图_辐照出力回归")

def rs_04():
    """电压—电流回归的残差:检验线性假设。"""
    fig, ax = new_fig(left=0.19, bottom=0.21)
    rng = np.random.default_rng(2004)
    i = rng.gamma(9, 30, 200)
    u = 10.18 - 0.00095 * i + rng.normal(0, 0.045, 200)
    df = pd.DataFrame({"电流": i, "电压": u})
    sns.residplot(data=df, x="电流", y="电压", ax=ax, color=C5,
                  scatter_kws={"s": 13, "edgecolor": "white"},
                  line_kws={"lw": 0.9, "color": C2})
    ax.set_xlabel("负荷电流 " + IT["I"] + " / A")
    ax.set_ylabel("残差 / kV")
    save(fig, "20_残差图", "20_残差图_电压电流回归")

def rs_05():
    """风速—出力二次回归的残差:模型是否够用。"""
    fig, ax = new_fig(left=0.19, bottom=0.21)
    rng = np.random.default_rng(2005)
    v = rng.uniform(2, 18, 190)
    p = np.clip(0.42 * v ** 3, 0, 1650) + rng.normal(0, 90, 190)
    df = pd.DataFrame({"风速": v, "出力": np.clip(p, 0, None)})
    sns.residplot(data=df, x="风速", y="出力", ax=ax, color=C1, order=2, lowess=True,
                  scatter_kws={"s": 13, "edgecolor": "white"},
                  line_kws={"lw": 1.0, "color": C2})
    ax.set_xlabel("风速 " + IT["v"] + " / (m/s)")
    ax.set_ylabel("残差 / kW")
    save(fig, "20_残差图", "20_残差图_风速出力回归")

说明

  • axes 级图 1181×944 px;figure 级多轴图按内容取 1100~1470 px 宽。
  • 图内不放标题,图题由本文件的标题承担。
  • 数据为 10 kV 配网、380 V 低压、50 Hz 量级的仿真算例,随机种子固定。
posted @ 2026-11-08 08:32  小青蛙电气  阅读(4)  评论(0)    收藏  举报