• 目的:更直观的方式来判断拟合规律
  • 方法:通过逐渐增加数据(特征),观察得到的模型在训练数据集和测试数据集上的表现。

    绘制学习曲线

    ```python import numpy as np import matplotlib.pyplot as plt

np.random.seed(666) x = np.random.uniform(-3.0, 3.0, size=100) X = x.reshape(-1, 1) y = 0.5 x*2 + x + 2 + np.random.normal(0, 1, size=100)

plt.scatter(x, y) plt.show()

  1. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/12405790/1637499707413-54952f6f-4905-45a6-a1d9-46017106fad9.png#clientId=u5ad2a576-aa83-4&from=paste&id=ufd10b366&margin=%5Bobject%20Object%5D&name=image.png&originHeight=252&originWidth=372&originalType=url&ratio=1&size=6591&status=done&style=none&taskId=u658a89b9-9566-4aa8-a652-2ae5d9649da)
  2. ```python
  3. from sklearn.model_selection import train_test_split
  4. X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=10)
  5. X_train.shape # (75, 1)
  6. from sklearn.linear_model import LinearRegression
  7. from sklearn.metrics import mean_squared_error
  8. train_score = []
  9. test_score = []
  10. for i in range(1, 76):
  11. lin_reg = LinearRegression()
  12. lin_reg.fit(X_train[:i], y_train[:i])
  13. y_train_predict = lin_reg.predict(X_train[:i])
  14. train_score.append(mean_squared_error(y_train[:i], y_train_predict))
  15. y_test_predict = lin_reg.predict(X_test)
  16. test_score.append(mean_squared_error(y_test, y_test_predict))
  17. plt.plot([i for i in range(1, 76)], np.sqrt(train_score), label="train")
  18. plt.plot([i for i in range(1, 76)], np.sqrt(test_score), label="test")
  19. plt.legend()
  20. plt.show()

image.png

封装学习曲线

  1. def plot_learning_curve(algo, X_train, X_test, y_train, y_test):
  2. train_score = []
  3. test_score = []
  4. for i in range(1, len(X_train)+1):
  5. algo.fit(X_train[:i], y_train[:i])
  6. y_train_predict = algo.predict(X_train[:i])
  7. train_score.append(mean_squared_error(y_train[:i], y_train_predict))
  8. y_test_predict = algo.predict(X_test)
  9. test_score.append(mean_squared_error(y_test, y_test_predict))
  10. plt.plot([i for i in range(1, len(X_train)+1)],
  11. np.sqrt(train_score), label="train")
  12. plt.plot([i for i in range(1, len(X_train)+1)],
  13. np.sqrt(test_score), label="test")
  14. plt.legend()
  15. plt.axis([0, len(X_train)+1, 0, 4])
  16. plt.show()

欠拟合学习曲线

  1. plot_learning_curve(LinearRegression(), X_train, X_test, y_train, y_test)

image.png

  • 测试集、训练集上误差大且接近,说明模型拟合效果不好

    恰拟合学习曲线

    ```python

    二阶多项式学习曲线

    from sklearn.preprocessing import PolynomialFeatures from sklearn.preprocessing import StandardScaler from sklearn.pipeline import Pipeline

def PolynomialRegression(degree): return Pipeline([ (“poly”, PolynomialFeatures(degree=degree)), (“std_scaler”, StandardScaler()), (“lin_reg”, LinearRegression()) ])

poly2_reg = PolynomialRegression(degree=2) plot_learning_curve(poly2_reg, X_train, X_test, y_train, y_test)

  1. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/12405790/1637499993577-491fac14-e61b-4b23-9e95-4076b2886c12.png#clientId=u5ad2a576-aa83-4&from=paste&id=u322f0b32&margin=%5Bobject%20Object%5D&name=image.png&originHeight=252&originWidth=375&originalType=url&ratio=1&size=11990&status=done&style=none&taskId=u139460c5-9be3-42db-bf8a-968a331bd76)
  2. - [x] 测试集、训练集上误差小且接近,说明模型拟合效果好
  3. <a name="efYgV"></a>
  4. # 过拟合学习曲线
  5. ```python
  6. poly20_reg = PolynomialRegression(degree=20)
  7. plot_learning_curve(poly20_reg, X_train, X_test, y_train, y_test)

image.png

  • 训练集上误差小,说明在训练集上拟合好
  • 测试集上误差大,说明在测试集上拟合不好,泛化能力不好

    拟合曲线对比

    image.png
    image.png