Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1"""LICENSE 

2Copyright 2020 Hermann Krumrey <hermann@krumreyh.com> 

3 

4This file is part of betbot. 

5 

6betbot is free software: you can redistribute it and/or modify 

7it under the terms of the GNU General Public License as published by 

8the Free Software Foundation, either version 3 of the License, or 

9(at your option) any later version. 

10 

11betbot is distributed in the hope that it will be useful, 

12but WITHOUT ANY WARRANTY; without even the implied warranty of 

13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

14GNU General Public License for more details. 

15 

16You should have received a copy of the GNU General Public License 

17along with betbot. If not, see <http://www.gnu.org/licenses/>. 

18LICENSE""" 

19 

20from betbot.api.Match import Match 

21from numpy import concatenate, ndarray, array 

22from sklearn.feature_extraction.text import CountVectorizer 

23from typing import Dict, Union, Optional, Tuple 

24from betbot.prediction.SKLearnPredictor import SKLearnPredictor 

25 

26 

27class NamePredictor(SKLearnPredictor): 

28 """ 

29 scikit-learn powered predictor that uses only the team names 

30 """ 

31 

32 @classmethod 

33 def name(cls) -> str: 

34 """ 

35 :return: The name of the predictor 

36 """ 

37 return "name-only" 

38 

39 # noinspection PyMethodMayBeStatic 

40 def encode_result(self, home_score: int, away_score: int) -> ndarray: 

41 """ 

42 Encodes a result vector 

43 This is done as a normalization step. 

44 :param home_score: The home score to encode 

45 :param away_score: The away score to encode 

46 :return: The encoded result vector 

47 """ 

48 encoded_home_score = 1 if home_score == 0 else 1 / (home_score + 1) 

49 encoded_away_score = 1 if away_score == 0 else 1 / (away_score + 1) 

50 return array([encoded_home_score, encoded_away_score]) 

51 

52 # noinspection PyMethodMayBeStatic 

53 def interpret_results(self, home_result: float, away_result: float) -> \ 

54 Tuple[int, int]: 

55 """ 

56 Interprets the raw results 

57 :param home_result: The home goals result 

58 :param away_result: The away goals result 

59 :return: The home goals, the away goals 

60 """ 

61 home_converted = (1 / home_result) - 1 

62 away_converted = (1 / away_result) - 1 

63 min_score = min([home_converted, away_converted]) 

64 normer = round(min_score) 

65 home_score = round(home_converted - min_score + normer) 

66 away_score = round(away_converted - min_score + normer) 

67 if home_score == away_score: 

68 if min_score == home_converted: 

69 away_score += 1 

70 else: 

71 home_score += 1 

72 

73 return home_score, away_score 

74 

75 def vectorize(self, match_data: Dict[str, Union[str, float]]) -> ndarray: 

76 """ 

77 Defines how a match is vectorized 

78 :param match_data: The match data to vectorize 

79 :return: The vector for the match 

80 """ 

81 team_vectorizer: CountVectorizer = self.model["team_vectorizer"] 

82 home, away = team_vectorizer.transform([ 

83 match_data["home_team"], match_data["away_team"] 

84 ]).toarray() 

85 return concatenate((home, away)) 

86 

87 def predict_match(self, match: Match) -> Optional[Tuple[int, int]]: 

88 """ 

89 Predicts the result of a single match using the trained model 

90 :param match: The match to predict 

91 :return: The home goals and away goals or None 

92 if no prediction took place 

93 """ 

94 match_data: Dict[str, Union[str, float]] = { 

95 "home_team": match.home_team, 

96 "away_team": match.away_team 

97 } 

98 vector = array([self.vectorize(match_data)]) 

99 results = self.model["regressor"].predict(vector)[0] 

100 return self.interpret_results(*results)