{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import time\n", "import requests\n", "import pandas as pd\n", "import seaborn as sns\n", "import matplotlib.pyplot as plt\n", "from matplotlib.pyplot import figure\n", "from matplotlib.offsetbox import OffsetImage, AnnotationBbox\n", "from scipy import stats\n", "import matplotlib.lines as mlines\n", "import matplotlib.transforms as mtransforms\n", "import numpy as np\n", "import time\n", "#import plotly.express as px\n", "#!pip install chart_studio\n", "#import chart_studio.tools as tls\n", "from bs4 import BeautifulSoup\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import matplotlib.font_manager as font_manager\n", "from datetime import datetime\n", "import pytz\n", "from matplotlib.ticker import MaxNLocator\n", "from matplotlib.patches import Ellipse\n", "import matplotlib.transforms as transforms\n", "from matplotlib.gridspec import GridSpec\n", "from datasets import load_dataset" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Starting Everything:\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Found cached dataset csv (C:/Users/thoma/.cache/huggingface/datasets/nesticot___csv/nesticot--mlb_data-a391519415fcbccf/0.0.0/6954658bab30a358235fa864b05cf819af0e179325c740e4bc853bcc7ec513e1)\n", "100%|██████████| 1/1 [00:00<00:00, 2.02it/s]\n" ] } ], "source": [ "\n", "colour_palette = ['#FFB000','#648FFF','#785EF0',\n", " '#DC267F','#FE6100','#3D1EB2','#894D80','#16AA02','#B5592B','#A3C1ED']\n", "\n", "print('Starting Everything:')\n", "# exit_velo_df = milb_a_ev_df.append([triple_a_ev_df,double_a_ev_df,a_high_a_ev_df,single_a_ev_df]).reset_index(drop=True)\n", "# player_df_all = mlb_a_player_df.append([triple_a_player_df,double_a_player_df,a_high_a_player_df,single_a_player_df]).reset_index(drop=True)\n", "# exit_velo_df = pd.read_csv('exit_velo_df_all.csv',index_col=[0])\n", "# player_df_all = pd.read_csv('player_df_all.csv',index_col=[0])\n", "\n", "# pa_df = pd.read_csv('pa_df_all.csv',index_col=[0])\n", "# pa_df_full_na = pa_df.dropna()\n", "\n", "### Import Datasets\n", "dataset = load_dataset('nesticot/mlb_data', data_files=['a_pitch_data_2023.csv',\n", " ])\n", "dataset_train = dataset['train']\n", "exit_velo_df = dataset_train.to_pandas().set_index(list(dataset_train.features.keys())[0]).reset_index(drop=True)\n", "colour_palette = ['#FFB000','#648FFF','#785EF0',\n", " '#DC267F','#FE6100','#3D1EB2','#894D80','#16AA02','#B5592B','#A3C1ED']\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 True\n", "1 True\n", "2 True\n", "3 True\n", "4 True\n", " ... \n", "575260 True\n", "575261 True\n", "575262 True\n", "575263 True\n", "575264 True\n", "Name: is_pitch, Length: 575265, dtype: bool" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "exit_velo_df['is_pitch']" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "tl_df = exit_velo_df[exit_velo_df['batter_id'] == 699073].groupby(['batter_id','batter_name','batter_hand']).agg(\n", " pitches = ('is_pitch','sum'),\n", " swings = ('is_swing','sum'),\n", " whiffs = ('is_whiff','sum')\n", ")" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "tl_df['whiff_rate'] = tl_df['whiffs'] / tl_df['swings']" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | \n", " | \n", " | pitches | \n", "swings | \n", "whiffs | \n", "whiff_rate | \n", "
---|---|---|---|---|---|---|
batter_id | \n", "batter_name | \n", "batter_hand | \n", "\n", " | \n", " | \n", " | \n", " |
699073 | \n", "Thayron Liranzo | \n", "L | \n", "1344 | \n", "554 | \n", "189 | \n", "0.341155 | \n", "
R | \n", "343 | \n", "160 | \n", "60 | \n", "0.375 | \n", "