<jupyter_start><jupyter_text>Learning Curves are a way to check on overfitting or underfitting of your model. (High degree polynomial linear regression will overfit the data)<jupyter_code>from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt

def plot_learning_curves(model, X, y):
    X_train, X_val, y_train, y_val = train_test_split(X, y, test_size = 0.2)
    train_errors, val_errors = [], [] #empty lists
    for m in range(1, len(X_train)):
        model.fit(X_train[:m], y_train[:m]) #fit on data up to state of current for loop
        y_train_predict = model.predict(X_train[:m])
        y_val_predict = model.predict(X_val)
        train_errors.append(mean_squared_error(y_train[:m], y_train_predict))
        val_errors.append(mean_squared_error(y_val, y_val_predict))
    plt.plot(np.sqrt(train_errors), "r-+", linewidth = 2, label = "train")
    plt.plot(np.sqrt(val_errors), "b-",  linewidth = 3, label = "val")

lin_reg = LinearRegression()
plot_learning_curves(lin_reg, X, y)<jupyter_output><empty_output><jupyter_text>As we get more data to train on we get closer to the error rate of the validation set.<jupyter_code># What if we train on a 10th degree polynomial
# also going to use sklearn's pipeline idea

from sklearn.pipeline import Pipeline

polynomial_regression = Pipeline([
    ("poly_features", PolynomialFeatures(degree = 10, include_bias = False)),
    ("lin_reg", LinearRegression()),
])

plot_learning_curves(polynomial_regression, X, y)<jupyter_output><empty_output><jupyter_text>Can't really see it here without adjusting the axes.<jupyter_code>#<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10308056872037914, 'fraction_numerical': 0.00533175355450237, 'mean_word_length': 5.496153846153846, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '553053', 'license': 'MIT', 'n_tokens_mistral': 587, 'n_tokens_neox': 560, 'n_words': 150, 'path': 'homl2/chapter4.ipynb', 'repo_name': 'natekratzer/py_study', 'size': '1688'}