<jupyter_start><jupyter_text>Get best params for BaggingEstimator<jupyter_code>X_train, y_train = data.train_data()
bag_model = BaggingEstimator()

bag_model.set_params(pipeline__correlationselector__labels=y_train)
bag_params = {
    'pipeline__baggingregressor__base_estimator__booster': ['gbtree', 'gblinear', 'dart'],
    'pipeline__baggingregressor__base_estimator__colsample_bylevel': stats.uniform(0.8, 0.2),
    'pipeline__baggingregressor__base_estimator__colsample_bytree': stats.uniform(0.8, 0.2),
    'pipeline__baggingregressor__base_estimator__learning_rate': stats.uniform(0.1, 0.04),
    'pipeline__baggingregressor__base_estimator__max_depth': stats.randint(2, 10),
    'pipeline__baggingregressor__base_estimator__n_estimators': stats.randint(75, 150),
    'pipeline__baggingregressor__base_estimator__reg_alpha': stats.uniform(0.05, 0.05),
    'pipeline__baggingregressor__base_estimator__reg_lambda': stats.uniform(0.1, 0.04),
    'pipeline__baggingregressor__base_estimator__subsample': stats.uniform(0.8, 0.2),
    'pipeline__baggingregressor__n_estimators': stats.randint(5, 15),
    'pipeline__correlationselector__threshold': stats.uniform(0.03, 0.02),
}

bag_cv = RandomizedSearchCV(
    bag_model,
    bag_params,
    scoring='neg_mean_absolute_error',
    n_jobs=-1,
    cv=YearSplit(n_splits=N_SPLITS),
    n_iter=N_ITER,
    random_state=SEED,
    verbose=2
)
                            
bag_cv
bag_cv.fit(*data.train_data())
bag_cv.best_score_, bag_cv.refit_time_, bag_cv.best_params_<jupyter_output><empty_output><jupyter_text>Compare to FootyTipper<jupyter_code>fitzroy = FitzroyDataReader()
match_results = fitzroy.match_results()

match_results
ft_predictions = (
    pd.read_csv(os.path.join(PATH, '..', 'data', 'footy_tipper_predictions_2018.csv'))
    .rename(columns=lambda col: col.lower().replace(' ', '_'))
    .assign(date=lambda df: df['date'].pipe(pd.to_datetime))
)

ft_predictions
ft_accuracy_df = (
    ft_predictions
    .merge(
        match_results[match_results['season'] == 2018]
        .assign(date=lambda df: df['date'].pipe(pd.to_datetime, unit='D'))
        .loc[:, ['date', 'home_team', 'away_team', 'home_points', 'away_points', 'round_number']],
        on=['date', 'home_team', 'away_team'],
        how='inner',
    )
    .assign(
        home_win=lambda df: (df['home_points'] > df['away_points']).astype(float),
        draw=lambda df: (df['home_points'] == df['away_points']).astype(float),
    )
    .assign(correct=lambda df: (
        (df['draw'].astype(bool)) | (df['home_win_predicted'] == df['home_win'])).astype(float)
    )
    .drop(['home_points', 'away_points'], axis=1)
)

ft_accuracy_df
print('2018 Footy Tipper Performance')
print('Tipping points:', int(ft_accuracy_df['correct'].sum()))
print('Tipping accuracy:', f"{round(ft_accuracy_df['correct'].mean() * 100, 2)}%")
data.train_years = (None, 2017)
data.test_years = (2018, 2018)

bag_cv.best_estimator_.fit(*data.train_data())
X_train, y_train = data.train_data()
X_test, y_test = data.test_data()
y_pred = bag_cv.best_estimator_.predict(X_test)

pred_df = (
    pd.concat([y_test, pd.Series(y_pred, index=y_test.index).rename('predicted_margin')], axis=1)
    .assign(at_home=X_test['at_home'], oppo_team=X_test['oppo_team'])
    .reset_index()
    .rename(columns={'level_0': 'team', 'level_1': 'year', 'level_2': 'round_number'})
)

home_df = (
    pred_df[(pred_df['at_home'] == 1) & (pred_df['round_number'] <= 23)]
    .rename(
        columns={
            'team': 'home_team',
            'oppo_team': 'away_team',
            'margin': 'home_margin',
            'predicted_margin': 'predicted_home_margin'
        }
    )
)

away_df = (
    pred_df[(pred_df['at_home'] == 0) & (pred_df['round_number'] <= 23)]
    .rename(columns={'team': 'away_team', 'oppo_team': 'home_team', 'predicted_margin': 'predicted_away_margin'})
    .loc[:, ['home_team', 'year', 'round_number', 'away_team', 'predicted_away_margin']]
)

accuracy_df = (
    home_df
    .merge(away_df, on=['home_team', 'away_team', 'year', 'round_number'], how='outer')
    .assign(correct=lambda df: (df['home_margin'] == 0) |
            ((df['home_margin'] < 0) & (df['predicted_home_margin'] < df['predicted_away_margin'])) |
            ((df['home_margin'] > 0) & (df['predicted_home_margin'] > df['predicted_away_margin'])))
    .sort_values(['year', 'round_number'])
    .drop('at_home', axis=1)
)

accuracy_df
print('2018 Tipresias Performance')
print('Tipping points:', int(accuracy_df['correct'].sum()))
print('Tipping accuracy:', f"{round(accuracy_df['correct'].mean() * 100, 2)}%")<jupyter_output>2018 Tipresias Performance
Tipping points: 141
Tipping accuracy: 71.21%<jupyter_text>Save final version of the model<jupyter_code>data.train_years = (None, 2018)

bag_cv.best_estimator_.fit(*data.train_data())
bag_cv.best_estimator_.dump(os.path.join(PATH, 'server/ml_estimators/bagging_estimator/tipresias.pkl'))
benchmark = BenchmarkEstimator(name='benchmark_estimator')
benchmark.fit(*data.train_data())
benchmark.dump(os.path.join(PATH, 'server/ml_estimators/benchmark_estimator/benchmark_estimator.pkl'))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1638867339022498, 'fraction_numerical': 0.02191621411947246, 'mean_word_length': 5.519595448798989, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 15, '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': '253842', 'license': 'MIT', 'n_tokens_mistral': 1986, 'n_tokens_neox': 1912, 'n_words': 247, 'path': 'notebooks/2019_season/11.0-final-tipresias-model.ipynb', 'repo_name': 'tipresias/augury', 'size': '5156'}