<jupyter_start><jupyter_text>学习曲线<jupyter_code>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()
from sklearn.model_selection._split import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y, random_state=10)
X_train.shape
# 慢慢增加训练的样本数，看看"对训练数据集的预测结果"以及"对测试数据集的预测结果"的变化

from sklearn.linear_model.base import LinearRegression
from sklearn.metrics.scorer import mean_squared_error

train_score = []    # 对训练数据集的预测结果(每一次增加样本后的结果均方误差)
test_score = []     # 对测试数据集的预测结果(每一次增加样本后的结果均方误差)
for i in range(1,76):
    lin_reg = LinearRegression()
    lin_reg.fit(X_train[:i], y_train[:i])
    
    y_train_predict = lin_reg.predict(X_train[:i])
    train_score.append(mean_squared_error(y_train[:i], y_train_predict))
    
    y_test_predict = lin_reg.predict(X_test)
    test_score.append(mean_squared_error(y_test, y_test_predict))
# 开根号只是为了让数目小一些，方便观察
plt.plot([i for i in range(1,76)], np.sqrt(train_score), label='train')
plt.plot([i for i in range(1,76)], np.sqrt(test_score), label='test')
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>上图就是学习曲线  把"画学习曲线"提炼成一个函数，以便调用<jupyter_code>def plot_learning_curve(algo, X_train, X_test, y_train, y_test):
    """
    绘制学习曲线
    :param algo: 要使用的机器学习算法模型，例如LinearRegression的实例
    :param X_train: 
    :param X_test: 
    :param y_train: 
    :param y_test: 
    :return: 
    """
    train_score = []    # 对训练数据集的预测结果(每一次增加样本后的结果均方误差)
    test_score = []     # 对测试数据集的预测结果(每一次增加样本后的结果均方误差)
    for i in range(1, len(X_train) + 1):
        algo.fit(X_train[:i], y_train[:i])
        
        y_train_predict = algo.predict(X_train[:i])
        train_score.append(mean_squared_error(y_train[:i],y_train_predict))
        
        y_test_predict = algo.predict(X_test)
        test_score.append(mean_squared_error(y_test, y_test_predict))
    plt.plot([i for i in range(1,len(X_train) + 1)], np.sqrt(train_score), label='train')
    plt.plot([i for i in range(1,len(X_train) + 1)], np.sqrt(test_score), label='test')
    plt.legend()
    plt.axis([0, len(X_train) + 1, 0, 4])
    plt.show()<jupyter_output><empty_output><jupyter_text>线性回归模型的学习曲线<jupyter_code># 绘制一下线性回归模型的学习曲线
plot_learning_curve(LinearRegression(), X_train, X_test, y_train, y_test)<jupyter_output><empty_output><jupyter_text>多项式回归模型的学习曲线  2阶多项式回归<jupyter_code>from sklearn.pipeline import Pipeline
from sklearn.preprocessing.data import PolynomialFeatures
from sklearn.preprocessing.data import StandardScaler

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)<jupyter_output><empty_output><jupyter_text>与线性回归的学习曲线图相比，此图两条曲线的稳定点比较低，大概1.0左右，而线性回归是1.5以上。这说明使用2阶多项式回归，拟合效果比线性回归好  20阶多项式回归<jupyter_code>poly20_reg = PolynomialRegression(degree=20)
plot_learning_curve(poly20_reg, X_train, X_test, y_train, y_test)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.127817227539364, 'fraction_numerical': 0.01821549861068231, 'mean_word_length': 5.266924564796906, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '194596', 'license': 'MIT', 'n_tokens_mistral': 1501, 'n_tokens_neox': 1558, 'n_words': 182, 'path': 'c5_polynomial_regression/05_learning_curve.ipynb', 'repo_name': 'Sea-Monster/MachineLearningClassicAlgorithm', 'size': '3239'}