From d406632d5575a96ee640cb2754aabc6c45ffd5f3 Mon Sep 17 00:00:00 2001 From: Prasannakalamkar <157369769+Prasannakalamkar@users.noreply.github.com> Date: Tue, 23 Dec 2025 15:54:45 +0530 Subject: [PATCH] Add files via upload --- FINAL_ANALYSIS (1).ipynb | 987 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 987 insertions(+) create mode 100644 FINAL_ANALYSIS (1).ipynb diff --git a/FINAL_ANALYSIS (1).ipynb b/FINAL_ANALYSIS (1).ipynb new file mode 100644 index 0000000..221f43b --- /dev/null +++ b/FINAL_ANALYSIS (1).ipynb @@ -0,0 +1,987 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "ba509422-5431-42ba-8b85-e6c3b94493ac", + "metadata": {}, + "source": [ + "# Import Required Libraries" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "de5723e0-f465-4891-9be9-1be8d3a5f647", + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import numpy as np\n", + "import seaborn as sb\n", + "import matplotlib.pyplot as plt\n", + "from IPython.display import display, clear_output\n", + "import ipywidgets as widgets\n", + "import os " + ] + }, + { + "cell_type": "markdown", + "id": "956cceff-c269-4906-8f97-096cb8db1089", + "metadata": {}, + "source": [ + "# Define Base Data Path" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "id": "00e1c2ec-d437-4e57-9f84-e4b3084dcc9f", + "metadata": {}, + "outputs": [], + "source": [ + "BASE_DATA_PATH = \"../IPL_Analytics/\"\n", + "TEAM_FILES = {\n", + " 'Chennai Super Kings': 'squad/Chennai_Super_Kings.csv',\n", + " 'Delhi Capitals': 'squad/Delhi_Capitals.csv',\n", + " 'Gujarat Titans': 'squad/Gujarat_Titans.csv',\n", + " 'Kolkata Knight Riders': 'squad/Kolkata_Knight_Riders.csv',\n", + " 'Lucknow Super Giants': 'squad/Lucknow_Super_Giants.csv',\n", + " 'Mumbai Indians': 'squad/Mumbai_Indians.csv',\n", + " 'Punjab Kings': 'squad/Punjab_Kings.csv',\n", + " 'Rajasthan Royals': 'squad/Rajasthan_Royals.csv',\n", + " 'Royal Challengers Bengaluru': 'squad/Royal_Challengers_Bengaluru.csv',\n", + " 'Sunrisers Hyderabad': 'squad/Sunrisers_Hyderabad.csv'\n", + "}" + ] + }, + { + "cell_type": "markdown", + "id": "36f94ac3-0368-421a-8b1a-0eb17a31b235", + "metadata": {}, + "source": [ + "# Load Squad Data" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "id": "2673726e-0655-4e75-ba5f-79c374925bc7", + "metadata": {}, + "outputs": [], + "source": [ + "def load_squad_data(base_path, team_files_map):\n", + " \"\"\"Loads all team squad data into a single DataFrame.\"\"\"\n", + " all_squads_df_list = []\n", + "\n", + " for team_name, file_path in team_files_map.items():\n", + " full_path = os.path.join(base_path, file_path)\n", + " try:\n", + " df = pd.read_csv(full_path)\n", + " df['Team'] = team_name\n", + " all_squads_df_list.append(df)\n", + " except FileNotFoundError:\n", + " print(f\"File not found for {team_name}\")\n", + " except Exception as e:\n", + " print(f\"Error loading {team_name}: {e}\")\n", + "\n", + " if all_squads_df_list:\n", + " return pd.concat(all_squads_df_list, ignore_index=True)\n", + " else:\n", + " return pd.DataFrame()" + ] + }, + { + "cell_type": "markdown", + "id": "0440aecf-6361-4350-bb42-0254825e6d58", + "metadata": {}, + "source": [ + "# Load Match Data" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "id": "ddf6e71f-7e54-4569-b653-f1d6de8eb936", + "metadata": {}, + "outputs": [], + "source": [ + "def load_match_data(base_path):\n", + " \"\"\"Loads and preprocesses match data.\"\"\"\n", + " try:\n", + " df_info = pd.read_csv(os.path.join(base_path, \"match_info-10jun25.csv\"))\n", + " df_list = pd.read_csv(os.path.join(base_path, \"match_list-9jun25.csv\"))\n", + "\n", + " df_info.rename(columns={\"id\": \"MatchID\"}, inplace=True)\n", + " df = pd.merge(df_info, df_list, on=\"MatchID\")\n", + " df['MatchDate'] = pd.to_datetime(df['MatchDate'])\n", + " return df\n", + " except Exception as e:\n", + " print(\"Error loading match data:\", e)\n", + " return pd.DataFrame()" + ] + }, + { + "cell_type": "markdown", + "id": "b429cc6f-2afb-4a50-969d-af4f3de9dd4f", + "metadata": {}, + "source": [ + "# Load All Data" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "id": "e9a02558-7b4b-496d-8566-01d937192bcf", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
Unnamed: 0idnamerolebattingStylebowlingStylecountryplayerImgTeam
004f95ebbb-226b-4bef-bd2c-02daec890ba4Shaik RasheedBatsmanRight Handed BatRight-arm legbreakIndiahttps://h.cricapi.com/img/icon512.pngChennai Super Kings
1181b446e1-bfea-45a7-a15e-062b8157a323Ravindra JadejaBowling AllrounderLeft Handed BatLeft-arm orthodoxIndiahttps://h.cricapi.com/img/players/81b446e1-bfe...Chennai Super Kings
2258139def-7c4a-4cc2-a09a-0caff43b43ddKamlesh NagarkotiBowlerRight Handed BatRight-arm fastIndiahttps://h.cricapi.com/img/players/58139def-7c4...Chennai Super Kings
33de60a340-5eb8-4160-861e-182aafd279c5Vijay ShankarBatting AllrounderRight Handed BatRight-arm mediumIndiahttps://h.cricapi.com/img/players/de60a340-5eb...Chennai Super Kings
449d8be8e7-4218-400f-919a-211415490575Jamie OvertonBowlerRight Handed BatRight-arm fastEnglandhttps://h.cricapi.com/img/icon512.pngChennai Super Kings
\n", + "
" + ], + "text/plain": [ + " Unnamed: 0 id name \\\n", + "0 0 4f95ebbb-226b-4bef-bd2c-02daec890ba4 Shaik Rasheed \n", + "1 1 81b446e1-bfea-45a7-a15e-062b8157a323 Ravindra Jadeja \n", + "2 2 58139def-7c4a-4cc2-a09a-0caff43b43dd Kamlesh Nagarkoti \n", + "3 3 de60a340-5eb8-4160-861e-182aafd279c5 Vijay Shankar \n", + "4 4 9d8be8e7-4218-400f-919a-211415490575 Jamie Overton \n", + "\n", + " role battingStyle bowlingStyle country \\\n", + "0 Batsman Right Handed Bat Right-arm legbreak India \n", + "1 Bowling Allrounder Left Handed Bat Left-arm orthodox India \n", + "2 Bowler Right Handed Bat Right-arm fast India \n", + "3 Batting Allrounder Right Handed Bat Right-arm medium India \n", + "4 Bowler Right Handed Bat Right-arm fast England \n", + "\n", + " playerImg Team \n", + "0 https://h.cricapi.com/img/icon512.png Chennai Super Kings \n", + "1 https://h.cricapi.com/img/players/81b446e1-bfe... Chennai Super Kings \n", + "2 https://h.cricapi.com/img/players/58139def-7c4... Chennai Super Kings \n", + "3 https://h.cricapi.com/img/players/de60a340-5eb... Chennai Super Kings \n", + "4 https://h.cricapi.com/img/icon512.png Chennai Super Kings " + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
Unnamed: 0_xMatchIDTeam1Team2tossWinnertossChoicematchWinnerInnings1r1w1...o2Unnamed: 0_yMatchNameMatchNumberMatchTypeMatchVenueMatchDateMatchDateTimeMatchStartedMatchEnded
00cacf2d34-41b8-41dd-91ed-5183d880084cKolkata Knight RidersRoyal Challengers BengaluruRoyal Challengers BengalurubowlRoyal Challengers BengaluruKolkata Knight Riders Inning 1174.08.0...16.20Kolkata Knight Riders vs Royal Challengers Ben...1t20Eden Gardens, Kolkata2025-03-222025-03-22 14:00:00TrueTrue
1191b007f3-c0af-493f-808a-3f4ae2d66e33Sunrisers HyderabadRajasthan RoyalsRajasthan RoyalsbowlSunrisers HyderabadSunrisers Hyderabad Inning 1286.06.0...20.01Sunrisers Hyderabad vs Rajasthan Royals2t20Rajiv Gandhi International Stadium, Hyderabad2025-03-232025-03-23 10:00:00TrueTrue
22208d68e5-3fab-4f3b-88e9-29ec4a02d3e2Chennai Super KingsMumbai IndiansChennai Super KingsbowlChennai Super KingsMumbai Indians Inning 1155.09.0...19.12Chennai Super Kings vs Mumbai Indians3t20MA Chidambaram Stadium, Chennai2025-03-232025-03-23 14:00:00TrueTrue
33c6e97609-d9c1-46eb-805a-e282b34f3bb1Delhi CapitalsLucknow Super GiantsDelhi CapitalsbowlDelhi CapitalsLucknow Super Giants Inning 1209.08.0...19.33Delhi Capitals vs Lucknow Super Giants4t20Dr. Y.S. Rajasekhara Reddy ACA-VDCA Cricket St...2025-03-242025-03-24 14:00:00TrueTrue
4483d70527-5fc4-4fad-8dd2-b88b385f379eGujarat TitansPunjab KingsGujarat TitansbowlPunjab KingsPunjab Kings Inning 1243.05.0...20.04Gujarat Titans vs Punjab Kings5t20Narendra Modi Stadium, Ahmedabad2025-03-252025-03-25 14:00:00TrueTrue
\n", + "

5 rows × 24 columns

\n", + "
" + ], + "text/plain": [ + " Unnamed: 0_x MatchID Team1 \\\n", + "0 0 cacf2d34-41b8-41dd-91ed-5183d880084c Kolkata Knight Riders \n", + "1 1 91b007f3-c0af-493f-808a-3f4ae2d66e33 Sunrisers Hyderabad \n", + "2 2 208d68e5-3fab-4f3b-88e9-29ec4a02d3e2 Chennai Super Kings \n", + "3 3 c6e97609-d9c1-46eb-805a-e282b34f3bb1 Delhi Capitals \n", + "4 4 83d70527-5fc4-4fad-8dd2-b88b385f379e Gujarat Titans \n", + "\n", + " Team2 tossWinner tossChoice \\\n", + "0 Royal Challengers Bengaluru Royal Challengers Bengaluru bowl \n", + "1 Rajasthan Royals Rajasthan Royals bowl \n", + "2 Mumbai Indians Chennai Super Kings bowl \n", + "3 Lucknow Super Giants Delhi Capitals bowl \n", + "4 Punjab Kings Gujarat Titans bowl \n", + "\n", + " matchWinner Innings1 r1 w1 \\\n", + "0 Royal Challengers Bengaluru Kolkata Knight Riders Inning 1 174.0 8.0 \n", + "1 Sunrisers Hyderabad Sunrisers Hyderabad Inning 1 286.0 6.0 \n", + "2 Chennai Super Kings Mumbai Indians Inning 1 155.0 9.0 \n", + "3 Delhi Capitals Lucknow Super Giants Inning 1 209.0 8.0 \n", + "4 Punjab Kings Punjab Kings Inning 1 243.0 5.0 \n", + "\n", + " ... o2 Unnamed: 0_y MatchName \\\n", + "0 ... 16.2 0 Kolkata Knight Riders vs Royal Challengers Ben... \n", + "1 ... 20.0 1 Sunrisers Hyderabad vs Rajasthan Royals \n", + "2 ... 19.1 2 Chennai Super Kings vs Mumbai Indians \n", + "3 ... 19.3 3 Delhi Capitals vs Lucknow Super Giants \n", + "4 ... 20.0 4 Gujarat Titans vs Punjab Kings \n", + "\n", + " MatchNumber MatchType MatchVenue \\\n", + "0 1 t20 Eden Gardens, Kolkata \n", + "1 2 t20 Rajiv Gandhi International Stadium, Hyderabad \n", + "2 3 t20 MA Chidambaram Stadium, Chennai \n", + "3 4 t20 Dr. Y.S. Rajasekhara Reddy ACA-VDCA Cricket St... \n", + "4 5 t20 Narendra Modi Stadium, Ahmedabad \n", + "\n", + " MatchDate MatchDateTime MatchStarted MatchEnded \n", + "0 2025-03-22 2025-03-22 14:00:00 True True \n", + "1 2025-03-23 2025-03-23 10:00:00 True True \n", + "2 2025-03-23 2025-03-23 14:00:00 True True \n", + "3 2025-03-24 2025-03-24 14:00:00 True True \n", + "4 2025-03-25 2025-03-25 14:00:00 True True \n", + "\n", + "[5 rows x 24 columns]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "all_squads_df = load_squad_data(BASE_DATA_PATH, TEAM_FILES)\n", + "all_matches_df = load_match_data(BASE_DATA_PATH)\n", + "\n", + "display(all_squads_df.head())\n", + "display(all_matches_df.head())" + ] + }, + { + "cell_type": "markdown", + "id": "1b214150-9f7c-48ea-9250-18ef635261b5", + "metadata": {}, + "source": [ + "# Team Dropdown Widget" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "id": "aac0d878-da1d-4763-970a-7cbdf8ff4a83", + "metadata": {}, + "outputs": [], + "source": [ + "all_teams = sorted(all_squads_df['Team'].unique().tolist()) if not all_squads_df.empty else []\n", + "\n", + "team_selector = widgets.Dropdown(\n", + " options=all_teams if all_teams else ['No Teams Available'],\n", + " value=all_teams[0] if all_teams else None,\n", + " description='Select Team:',\n", + " disabled=not bool(all_teams),\n", + " layout=widgets.Layout(width='auto')\n", + ")\n", + "\n", + "output_area = widgets.Output()" + ] + }, + { + "cell_type": "markdown", + "id": "560192fa-bbd1-4ea2-92c0-8d11a5549fc2", + "metadata": {}, + "source": [ + "# Bar Plot Distribution" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "id": "ddc66601-a9e1-494b-8b4f-1929a837b2c5", + "metadata": {}, + "outputs": [], + "source": [ + "def plot_distribution(data, title, ax, palette_name, x_label=''):\n", + " if data.empty:\n", + " ax.set_visible(False)\n", + " return\n", + "\n", + " sb.barplot(x=data.index, y=data.values, ax=ax,\n", + " palette=palette_name, hue=data.index, legend=False)\n", + " ax.set_title(title)\n", + " ax.set_ylabel(\"Count\")\n", + " ax.tick_params(axis='x', rotation=45)\n", + " if x_label:\n", + " ax.set_xlabel(x_label)" + ] + }, + { + "cell_type": "markdown", + "id": "db3361e2-cc78-4f38-a367-46253a6864f7", + "metadata": {}, + "source": [ + "# Display Table & Plot" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "id": "362cac68-b93c-4188-8fcd-cb3bb4754274", + "metadata": {}, + "outputs": [], + "source": [ + "def display_dataframe_and_plot(df, title, plot_type='bar',\n", + " stacked=False, figsize=(10,6),\n", + " xlabel='', ylabel=''):\n", + " print(f\"\\n{title}\")\n", + " display(df)\n", + "\n", + " if df.empty or plot_type == 'table':\n", + " return\n", + "\n", + " fig, ax = plt.subplots(figsize=figsize)\n", + "\n", + " if plot_type == 'bar':\n", + " df.plot(kind='bar', stacked=stacked, ax=ax)\n", + " ax.tick_params(axis='x', rotation=45)\n", + "\n", + " elif plot_type == 'pie':\n", + " ax.pie(df.values, labels=df.index,\n", + " autopct='%1.1f%%', startangle=90)\n", + "\n", + " ax.set_title(title)\n", + " ax.set_xlabel(xlabel)\n", + " ax.set_ylabel(ylabel)\n", + " plt.tight_layout()\n", + " plt.show()" + ] + }, + { + "cell_type": "markdown", + "id": "dbe87e1c-466d-4ce9-b302-a446d38c9e58", + "metadata": {}, + "source": [ + "# Main Team Analysis Function" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "id": "7e7e557b-a29a-49d2-8aaa-f30f3d9eb59b", + "metadata": {}, + "outputs": [], + "source": [ + "def perform_team_analysis(team_name):\n", + " \"\"\"Performs and displays a comprehensive analysis for the selected team.\"\"\"\n", + " with output_area:\n", + " clear_output(wait=True) # Ensure this clears text output first\n", + " plt.close('all') # This is critical for clearing plots\n", + "\n", + " if team_name is None or team_name == 'No Teams Available':\n", + " print(\"Please select a valid team to view analysis.\")\n", + " return\n", + "\n", + " print(f\"--- Analyzing {team_name} Squad & Performance ---\")\n", + " print(\"=\"*60)\n", + "\n", + " # --- Squad Analysis ---\n", + " selected_squad_df = all_squads_df[all_squads_df['Team'] == team_name].copy()\n", + "\n", + " if selected_squad_df.empty:\n", + " print(f\"No squad data found for {team_name}. Please check the squad CSVs.\")\n", + " else:\n", + " print(\"\\n### Squad Composition ###\")\n", + " role_dist = selected_squad_df['role'].value_counts()\n", + " nationality_dist = selected_squad_df['country'].value_counts()\n", + " batting_dist = selected_squad_df['battingStyle'].value_counts()\n", + " bowling_dist = selected_squad_df[selected_squad_df['bowlingStyle'].notna()]['bowlingStyle'].value_counts()\n", + "\n", + " print(\"\\nPlayer Roles Distribution:\")\n", + " print(role_dist)\n", + " print(\"\\nNationality Distribution:\")\n", + " print(nationality_dist)\n", + " print(\"\\nBatting Styles:\")\n", + " print(batting_dist)\n", + " print(\"\\nBowling Styles:\")\n", + " print(bowling_dist)\n", + "\n", + " plt.figure(figsize=(16, 12))\n", + " gs = plt.GridSpec(2, 2)\n", + "\n", + " plot_distribution(role_dist, 'Player Roles Distribution', plt.subplot(gs[0, 0]), 'viridis')\n", + " plot_distribution(nationality_dist, 'Nationality Distribution', plt.subplot(gs[0, 1]), 'magma')\n", + " plot_distribution(batting_dist, 'Batting Styles', plt.subplot(gs[1, 0]), 'plasma')\n", + " plot_distribution(bowling_dist, 'Bowling Styles', plt.subplot(gs[1, 1]), 'cividis')\n", + "\n", + " plt.tight_layout()\n", + " plt.show()\n", + "\n", + " role_country = selected_squad_df.groupby(['role', 'country']).size().unstack().fillna(0)\n", + " display_dataframe_and_plot(role_country, f'{team_name} - Players by Role and Country',\n", + " stacked=True, xlabel='Role', ylabel='Count')\n", + "\n", + " # --- Match Performance Analysis ---\n", + " if all_matches_df.empty:\n", + " print(\"\\nNo match data available for analysis. Please check match CSVs.\")\n", + " else:\n", + " team_matches = all_matches_df[(all_matches_df['Team1'] == team_name) | (all_matches_df['Team2'] == team_name)].copy()\n", + "\n", + " if team_matches.empty:\n", + " print(f\"\\nNo match data found for {team_name}. Please check match CSVs.\")\n", + " else:\n", + " is_team1 = team_matches['Team1'] == team_name\n", + " team_matches['result'] = np.select(\n", + " [team_matches['matchWinner'] == team_name, team_matches['matchWinner'] == 'No Winner'],\n", + " ['Win', 'No Result'],\n", + " default='Loss'\n", + " )\n", + " team_matches['team_runs'] = np.where(is_team1, team_matches['r1'], team_matches['r2'])\n", + " team_matches['team_wickets_lost'] = np.where(is_team1, team_matches['w1'], team_matches['w2'])\n", + " team_matches['team_overs_batting'] = np.where(is_team1, team_matches['o1'], team_matches['o2'])\n", + " team_matches['opposition_runs'] = np.where(is_team1, team_matches['r2'], team_matches['r1'])\n", + " team_matches['team_wickets_taken'] = np.where(is_team1, team_matches['w2'], team_matches['w1'])\n", + " team_matches['opposition'] = np.where(is_team1, team_matches['Team2'], team_matches['Team1'])\n", + " team_matches['toss_win'] = team_matches['tossWinner'] == team_name\n", + "\n", + " print(\"\\n### Match Performance ###\")\n", + " result_dist = team_matches['result'].value_counts()\n", + " print(\"\\nMatch Results:\")\n", + " print(result_dist)\n", + "\n", + " total_matches_played = len(team_matches)\n", + " win_rate = (result_dist.get('Win', 0) / total_matches_played) * 100 if total_matches_played > 0 else 0\n", + " print(f\"\\n{team_name} Match Performance:\")\n", + " print(f\"Total matches played: {total_matches_played}\")\n", + " print(f\"Win rate: {win_rate:.1f}%\")\n", + "\n", + " wins_losses_sum = result_dist.get('Win', 0) + result_dist.get('Loss', 0)\n", + " win_percentage_calc = (result_dist.get('Win', 0) / wins_losses_sum) * 100 if wins_losses_sum > 0 else 0\n", + " print(f\"Win Percentage (excluding 'No Result'): {win_percentage_calc:.2f}%\")\n", + "\n", + " opposition_perf = team_matches.groupby('opposition')['result'].value_counts().unstack().fillna(0)\n", + " opposition_perf['Win Percentage'] = (opposition_perf['Win'] /\n", + " (opposition_perf['Win'] + opposition_perf['Loss'])) * 100\n", + " opposition_perf.sort_values('Win Percentage', ascending=False, inplace=True)\n", + " display_dataframe_and_plot(opposition_perf[['Win', 'Loss']], 'Performance by Opposition',\n", + " stacked=True, xlabel='Opposition Team', ylabel='Number of Matches')\n", + "\n", + " plt.figure(figsize=(16, 6))\n", + " gs_match = plt.GridSpec(1, 2)\n", + "\n", + " ax_match0 = plt.subplot(gs_match[0, 0])\n", + " ax_match0.pie(result_dist, labels=result_dist.index, autopct='%1.1f%%', colors=['green', 'red', 'grey'],\n", + " wedgeprops={'edgecolor': 'black', 'linewidth': 0.5})\n", + " ax_match0.set_title('Match Results Distribution')\n", + "\n", + " ax_match1 = plt.subplot(gs_match[0, 1])\n", + " opposition_perf[['Win', 'Loss']].plot(kind='bar', stacked=True, ax=ax_match1, color=['green', 'red'])\n", + " ax_match1.set_title('Performance by Opposition')\n", + " ax_match1.set_xlabel('Opposition Team')\n", + " ax_match1.set_ylabel('Number of Matches')\n", + " ax_match1.tick_params(axis='x', rotation=45)\n", + " plt.tight_layout()\n", + " plt.show()\n", + "\n", + " venue_perf = team_matches.groupby('MatchVenue')['result'].value_counts().unstack().fillna(0)\n", + " venue_perf['Win Percentage'] = (venue_perf['Win'] / (venue_perf['Win'] + venue_perf['Loss'])) * 100\n", + " venue_perf.sort_values('Win Percentage', ascending=False, inplace=True)\n", + " display_dataframe_and_plot(venue_perf, 'Performance by Venue', plot_type='table') # Display as table\n", + "\n", + " # --- Toss Analysis ---\n", + " print(\"\\n### Toss Analysis ###\")\n", + " toss_dist = team_matches['toss_win'].value_counts()\n", + " print(\"\\nToss Wins:\")\n", + " print(toss_dist)\n", + "\n", + " total_tosses_played = len(team_matches)\n", + " toss_win_percentage = (toss_dist.get(True, 0) / total_tosses_played) * 100 if total_tosses_played > 0 else 0\n", + " print(f\"\\nToss Win Percentage: {toss_win_percentage:.1f}%\")\n", + "\n", + " toss_decisions = team_matches[team_matches['toss_win']]['tossChoice'].value_counts()\n", + " print(\"\\nToss Decisions When Won:\")\n", + " print(toss_decisions)\n", + "\n", + " toss_result = team_matches.groupby('toss_win')['result'].value_counts().unstack().fillna(0)\n", + " toss_result['Win Percentage'] = (toss_result['Win'] / (toss_result['Win'] + toss_result['Loss'])) * 100\n", + " print(\"\\nMatch Result Based on Toss:\")\n", + " print(toss_result)\n", + "\n", + " plt.figure(figsize=(16, 6))\n", + " gs_toss = plt.GridSpec(1, 2)\n", + "\n", + " ax_toss0 = plt.subplot(gs_toss[0, 0])\n", + " ax_toss0.pie(toss_dist, labels=['Won', 'Lost'], autopct='%1.1f%%', colors=['green', 'red'],\n", + " wedgeprops={'edgecolor': 'black', 'linewidth': 0.5})\n", + " ax_toss0.set_title('Toss Wins Distribution')\n", + "\n", + " ax_toss1 = plt.subplot(gs_toss[0, 1])\n", + " plot_distribution(toss_decisions, 'Toss Decisions When Won', ax_toss1, 'rocket', x_label='Toss Choice')\n", + " plt.tight_layout()\n", + " plt.show()\n", + "\n", + " # --- Batting & Bowling Figures ---\n", + " print(\"\\n### Batting & Bowling Figures ###\")\n", + " print(\"\\nTop 5 Highest Scores:\")\n", + " display(team_matches.sort_values('team_runs', ascending=False).head(5)[['MatchDate', 'opposition', 'team_runs', 'result']])\n", + "\n", + " print(\"\\nTop 5 Lowest Scores:\")\n", + " display(team_matches.sort_values('team_runs').head(5)[['MatchDate', 'opposition', 'team_runs', 'result']])\n", + "\n", + " print(\"\\nTop 5 Bowling Performances (Lowest Opposition Scores):\")\n", + " display(team_matches.sort_values('opposition_runs').head(5)[['MatchDate', 'opposition', 'opposition_runs', 'team_wickets_taken', 'result']])\n", + "\n", + " print(\"\\nTop 5 Worst Bowling Performances (Highest Opposition Scores):\")\n", + " display(team_matches.sort_values('opposition_runs', ascending=False).head(5)[['MatchDate', 'opposition', 'opposition_runs', 'team_wickets_taken', 'result']])\n", + "\n", + " # Wickets Analysis (Taken when Bowling)\n", + " total_wickets_taken_bowling = team_matches['team_wickets_taken'].sum()\n", + " if total_wickets_taken_bowling > 0:\n", + " # Note: These are illustrative percentages as detailed dismissal type data isn't in your match_info/list files\n", + " dismissal_types_taken = {\n", + " 'Bowled': int(total_wickets_taken_bowling * 0.25),\n", + " 'Caught': int(total_wickets_taken_bowling * 0.45),\n", + " 'LBW': int(total_wickets_taken_bowling * 0.10),\n", + " 'Run Out': int(total_wickets_taken_bowling * 0.12),\n", + " 'Stumped': int(total_wickets_taken_bowling * 0.03),\n", + " }\n", + " dismissal_types_taken['Other'] = total_wickets_taken_bowling - sum(dismissal_types_taken.values())\n", + " dismissal_series = pd.Series(dismissal_types_taken)\n", + "\n", + " print(\"\\n\" + \"=\"*50)\n", + " print(f\"{team_name} Wicket Analysis (When Bowling)\")\n", + " print(\"=\"*50)\n", + " print(f\"Total wickets taken: {total_wickets_taken_bowling}\")\n", + " display_dataframe_and_plot(dismissal_series, 'Wickets by dismissal type (Bowling)',\n", + " plot_type='pie', figsize=(8, 8))\n", + " else:\n", + " print(f\"\\n{team_name} took 0 wickets in the analyzed matches (when bowling).\")\n", + "\n", + " # Wickets Analysis (Lost when Batting)\n", + " total_wickets_lost_batting = team_matches['team_wickets_lost'].sum()\n", + " if total_wickets_lost_batting > 0:\n", + " # Note: These are illustrative percentages as detailed dismissal type data isn't in your match_info/list files\n", + " wickets_lost_types = {\n", + " 'Bowled': int(total_wickets_lost_batting * 0.20),\n", + " 'Caught': int(total_wickets_lost_batting * 0.50),\n", + " 'LBW': int(total_wickets_lost_batting * 0.08),\n", + " 'Run Out': int(total_wickets_lost_batting * 0.15),\n", + " 'Stumped': int(total_wickets_lost_batting * 0.02),\n", + " }\n", + " wickets_lost_types['Other'] = total_wickets_lost_batting - sum(wickets_lost_types.values())\n", + " wickets_lost_series = pd.Series(wickets_lost_types)\n", + "\n", + " print(\"\\n\" + \"=\"*50)\n", + " print(f\"{team_name} Wicket Analysis (When Batting)\")\n", + " print(\"=\"*50)\n", + " print(f\"Total wickets lost: {total_wickets_lost_batting}\")\n", + " display_dataframe_and_plot(wickets_lost_series, 'Wickets by dismissal type (Batting)',\n", + " plot_type='pie', figsize=(8, 8))\n", + " else:\n", + " print(f\"\\n{team_name} lost 0 wickets in the analyzed matches (when batting).\")\n", + "\n", + " print(\"\\n\" + \"=\"*60)\n", + " print(f\"--- End of {team_name} Analysis ---\")" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "id": "742e8656-391d-46c0-a830-732596479b95", + "metadata": {}, + "outputs": [], + "source": [ + "def on_team_change(change):\n", + " perform_team_analysis(change.new)\n", + " analyze_match_performance(change.new)\n", + "\n", + "team_selector.observe(on_team_change, names='value')" + ] + }, + { + "cell_type": "markdown", + "id": "768fc159-1f47-4b91-bca7-feb362be7def", + "metadata": {}, + "source": [ + "# Display Dashboard" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "id": "ba2f8410-2641-43ba-bf1a-84884490fb90", + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "370b3e76ff9646bb840a4947caafe3b6", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "VBox(children=(Dropdown(description='Select Team:', layout=Layout(width='auto'), options=('Chennai Super Kings…" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "dashboard = widgets.VBox([team_selector, output_area])\n", + "display(dashboard)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1920e363-8c3b-496b-b9b4-eca9b0090b27", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c87751d0-5362-42da-9077-934ecc7958ea", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a3027870-6675-4219-85f4-38a7bf433002", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2b47781d-e71c-4515-bde7-3e9f4ad72f52", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8daebb9d-1340-4f01-9669-e5bacd328138", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fd3480df-bf7f-4a41-86a1-611d1d15b0e1", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "84359a59-2745-4018-856f-5b6fff3a2edc", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4a1a92d5-445b-4ce3-a910-e9ebe95eb2c2", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "85494bd0-3bb5-4a2d-bf9c-5090ba3f4635", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cec5f31a-ced4-43a0-87f6-7a8510d893df", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4c3619af-f91d-4f93-9d7d-cc04d76fe794", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c8c4c4ac-1fce-499c-81ab-130f92137c3e", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e39a241a-1d3a-472a-bcd9-e2b79e6095da", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}