IT评测·应用市场-qidao123.com技术社区

标题: 【漫话呆板学习系列】199.过拟合 vs 欠拟合(Overfit vs Underfit) [打印本页]

作者: 耶耶耶耶耶    时间: 2025-4-14 20:44
标题: 【漫话呆板学习系列】199.过拟合 vs 欠拟合(Overfit vs Underfit)

呆板学习核心标题:过拟合 vs 欠拟合

   图示作者:Chris Albon
  
1. 什么是拟合(Fit)?

拟合(Fit)是指模子对数据的学习效果。
   理想目标:
  
  
2. 什么是过拟合(Overfitting)?

定义

过拟合是指模子在训练集上表现很好,但在测试集或新数据上效果很差。模子学到了“噪声”或“非常值”的特征。

特征

特征表现方差很大对数据过分敏感决策边界复杂曲折、震荡泛化能力差新数据易失败
图示(来自图片左侧)

  1. 特征2 ↑
  2.        ● ○   ○
  3.         ○ ○
  4.     ○ ● ● ○  ← 决策边界很曲折
  5. 特征1 →
复制代码
 

3. 什么是欠拟合(Underfitting)?

定义

欠拟合是指模子太简朴,无法捕捉数据的有用规律,无论在训练集还是测试集上效果都不好。

特征

特征表现毛病很大无法拟合数据规律决策边界简朴接近直线泛化能力弱无法有用学习
图示(来自图片右侧)

  1. 特征2 ↑
  2.        ● ○   ○
  3.         ○ ○
  4.     ○ ● ● ○  ← 决策边界几乎是直线
  5. 特征1 →
复制代码
 

4. 理想拟合(Best Fit)

状态



图示(图片中心)

  1. 特征2 ↑
  2.        ● ○   ○
  3.         ○ ○
  4.     ○ ● ● ○  ← 决策边界几乎是直线
  5. 特征1 →
复制代码
 

5. 数学公式:毛病-方差分解(Bias-Variance Tradeoff)

理论公式

呆板学习盼望误差可以分解为:

名称含义Bias毛病,模子简朴Variance方差,模子敏感Irreducible Error无法消除的随机误差
6. Python实操示例

构造数据

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from sklearn.model_selection import train_test_split
  4. from sklearn.preprocessing import PolynomialFeatures
  5. from sklearn.linear_model import LinearRegression
  6. from sklearn.metrics import mean_squared_error
  7. # 真实函数
  8. def true_func(x):
  9.     return np.sin(2 * np.pi * x)
  10. np.random.seed(0)
  11. x = np.sort(np.random.rand(30))
  12. y = true_func(x) + np.random.normal(scale=0.3, size=x.shape)
  13. x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3)
复制代码
 

欠拟合(低阶多项式)

  1. poly1 = PolynomialFeatures(degree=1)
  2. x1_train = poly1.fit_transform(x_train.reshape(-1, 1))
  3. x1_test = poly1.transform(x_test.reshape(-1, 1))
  4. model1 = LinearRegression().fit(x1_train, y_train)
  5. print('欠拟合 MSE:', mean_squared_error(y_test, model1.predict(x1_test)))
复制代码
  1. 欠拟合 MSE: 0.418206083278207
复制代码
 

过拟合(高阶多项式)

  1. poly15 = PolynomialFeatures(degree=15)
  2. x15_train = poly15.fit_transform(x_train.reshape(-1, 1))
  3. x15_test = poly15.transform(x_test.reshape(-1, 1))
  4. model15 = LinearRegression().fit(x15_train, y_train)
  5. print('过拟合 MSE:', mean_squared_error(y_test, model15.predict(x15_test)))
复制代码
  1. 过拟合 MSE: 2.732472745353921
复制代码
 

理想环境(适中阶)

  1. poly4 = PolynomialFeatures(degree=4)
  2. x4_train = poly4.fit_transform(x_train.reshape(-1, 1))
  3. x4_test = poly4.transform(x_test.reshape(-1, 1))
  4. model4 = LinearRegression().fit(x4_train, y_train)
  5. print('理想拟合 MSE:', mean_squared_error(y_test, model4.predict(x4_test)))
复制代码
  1. 理想拟合 MSE: 0.1546713133312227
复制代码
 

7. 如何制止过拟合与欠拟合?

标题范例解决思绪过拟合- 低沉模子复杂度
- 增长正则化(L1/L2)
- 增长数据量
- Dropout
- 提前停止欠拟合- 增长特征
- 增强模子复杂度
- 低沉正则化
- 利用更强模子
8. 可视化毛病-方差关系(效果示意)

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from sklearn.model_selection import train_test_split
  4. from sklearn.preprocessing import PolynomialFeatures
  5. from sklearn.linear_model import LinearRegression
  6. from sklearn.metrics import mean_squared_error
  7. # 真实函数
  8. def true_func(x):
  9.     return np.sin(2 * np.pi * x)
  10. np.random.seed(0)
  11. x = np.sort(np.random.rand(30))
  12. y = true_func(x) + np.random.normal(scale=0.3, size=x.shape)
  13. x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3)
  14. complexity = np.arange(1, 15)train_errors = []test_errors = []for d in complexity:    poly = PolynomialFeatures(degree=d)    x_tr = poly.fit_transform(x_train.reshape(-1, 1))    x_te = poly.transform(x_test.reshape(-1, 1))    model = LinearRegression().fit(x_tr, y_train)    train_errors.append(mean_squared_error(y_train, model.predict(x_tr)))    test_errors.append(mean_squared_error(y_test, model.predict(x_te)))plt.plot(complexity, train_errors, label='Train Error')plt.plot(complexity, test_errors, label='Test Error')plt.xlabel('Model Complexity (degree)')plt.ylabel('MSE')plt.legend()plt.show()
复制代码
 

 

9. 总结

项目过拟合欠拟合决策边界复杂简朴毛病Bias低高方差Variance高低表现训练好,测试差训练差,测试差
10. 最佳实践

   探求毛病和方差的均衡,是呆板学习调参的艺术。
公道的特征工程 + 模子复杂度控制 + 正则化技能,是最佳组合。
   

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com技术社区 (https://dis.qidao123.com/) Powered by Discuz! X3.4