CoolFace
Apppublic

Chittrarasu/Cricket-match-prediction-FastAPI

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
train_model.py247 linesDownload Raw Back to models
1import pandas as pd2import numpy as np3from sklearn.ensemble import RandomForestClassifier, HistGradientBoostingRegressor4from sklearn.multioutput import MultiOutputRegressor5from sklearn.model_selection import train_test_split6from sklearn.metrics import mean_squared_error, accuracy_score, r2_score7from sklearn.preprocessing import StandardScaler8 9# Load and preprocess data (same as original)10def load_and_preprocess_data():11    # Load datasets with exact column names12    ball_df = pd.read_csv('data/cleaned_ball_data.csv', 13                          dtype={14                              'match_id': str, 'season': str, 'start_date': str, 'venue': str,15                              'innings': int, 'ball': float, 'batting_team': str, 'bowling_team': str,16                              'striker': str, 'non_striker': str, 'bowler': str, 'runs_off_bat': int,17                              'extras': int, 'wides': float, 'noballs': float, 'byes': float,18                              'legbyes': float, 'penalty': float, 'wicket_type': str,19                              'player_dismissed': str, 'other_wicket_type': str,20                              'other_player_dismissed': str, 'cricsheet_id': str, 'total_runs': int21                          })22    match_df = pd.read_csv('data/cleaned_match_data.csv', 23                           dtype={24                               'id': str, 'season': str, 'city': str, 'date': str,25                               'team1': str, 'team2': str, 'toss_winner': str, 'toss_decision': str,26                               'result': str, 'dl_applied': int, 'winner': str,27                               'win_by_runs': float, 'win_by_wickets': float, 'player_of_match': str,28                               'venue': str, 'umpire1': str, 'umpire2': str, 'umpire3': str29                           })30 31    # Convert date columns to datetime32    match_df['date'] = pd.to_datetime(match_df['date'], errors='coerce')33    ball_df['start_date'] = pd.to_datetime(ball_df['start_date'], errors='coerce')34 35    # Filter for ODI matches36    odi_date_mask = (match_df['date'].dt.year >= 2015) & (match_df['date'].dt.year <= 2022)37    match_df = match_df[odi_date_mask].copy()38 39    # Compute team total scores40    team_scores = ball_df.groupby(['match_id', 'batting_team'])['total_runs'].sum().reset_index()41    team_scores.rename(columns={'total_runs': 'team_total'}, inplace=True)42 43    # Merge scores into match_df44    match_df = match_df.merge(team_scores, left_on=['id', 'team1'], right_on=['match_id', 'batting_team'], how='left')45    match_df.rename(columns={'team_total': 'team1_total'}, inplace=True)46    match_df['team1_total'] = match_df['team1_total'].fillna(match_df['team1_total'].mean())47    match_df = match_df.merge(team_scores, left_on=['id', 'team2'], right_on=['match_id', 'batting_team'], how='left')48    match_df.rename(columns={'team_total': 'team2_total'}, inplace=True)49    match_df['team2_total'] = match_df['team2_total'].fillna(match_df['team2_total'].mean())50    match_df.drop(columns=['batting_team', 'match_id'], errors='ignore', inplace=True)51 52    # Add venue and city indices53    match_df['venue_index'] = match_df['venue'].astype('category').cat.codes54    match_df['city_index'] = match_df['city'].astype('category').cat.codes55 56    # Add toss features57    match_df['toss_winner_index'] = match_df['toss_winner'].astype('category').cat.codes58    match_df['toss_decision_index'] = match_df['toss_decision'].map({'bat': 1, 'field': 0}).fillna(0).astype(int)59 60    # Compute historical win rates61    match_df['date_numeric'] = (match_df['date'] - pd.Timestamp("1970-01-01")) // pd.Timedelta('1d')62    max_date = match_df['date_numeric'].max()63    team1_wins = match_df[match_df['winner'] == match_df['team1']].groupby('team1').agg({'date_numeric': 'mean', 'id': 'count'}).reset_index()64    team1_wins.rename(columns={'id': 'wins', 'date_numeric': 'win_date', 'team1': 'team'}, inplace=True)65    team2_wins = match_df[match_df['winner'] == match_df['team2']].groupby('team2').agg({'date_numeric': 'mean', 'id': 'count'}).reset_index()66    team2_wins.rename(columns={'id': 'wins', 'date_numeric': 'win_date', 'team2': 'team'}, inplace=True)67    team_wins = pd.concat([team1_wins, team2_wins]).groupby('team').agg({'wins': 'sum', 'win_date': 'mean'}).reset_index()68    team1_matches = match_df.groupby('team1').size().reset_index(name='matches')69    team1_matches.rename(columns={'team1': 'team'}, inplace=True)70    team2_matches = match_df.groupby('team2').size().reset_index(name='matches')71    team2_matches.rename(columns={'team2': 'team'}, inplace=True)72    team_matches = pd.concat([team1_matches, team2_matches]).groupby('team')['matches'].sum().reset_index()73    team_win_rates = team_matches.merge(team_wins, on='team', how='left').fillna(0)74    team_win_rates['weighted_wins'] = team_win_rates.apply(lambda x: x['wins'] * np.exp(-0.1 * (max_date - x['win_date']) / 365) if pd.notna(x['win_date']) else 0, axis=1)75    team_win_rates['win_rate'] = team_win_rates['weighted_wins'] / team_win_rates['matches']76    team_win_rates['win_rate'] = team_win_rates['win_rate'].fillna(0)77    match_df = match_df.merge(team_win_rates[['team', 'win_rate']].rename(columns={'team': 'team1', 'win_rate': 'team1_win_rate'}), on='team1', how='left')78    match_df = match_df.merge(team_win_rates[['team', 'win_rate']].rename(columns={'team': 'team2', 'win_rate': 'team2_win_rate'}), on='team2', how='left')79 80    # Compute head-to-head win rates81    head_to_head = match_df[match_df['team1'].isin(match_df['team1'].unique()) & match_df['team2'].isin(match_df['team2'].unique())]82    head_to_head_wins = head_to_head[head_to_head['winner'] == head_to_head['team1']].groupby(['team1', 'team2']).size().reset_index(name='h2h_wins')83    head_to_head_matches = head_to_head.groupby(['team1', 'team2']).size().reset_index(name='h2h_matches')84    h2h_win_rates = head_to_head_matches.merge(head_to_head_wins, on=['team1', 'team2'], how='left').fillna(0)85    h2h_win_rates = h2h_win_rates[head_to_head_matches['h2h_matches'] >= 1]86    h2h_win_rates['h2h_win_rate'] = h2h_win_rates['h2h_wins'] / h2h_win_rates['h2h_matches']87    match_df = match_df.merge(h2h_win_rates[['team1', 'team2', 'h2h_win_rate']], on=['team1', 'team2'], how='left').fillna(0)88 89    # Cap outliers90    match_df['team1_total'] = match_df['team1_total'].clip(upper=500)91    match_df['team2_total'] = match_df['team2_total'].clip(upper=500)92 93    return match_df, ball_df94 95# Train team performance model and return it96def train_team_performance_model(match_df):97    data = match_df[['team1', 'team2', 'winner', 'team1_total', 'team2_total', 'venue_index', 'city_index', 98                     'toss_winner_index', 'toss_decision_index', 'dl_applied', 'team1_win_rate', 99                     'team2_win_rate', 'h2h_win_rate']].dropna()100 101    # Convert categorical teams to numerical indices102    data['team1_index'] = data['team1'].astype('category').cat.codes103    data['team2_index'] = data['team2'].astype('category').cat.codes104    data['winner_index'] = (data['winner'] == data['team1']).astype(int)105 106    # Features and targets107    X = pd.DataFrame()108    X['team1_index'] = data['team1_index']109    X['team2_index'] = data['team2_index']110    X['venue_index'] = data['venue_index']111    X['city_index'] = data['city_index']112    X['toss_winner_index'] = data['toss_winner_index']113    X['toss_decision_index'] = data['toss_decision_index']114    X['dl_applied'] = data['dl_applied']115    X['team1_win_rate'] = data['team1_win_rate']116    X['team2_win_rate'] = data['team2_win_rate']117    X['h2h_win_rate'] = data['h2h_win_rate'] * 2118 119    y_win = data['winner_index']120    y_score = data[['team1_total', 'team2_total']]121 122    # Scale features123    scaler = StandardScaler()124    scaled_features = scaler.fit_transform(X[['venue_index', 'city_index', 'toss_winner_index', 'toss_decision_index', 125                                             'dl_applied', 'team1_win_rate', 'team2_win_rate', 'h2h_win_rate']])126    X_scaled = pd.DataFrame(scaled_features, columns=['venue_index', 'city_index', 'toss_winner_index', 'toss_decision_index', 127                                                     'dl_applied', 'team1_win_rate', 'team2_win_rate', 'h2h_win_rate'])128    X_scaled['team1_index'] = X['team1_index']129    X_scaled['team2_index'] = X['team2_index']130 131    # Train/test split132    X_train, X_test, y_train, y_test = train_test_split(X_scaled, y_win, test_size=0.2, random_state=42)133 134    # Train win model135    win_model = RandomForestClassifier(n_estimators=200, max_depth=15, random_state=42, class_weight='balanced')136    win_model.fit(X_train, y_train)137 138    # Train score model139    base_score_model = HistGradientBoostingRegressor(random_state=42, learning_rate=0.1, max_iter=100)140    score_model = MultiOutputRegressor(base_score_model)141    score_model.fit(X_scaled, y_score)142 143    return win_model, score_model, data, scaler144 145# Train player score model and return it146def train_player_score_model(match_df, ball_df):147    player_runs = ball_df.groupby(['match_id', 'striker', 'batting_team'])['runs_off_bat'].sum().reset_index()148    player_runs.rename(columns={'runs_off_bat': 'player_total'}, inplace=True)149    player_data = player_runs.merge(match_df, left_on='match_id', right_on='id', how='left')150 151    # Feature engineering152    player_data['player_avg'] = player_data.groupby('striker')['player_total'].transform('mean')153    player_data['team_win_rate'] = player_data.apply(lambda x: player_data[player_data['team1'] == x['batting_team']]['team1_win_rate'].mean() 154                                                    if x['batting_team'] == x['team1'] else player_data[player_data['team2'] == x['batting_team']]['team2_win_rate'].mean(), axis=1)155    player_data['venue_index'] = player_data['venue'].astype('category').cat.codes156    player_data['city_index'] = player_data['city'].astype('category').cat.codes157    player_data['toss_winner_index'] = player_data['toss_winner'].astype('category').cat.codes158    player_data['toss_decision_index'] = player_data['toss_decision'].map({'bat': 1, 'field': 0}).fillna(0).astype(int)159 160    # Features and target161    X = player_data[['player_avg', 'team_win_rate', 'venue_index', 'city_index', 'toss_winner_index', 'toss_decision_index']].dropna()162    y = player_data.loc[X.index, 'player_total']163 164    # Scale features165    scaler = StandardScaler()166    X_scaled = scaler.fit_transform(X)167 168    # Train model169    score_model = HistGradientBoostingRegressor(random_state=42, learning_rate=0.1, max_iter=100)170    score_model.fit(X_scaled, y)171 172    return score_model, scaler, player_data173 174# Prediction functions (unchanged except removing joblib.load)175def predict_player_score(player, team, opponent, venue=None, city=None, toss_winner=None, toss_decision=None, 176                        score_model=None, scaler=None, player_data=None):177    try:178        if player not in player_data['striker'].values or team not in player_data['batting_team'].values:179            raise ValueError("Player or team not found in training data")180 181        player_avg = player_data[player_data['striker'] == player]['player_total'].mean()182        team_win_rate = player_data[player_data['batting_team'] == team]['team_win_rate'].mean()183        venue_index = player_data[player_data['venue'] == venue]['venue_index'].values[0] if venue else player_data['venue_index'].mean()184        city_index = player_data[player_data['city'] == city]['city_index'].values[0] if city else player_data['city_index'].mean()185        toss_winner_index = player_data[player_data['toss_winner'] == toss_winner]['toss_winner_index'].values[0] if toss_winner else player_data['toss_winner_index'].mean()186        toss_decision_index = 1 if toss_decision == 'bat' else 0 if toss_decision == 'field' else player_data['toss_decision_index'].mean()187 188        features = scaler.transform([[player_avg, team_win_rate, venue_index, city_index, toss_winner_index, toss_decision_index]])189        predicted_score = score_model.predict(features)[0]190 191        return {192            "player": player,193            "team": team,194            "opponent": opponent,195            "expected_score": round(predicted_score, 2)196        }197    except Exception as e:198        print(f"Prediction error: {str(e)}")199        return {200            "player": player,201            "team": team,202            "opponent": opponent,203            "expected_score": 0.0204        }205 206def predict_team_performance(team1, team2, venue=None, city=None, toss_winner=None, toss_decision=None,207                             win_model=None, score_model=None, data=None, scaler=None):208    try:209        if team1 not in data['team1'].values or team2 not in data['team2'].values:210            raise ValueError("Team not found in training data")211 212        team1_index = data[data['team1'] == team1]['team1_index'].values[0]213        team2_index = data[data['team2'] == team2]['team2_index'].values[0]214        venue_index = data[data['venue'] == venue]['venue_index'].values[0] if venue else data['venue_index'].mean()215        city_index = data[data['city'] == city]['city_index'].values[0] if city else data['city_index'].mean()216        toss_winner_index = data[data['toss_winner'] == toss_winner]['toss_winner_index'].values[0] if toss_winner else data['toss_winner_index'].mean()217        toss_decision_index = 1 if toss_decision == 'bat' else 0 if toss_decision == 'field' else data['toss_decision_index'].mean()218        dl_applied = 0 if pd.isna(toss_decision) else data['dl_applied'].mean()219        team1_win_rate = data[data['team1'] == team1]['team1_win_rate'].values[0]220        team2_win_rate = data[data['team2'] == team2]['team2_win_rate'].values[0]221        h2h_win_rate = data[(data['team1'] == team1) & (data['team2'] == team2)]['h2h_win_rate'].values[0] if not data[(data['team1'] == team1) & (data['team2'] == team2)].empty else 0222 223        features = scaler.transform([[venue_index, city_index, toss_winner_index, toss_decision_index, dl_applied, 224                                     team1_win_rate, team2_win_rate, h2h_win_rate]])225        win_probability = win_model.predict_proba([[team1_index, team2_index, features[0][0], features[0][1], 226                                                   features[0][2], features[0][3], features[0][4], features[0][5], 227                                                   features[0][6], features[0][7]]])[:, 1][0] * 100228        predicted_scores = score_model.predict([[team1_index, team2_index, features[0][0], features[0][1], 229                                                features[0][2], features[0][3], features[0][4], features[0][5], 230                                                features[0][6], features[0][7]]])[0]231 232        return {233            "team1": team1,234            "team2": team2,235            "win_probability_team1": round(win_probability, 2),236            "expected_team1_score": round(predicted_scores[0], 2),237            "expected_team2_score": round(predicted_scores[1], 2)238        }239    except Exception as e:240        print(f"Prediction error: {str(e)}")241        return {242            "team1": team1,243            "team2": team2,244            "win_probability_team1": 50.0,245            "expected_team1_score": 0.0,246            "expected_team2_score": 0.0247        }