import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
# 设置中文字体
# 注意:这需要在您的环境中安装支持中文的字体,例如 "SimHei"
# 如果没有,matplotlib会回退到默认字体,中文可能显示为方框
mpl.rcParams['font.sans-serif'] = ['SimHei', 'Microsoft YaHei', 'Arial Unicode MS']
mpl.rcParams['axes.unicode_minus'] = False # 解决负号显示问题
x = np.linspace(0.1, 1, 100)
bias_sq = 0.8 / (x * 10)
variance = x * 0.4
noise = np.full_like(x, 0.1)
total_error = bias_sq + variance + noise
min_error_idx = np.argmin(total_error)
optimal_complexity = x[min_error_idx]
fig, ax = plt.subplots(figsize=(10, 6))
ax.plot(x, bias_sq, 'r-', label='偏差平方 (Bias²)')
ax.plot(x, variance, 'b--', label='方差 (Variance)')
ax.plot(x, noise, 'orange', linestyle=':', label='噪声 (Irreducible Error)')
ax.plot(x, total_error, 'k-.', label='总误差 (Total Error)', linewidth=2)
ax.axvline(optimal_complexity, color='grey', linestyle='--', label='最佳复杂度')
ax.fill_between(x, 0, 1.2, where=x < optimal_complexity, color='lightblue', alpha=0.3, label='欠拟合区域')
ax.fill_between(x, 0, 1.2, where=x > optimal_complexity, color='lightcoral', alpha=0.3, label='过拟合区域')
ax.set_xlabel('模型复杂度', fontsize=14)
ax.set_ylabel('误差', fontsize=14)
ax.set_title('偏差-方差权衡', fontsize=16)
ax.legend(fontsize=12)
ax.set_ylim(0, 1.2)
ax.set_xlim(0, 1)
ax.text(optimal_complexity - 0.25, 1.1, '欠拟合\n(Underfitting)', ha='center', fontsize=12)
ax.text(optimal_complexity + 0.25, 1.1, '过拟合\n(Overfitting)', ha='center', fontsize=12)
ax.plot(optimal_complexity, total_error[min_error_idx], 'ko', markersize=8)
ax.annotate('平衡点',
xy=(optimal_complexity, total_error[min_error_idx]),
xytext=(optimal_complexity, total_error[min_error_idx] + 0.2),
arrowprops=dict(facecolor='black', shrink=0.05),
ha='center', fontsize=12)
plt.show()