<jupyter_start><jupyter_text>Test Train Split<jupyter_code>train = df.query('Year < 2008')
test = df.query('Year >=2008')
X_train = train[features]
y_train = train[target]
X_test = test[features]
y_test = test[target]

X_train.shape, y_train.shape, X_test.shape, y_test.shape
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
y_pred
y_test
mean_absolute_error(y_test, y_pred)<jupyter_output><empty_output><jupyter_text>Leave-One-Out Cross-Validation<jupyter_code>from sklearn.linear_model import LinearRegression
print('Leave-One-Out Cross-Validation, Do-It-Yourself implemnetation')

maes =[]

for year in df['Year']: 
    print(f'Fit Linear Regression on all years except {year}')
    
    #Choose model hyperparmeters by instantiating this class
    model = LinearRegression()
    
    #Arrange data into X features matrix and y target 
    train = df[df['Year'] !=year]
    test = df[df['Year'] == year]
    X_train = train[features]
    y_train = train[target]
    X_test = test[features]
    y_test = test[target]
    
    #Fit the model to your data by calling
    model.fit(X_train, y_train)
    
    #Apply the model to new data: predict labels for unknown data using predict() method
    y_pred = model.predict(X_test)
    
    mae= mean_absolute_error(y_test, y_pred)
    maes.append(mae)
    print(f'Absolute Error on prediction for {year} = {mae}')
    
    
#Leave-One-Out Cross-Validation Mean Absolute Error
pd.Series(maes).mean()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1111111111111111, 'fraction_numerical': 0.0053872053872053875, 'mean_word_length': 4.384057971014493, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '118506', 'license': 'MIT', 'n_tokens_mistral': 537, 'n_tokens_neox': 517, 'n_words': 124, 'path': 'module3-doing-linear-regression/doing_linear_regression_assignment.ipynb', 'repo_name': 'KevWebb/DS-Unit-2-Regression-1', 'size': '1485'}