Repository navigation
Expand file tree
/
Copy pathpredict_cli.py
More file actions
139 lines (120 loc) · 5.48 KB
/
Copy pathpredict_cli.py
File metadata and controls
139 lines (120 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import pandas as pd
import os
from predict_match import predict_match
def get_available_teams():
"""Return a list of available teams"""
try:
team_venue_stats = pd.read_csv('data/processed/team_venue_stats.csv')
return sorted(team_venue_stats['team'].unique())
except:
return [
"Chennai Super Kings", "Mumbai Indians", "Royal Challengers Bangalore",
"Kolkata Knight Riders", "Delhi Capitals", "Sunrisers Hyderabad",
"Punjab Kings", "Rajasthan Royals", "Gujarat Titans", "Lucknow Super Giants"
]
def get_venues():
"""Return available IPL 2024 venues only"""
try:
team_venue_stats = pd.read_csv('data/processed/team_venue_stats.csv')
# Filter for main venues only - add/remove as needed
main_venues = [
"M.A. Chidambaram Stadium, Chepauk",
"Wankhede Stadium, Mumbai",
"M. Chinnaswamy Stadium, Bengaluru",
"Eden Gardens, Kolkata",
"Arun Jaitley Stadium, Delhi",
"Rajiv Gandhi International Stadium, Uppal",
"Punjab Cricket Association Stadium, Mohali",
"Sawai Mansingh Stadium, Jaipur",
"Narendra Modi Stadium, Ahmedabad",
"Bharat Ratna Shri Atal Bihari Vajpayee Ekana Cricket Stadium, Lucknow"
]
# Only return venues in our list that exist in the data
return sorted([v for v in team_venue_stats['venue'].unique() if v in main_venues or any(m in v for m in main_venues)])
except Exception as e:
# Fallback to main venues
return [
"M.A. Chidambaram Stadium, Chepauk",
"Wankhede Stadium, Mumbai",
"M. Chinnaswamy Stadium, Bengaluru",
"Eden Gardens, Kolkata",
"Arun Jaitley Stadium, Delhi",
"Rajiv Gandhi International Stadium, Uppal",
"Punjab Cricket Association Stadium, Mohali",
"Sawai Mansingh Stadium, Jaipur",
"Narendra Modi Stadium, Ahmedabad",
"Bharat Ratna Shri Atal Bihari Vajpayee Ekana Cricket Stadium, Lucknow"
]
def print_menu(options):
"""Print a numbered menu of options"""
for i, option in enumerate(options, 1):
print(f"{i}. {option}")
def get_user_choice(prompt, options):
"""Get user choice from a list of options"""
while True:
print_menu(options)
try:
choice = int(input(prompt))
if 1 <= choice <= len(options):
return options[choice-1]
else:
print(f"Please enter a number between 1 and {len(options)}.")
except ValueError:
print("Please enter a valid number.")
def main():
"""Main function for the prediction CLI"""
print("=" * 50)
print("IPL Match Prediction System")
print("=" * 50)
# Check if model exists
if not os.path.exists('models/best_model.pkl'):
print("Error: Model files not found. Please run model_training.py first.")
return
# Get available teams and venues
teams = get_available_teams()
venues = get_venues()
# Select teams
print("\nSelect the first team:")
team1 = get_user_choice("Enter team number: ", teams)
remaining_teams = [team for team in teams if team != team1]
print("\nSelect the second team:")
team2 = get_user_choice("Enter team number: ", remaining_teams)
# Select venue
print("\nSelect the venue:")
venue = get_user_choice("Enter venue number: ", venues)
# Toss information
print("\nDo you know the toss result?")
know_toss = get_user_choice("Enter your choice: ", ["Yes", "No"])
toss_winner = None
toss_decision = None
if know_toss == "Yes":
print("\nWhich team won the toss?")
toss_winner = get_user_choice("Enter team: ", [team1, team2])
print("\nWhat did they decide?")
toss_decision = get_user_choice("Enter decision: ", ["bat", "field"])
# Make prediction
prediction = predict_match(team1, team2, venue, toss_winner, toss_decision)
if prediction:
# Display prediction
print("\n" + "=" * 50)
print("Match Prediction Results")
print("=" * 50)
print(f"Team 1: {prediction['team1']}")
print(f"Team 2: {prediction['team2']}")
print(f"Predicted Winner: {prediction['predicted_winner']}")
print(f"Win Probability: {prediction['win_probability']:.2f}%")
print(f"Confidence: {prediction['confidence']}")
print(f"\nTeam 1 Win Probability: {prediction['team1_win_probability']:.2f}%")
print(f"Team 2 Win Probability: {prediction['team2_win_probability']:.2f}%")
print("\nMatch Details:")
print(f"Venue: {prediction['match_details']['venue']}")
print(f"Team 1 is Home Team: {'Yes' if prediction['match_details']['team1_is_home'] else 'No'}")
print(f"Team 1 Recent Form: {prediction['match_details']['team1_recent_form']:.2f}")
print(f"Team 2 Recent Form: {prediction['match_details']['team2_recent_form']:.2f}")
print(f"Team 1 H2H Win Rate: {prediction['match_details']['team1_h2h_win_rate']:.2f}")
print(f"Team 2 H2H Win Rate: {prediction['match_details']['team2_h2h_win_rate']:.2f}")
if prediction['match_details']['toss_winner']:
print(f"Toss Winner: {prediction['match_details']['toss_winner']}")
print(f"Toss Decision: {prediction['match_details']['toss_decision']}")
if __name__ == "__main__":
main()