def __make_predictions(self, year_range: Tuple[int, int]) -> None: predictions = self.data_importer.fetch_prediction_data( year_range, verbose=self.verbose ) home_away_df = pivot_team_matches_to_matches(predictions) for pred in home_away_df.to_dict("records"): Prediction.update_or_create_from_data(pred) if self.verbose == 1: print("\nPredictions saved!")
def __make_predictions(self, year: int, round_number: int) -> None: predictions = self.data_importer.fetch_prediction_data( (year, year + 1), round_number=round_number, ml_models=self.ml_models ) home_away_df = pivot_team_matches_to_matches(predictions) for pred in home_away_df.to_dict("records"): Prediction.update_or_create_from_data(pred) if self.verbose == 1: print("Predictions saved!\n")
def test_convert_data_to_record(self): data = fake_prediction_data(self.match, ml_model_name=self.ml_model.name) home_away_df = pivot_team_matches_to_matches(pd.DataFrame(data)) self.assertEqual(Prediction.objects.count(), 0) Prediction.update_or_create_from_data(home_away_df.to_dict("records")[0]) self.assertEqual(Prediction.objects.count(), 1) with self.subTest("when prediction record already exists"): predicted_margin = 100 home_away_df.loc[:, "home_predicted_margin"] = predicted_margin home_away_df.loc[:, "away_predicted_margin"] = -predicted_margin Prediction.update_or_create_from_data(home_away_df.to_dict("records")[0]) self.assertEqual(Prediction.objects.count(), 1) prediction = Prediction.objects.first() self.assertEqual(prediction.predicted_margin, predicted_margin) # Regression tests for bug that caused update_or_create_from_data # to select wrong team as predicted_winner when predicted margin # was greater than away team's predicted winning margin with self.subTest( "when predicted margins are skewed with large home losing margin" ): predicted_winning_margin = 100 predicted_losing_margin = -200 home_away_df.loc[:, "home_predicted_margin"] = predicted_losing_margin home_away_df.loc[:, "away_predicted_margin"] = predicted_winning_margin Prediction.update_or_create_from_data(home_away_df.to_dict("records")[0]) prediction = Prediction.objects.first() self.assertEqual(prediction.predicted_margin, 150) self.assertEqual( home_away_df["away_team"].iloc[0], prediction.predicted_winner.name ) with self.subTest( "when predicted margins are skewed with large away losing margin" ): predicted_winning_margin = 100 predicted_losing_margin = -200 home_away_df.loc[:, "home_predicted_margin"] = predicted_winning_margin home_away_df.loc[:, "away_predicted_margin"] = predicted_losing_margin Prediction.update_or_create_from_data(home_away_df.to_dict("records")[0]) prediction = Prediction.objects.first() self.assertEqual(prediction.predicted_margin, 150) self.assertEqual( home_away_df["home_team"].iloc[0], prediction.predicted_winner.name ) with self.subTest("when predicted margins are less than 0.5"): predicted_winning_margin = 0.4 predicted_losing_margin = -0.4 home_away_df.loc[:, "home_predicted_margin"] = predicted_winning_margin home_away_df.loc[:, "away_predicted_margin"] = predicted_losing_margin Prediction.update_or_create_from_data(home_away_df.to_dict("records")[0]) prediction = Prediction.objects.first() self.assertEqual(prediction.predicted_margin, 1) self.assertEqual( home_away_df["home_team"].iloc[0], prediction.predicted_winner.name )