{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nfrom matplotlib.dates import DateFormatter\nimport matplotlib.dates as mdates\npd.options.display.max_columns = 999","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-20T06:47:49.922176Z","iopub.execute_input":"2021-06-20T06:47:49.92284Z","iopub.status.idle":"2021-06-20T06:47:49.93249Z","shell.execute_reply.started":"2021-06-20T06:47:49.922724Z","shell.execute_reply":"2021-06-20T06:47:49.931552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv('/kaggle/input/mlb-player-digital-engagement-data-exploration/out.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-20T06:47:49.934083Z","iopub.execute_input":"2021-06-20T06:47:49.934574Z","iopub.status.idle":"2021-06-20T06:48:08.403937Z","shell.execute_reply.started":"2021-06-20T06:47:49.934509Z","shell.execute_reply":"2021-06-20T06:48:08.403093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['dailyDataDate'] = pd.to_datetime(data['dailyDataDate'])","metadata":{"execution":{"iopub.status.busy":"2021-06-20T06:48:08.405325Z","iopub.execute_input":"2021-06-20T06:48:08.405803Z","iopub.status.idle":"2021-06-20T06:48:08.895386Z","shell.execute_reply.started":"2021-06-20T06:48:08.405753Z","shell.execute_reply":"2021-06-20T06:48:08.894501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"MLB digital engagement competition aims to predict four different engagement metrics and help to see how different factors affect fan engagement in baseball. Since the metrics are base on real life performnace and news it is hard to truly anonymize the data. Here we will look at a few selected players.","metadata":{}},{"cell_type":"markdown","source":"First we would look at players that are retired or no longer playing in MLB. One of example is future hall of famer Ichiro Suzuki who retired in 2019 season. Here is a plot of target metrics:","metadata":{}},{"cell_type":"code","source":"player_data = data[data.playerId == 400085] #Ichiro Suzuki\n\nfig1, f1_axes = plt.subplots(ncols=2, nrows=2, constrained_layout=True,figsize=(20, 8))\n\nplt.suptitle('Target metric of Ichiro Suzuki')\n\nf1_axes[0,0].plot(player_data['dailyDataDate'],player_data['target1'])\nf1_axes[0,0].xaxis.set_major_locator(mdates.WeekdayLocator(interval=13))\nf1_axes[0,0].xaxis.set_major_formatter(DateFormatter(\"%y-%m-%d\"))\nf1_axes[0,0].set_title('Target metric 1')\n\n\nf1_axes[0,1].plot(player_data['dailyDataDate'],player_data['target2'])\nf1_axes[0,1].xaxis.set_major_locator(mdates.WeekdayLocator(interval=13))\nf1_axes[0,1].xaxis.set_major_formatter(DateFormatter(\"%y-%m-%d\"))\nf1_axes[0,1].set_title('Target metric 2')\n\nf1_axes[1,0].plot(player_data['dailyDataDate'],player_data['target3'])\nf1_axes[1,0].xaxis.set_major_locator(mdates.WeekdayLocator(interval=13))\nf1_axes[1,0].xaxis.set_major_formatter(DateFormatter(\"%y-%m-%d\"))\nf1_axes[1,0].set_title('Target metric 3')\n\nf1_axes[1,1].plot(player_data['dailyDataDate'],player_data['target4'])\nf1_axes[1,1].xaxis.set_major_locator(mdates.WeekdayLocator(interval=13))\nf1_axes[1,1].xaxis.set_major_formatter(DateFormatter(\"%y-%m-%d\"))\nf1_axes[1,1].set_title('Target metric 4')\n\n","metadata":{"execution":{"iopub.status.busy":"2021-06-17T06:58:00.957187Z","iopub.execute_input":"2021-06-17T06:58:00.957784Z","iopub.status.idle":"2021-06-17T06:58:02.00312Z","shell.execute_reply.started":"2021-06-17T06:58:00.957747Z","shell.execute_reply":"2021-06-17T06:58:02.002053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"From the figures only target metric 4 shows non-zero value after his retirement which may imply target metric 4 is some sort of social media engagement and independnet of players' active status, and target 1 and 3 happens sparsely while targe 2 and 4 are more continous. Also we can checked Masahiro Tanaka who was playing for New York Yankees but played for Rakuten Golden Eagles in NPB in 2021 season.","metadata":{}},{"cell_type":"code","source":"player_data = data[data.playerId == 547888] #Masahiro Tanaka\n\n\nfig1, f1_axes = plt.subplots(ncols=2, nrows=2, constrained_layout=True,figsize=(20, 8))\n\nplt.suptitle('Target metric of Masahiro Tanaka')\n\n\nf1_axes[0,0].plot(player_data['dailyDataDate'],player_data['target1'])\nf1_axes[0,0].xaxis.set_major_locator(mdates.WeekdayLocator(interval=13))\nf1_axes[0,0].xaxis.set_major_formatter(DateFormatter(\"%y-%m-%d\"))\nf1_axes[0,0].set_title('Target metric 1')\n\n\nf1_axes[0,1].plot(player_data['dailyDataDate'],player_data['target2'])\nf1_axes[0,1].xaxis.set_major_locator(mdates.WeekdayLocator(interval=13))\nf1_axes[0,1].xaxis.set_major_formatter(DateFormatter(\"%y-%m-%d\"))\nf1_axes[0,1].set_title('Target metric 2')\n\nf1_axes[1,0].plot(player_data['dailyDataDate'],player_data['target3'])\nf1_axes[1,0].xaxis.set_major_locator(mdates.WeekdayLocator(interval=13))\nf1_axes[1,0].xaxis.set_major_formatter(DateFormatter(\"%y-%m-%d\"))\nf1_axes[1,0].set_title('Target metric 3')\n\nf1_axes[1,1].plot(player_data['dailyDataDate'],player_data['target4'])\nf1_axes[1,1].xaxis.set_major_locator(mdates.WeekdayLocator(interval=13))\nf1_axes[1,1].xaxis.set_major_formatter(DateFormatter(\"%y-%m-%d\"))\nf1_axes[1,1].set_title('Target metric 4')\n\n","metadata":{"execution":{"iopub.status.busy":"2021-06-20T07:27:47.274707Z","iopub.execute_input":"2021-06-20T07:27:47.275095Z","iopub.status.idle":"2021-06-20T07:27:48.577788Z","shell.execute_reply.started":"2021-06-20T07:27:47.275064Z","shell.execute_reply":"2021-06-20T07:27:48.576683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Similar trend are shown as well after he went to played for NPB in 2021 season. After checking playerse no longer play in MLB, the next step is to loop players who are new to the league. Two players are selected: Yoshi Tsutsugo who first played in MLB in 2020 season without playing in US before, and Yermín Mercedes who played in the minor league in US before MLB.","metadata":{}},{"cell_type":"code","source":"player_data = data[data.playerId == 660294] #Yoshi Tsutsugo\n\n\nfig1, f1_axes = plt.subplots(ncols=2, nrows=2, constrained_layout=True,figsize=(20, 8))\n\nplt.suptitle('Target metric of Yoshi Tsutsugo')\n\n\nf1_axes[0,0].plot(player_data['dailyDataDate'],player_data['target1'])\nf1_axes[0,0].xaxis.set_major_locator(mdates.WeekdayLocator(interval=13))\nf1_axes[0,0].xaxis.set_major_formatter(DateFormatter(\"%y-%m-%d\"))\nf1_axes[0,0].set_title('Target metric 1')\n\n\nf1_axes[0,1].plot(player_data['dailyDataDate'],player_data['target2'])\nf1_axes[0,1].xaxis.set_major_locator(mdates.WeekdayLocator(interval=13))\nf1_axes[0,1].xaxis.set_major_formatter(DateFormatter(\"%y-%m-%d\"))\nf1_axes[0,1].set_title('Target metric 2')\n\nf1_axes[1,0].plot(player_data['dailyDataDate'],player_data['target3'])\nf1_axes[1,0].xaxis.set_major_locator(mdates.WeekdayLocator(interval=13))\nf1_axes[1,0].xaxis.set_major_formatter(DateFormatter(\"%y-%m-%d\"))\nf1_axes[1,0].set_title('Target metric 3')\n\nf1_axes[1,1].plot(player_data['dailyDataDate'],player_data['target4'])\nf1_axes[1,1].xaxis.set_major_locator(mdates.WeekdayLocator(interval=13))\nf1_axes[1,1].xaxis.set_major_formatter(DateFormatter(\"%y-%m-%d\"))\nf1_axes[1,1].set_title('Target metric 4')\n","metadata":{"execution":{"iopub.status.busy":"2021-06-17T06:57:54.485379Z","iopub.execute_input":"2021-06-17T06:57:54.485829Z","iopub.status.idle":"2021-06-17T06:57:55.486569Z","shell.execute_reply.started":"2021-06-17T06:57:54.485788Z","shell.execute_reply":"2021-06-17T06:57:55.485787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"player_data = data[data.playerId == 606213] #Yermín Mercedes\n\n\nfig1, f1_axes = plt.subplots(ncols=2, nrows=2, constrained_layout=True,figsize=(20, 8))\n\nplt.suptitle('Target metric of Yermín Mercedes')\n\nf1_axes[0,0].plot(player_data['dailyDataDate'],player_data['target1'])\nf1_axes[0,0].xaxis.set_major_locator(mdates.WeekdayLocator(interval=13))\nf1_axes[0,0].xaxis.set_major_formatter(DateFormatter(\"%y-%m-%d\"))\nf1_axes[0,0].set_title('Target metric 1')\n\n\nf1_axes[0,1].plot(player_data['dailyDataDate'],player_data['target2'])\nf1_axes[0,1].xaxis.set_major_locator(mdates.WeekdayLocator(interval=13))\nf1_axes[0,1].xaxis.set_major_formatter(DateFormatter(\"%y-%m-%d\"))\nf1_axes[0,1].set_title('Target metric 2')\n\nf1_axes[1,0].plot(player_data['dailyDataDate'],player_data['target3'])\nf1_axes[1,0].xaxis.set_major_locator(mdates.WeekdayLocator(interval=13))\nf1_axes[1,0].xaxis.set_major_formatter(DateFormatter(\"%y-%m-%d\"))\nf1_axes[1,0].set_title('Target metric 3')\n\nf1_axes[1,1].plot(player_data['dailyDataDate'],player_data['target4'])\nf1_axes[1,1].xaxis.set_major_locator(mdates.WeekdayLocator(interval=13))\nf1_axes[1,1].xaxis.set_major_formatter(DateFormatter(\"%y-%m-%d\"))\nf1_axes[1,1].set_title('Target metric 4')\n","metadata":{"execution":{"iopub.status.busy":"2021-06-17T06:57:51.146973Z","iopub.execute_input":"2021-06-17T06:57:51.147317Z","iopub.status.idle":"2021-06-17T06:57:52.208955Z","shell.execute_reply.started":"2021-06-17T06:57:51.147287Z","shell.execute_reply":"2021-06-17T06:57:52.20789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"They have all zero for target metrics except metric 2 where Yermín Mercedes has non-zero in season 2019 when he was still playing in minor league, which means metric 2 can be non-zero even there is no game played in the data","metadata":{}},{"cell_type":"markdown","source":"After setting baseline it's the fun stuff where we allign baseball news with metric, in particular hopefully finding some correlation between setiment and metrics. First we have Joe Kelly and the date July 29, 2020, it's the first series with Houston Astros after the sign stealing scandal when Astros illegally use electronic device and banging trash can to rally catcher's sign, hopefully to gain an edge. Joe Kelly was subsequently suspened 8 games by MLB for throwing at opponent batter, but for fans it was saw as an heroic move. Let see how the metric said","metadata":{}},{"cell_type":"code","source":"player_data = data[data.playerId == 523260] #Joe Kelly\n\n\nfig1, f1_axes = plt.subplots(ncols=2, nrows=2, constrained_layout=True,figsize=(20, 8))\n\nplt.suptitle('Target metric of Joe Kelly')\n\nf1_axes[0,0].plot(player_data['dailyDataDate'],player_data['target1'])\nf1_axes[0,0].xaxis.set_major_locator(mdates.WeekdayLocator(interval=13))\nf1_axes[0,0].xaxis.set_major_formatter(DateFormatter(\"%y-%m-%d\"))\nf1_axes[0,0].set_title('Target metric 1')\n\n\nf1_axes[0,1].plot(player_data['dailyDataDate'],player_data['target2'])\nf1_axes[0,1].xaxis.set_major_locator(mdates.WeekdayLocator(interval=13))\nf1_axes[0,1].xaxis.set_major_formatter(DateFormatter(\"%y-%m-%d\"))\nf1_axes[0,1].set_title('Target metric 2')\n\nf1_axes[1,0].plot(player_data['dailyDataDate'],player_data['target3'])\nf1_axes[1,0].xaxis.set_major_locator(mdates.WeekdayLocator(interval=13))\nf1_axes[1,0].xaxis.set_major_formatter(DateFormatter(\"%y-%m-%d\"))\nf1_axes[1,0].set_title('Target metric 3')\n\nf1_axes[1,1].plot(player_data['dailyDataDate'],player_data['target4'])\nf1_axes[1,1].xaxis.set_major_locator(mdates.WeekdayLocator(interval=13))\nf1_axes[1,1].xaxis.set_major_formatter(DateFormatter(\"%y-%m-%d\"))\nf1_axes[1,1].set_title('Target metric 4')\n\n\n","metadata":{"execution":{"iopub.status.busy":"2021-06-20T06:53:54.085977Z","iopub.execute_input":"2021-06-20T06:53:54.086389Z","iopub.status.idle":"2021-06-20T06:53:55.154193Z","shell.execute_reply.started":"2021-06-20T06:53:54.086335Z","shell.execute_reply":"2021-06-20T06:53:55.15315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We can see all metric increase for Joe Kelly at start of 2020 season except metric 1 which just saw a small increase, which would let us assume it is more about official source of news. To compare with somewhat negative setiment we can see how José Altuve one of player in Astros perform:","metadata":{}},{"cell_type":"code","source":"player_data = data[data.playerId == 514888] #José Altuve\n\n\nfig1, f1_axes = plt.subplots(ncols=2, nrows=2, constrained_layout=True,figsize=(20, 8))\n\nplt.suptitle('Target metric of José Altuve')\n\nf1_axes[0,0].plot(player_data['dailyDataDate'],player_data['target1'])\nf1_axes[0,0].xaxis.set_major_locator(mdates.WeekdayLocator(interval=13))\nf1_axes[0,0].xaxis.set_major_formatter(DateFormatter(\"%y-%m-%d\"))\nf1_axes[0,0].set_title('Target metric 1')\n\n\nf1_axes[0,1].plot(player_data['dailyDataDate'],player_data['target2'])\nf1_axes[0,1].xaxis.set_major_locator(mdates.WeekdayLocator(interval=13))\nf1_axes[0,1].xaxis.set_major_formatter(DateFormatter(\"%y-%m-%d\"))\nf1_axes[0,1].set_title('Target metric 2')\n\nf1_axes[1,0].plot(player_data['dailyDataDate'],player_data['target3'])\nf1_axes[1,0].xaxis.set_major_locator(mdates.WeekdayLocator(interval=13))\nf1_axes[1,0].xaxis.set_major_formatter(DateFormatter(\"%y-%m-%d\"))\nf1_axes[1,0].set_title('Target metric 3')\n\nf1_axes[1,1].plot(player_data['dailyDataDate'],player_data['target4'])\nf1_axes[1,1].xaxis.set_major_locator(mdates.WeekdayLocator(interval=13))\nf1_axes[1,1].xaxis.set_major_formatter(DateFormatter(\"%y-%m-%d\"))\nf1_axes[1,1].set_title('Target metric 4')\n","metadata":{"execution":{"iopub.status.busy":"2021-06-20T06:58:49.133106Z","iopub.execute_input":"2021-06-20T06:58:49.133749Z","iopub.status.idle":"2021-06-20T06:58:50.127045Z","shell.execute_reply.started":"2021-06-20T06:58:49.133698Z","shell.execute_reply":"2021-06-20T06:58:50.125953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We see conversely target metric 2 is low compare to target 1 and 3 which may indicate target 2 correlate much more strongly to positive setiment than negative one.\n\nAnother example of negative setiment is on July 28, 2019 Trevor Bauer then pitcher for Cleveland Indians frustrated by giving up runs and launch a baseball to outfield, and traded to Cincinnati Reds three days ago. He then plays very well in Reds and get his first Cy Young award in 2020 season, but here we only focus on the time when it happen.","metadata":{}},{"cell_type":"code","source":"player_data = data[data.playerId == 545333] #Trevor Bauer\n\n\nfig1, f1_axes = plt.subplots(ncols=2, nrows=2, constrained_layout=True,figsize=(20, 8))\n\nplt.suptitle('Target metric of Trevor Bauer')\n\nf1_axes[0,0].plot(player_data['dailyDataDate'],player_data['target1'])\nf1_axes[0,0].xaxis.set_major_locator(mdates.WeekdayLocator(interval=13))\nf1_axes[0,0].xaxis.set_major_formatter(DateFormatter(\"%y-%m-%d\"))\nf1_axes[0,0].set_title('Target metric 1')\n\n\nf1_axes[0,1].plot(player_data['dailyDataDate'],player_data['target2'])\nf1_axes[0,1].xaxis.set_major_locator(mdates.WeekdayLocator(interval=13))\nf1_axes[0,1].xaxis.set_major_formatter(DateFormatter(\"%y-%m-%d\"))\nf1_axes[0,1].set_title('Target metric 2')\n\nf1_axes[1,0].plot(player_data['dailyDataDate'],player_data['target3'])\nf1_axes[1,0].xaxis.set_major_locator(mdates.WeekdayLocator(interval=13))\nf1_axes[1,0].xaxis.set_major_formatter(DateFormatter(\"%y-%m-%d\"))\nf1_axes[1,0].set_title('Target metric 3')\n\nf1_axes[1,1].plot(player_data['dailyDataDate'],player_data['target4'])\nf1_axes[1,1].xaxis.set_major_locator(mdates.WeekdayLocator(interval=13))\nf1_axes[1,1].xaxis.set_major_formatter(DateFormatter(\"%y-%m-%d\"))\nf1_axes[1,1].set_title('Target metric 4')\n","metadata":{"execution":{"iopub.status.busy":"2021-06-20T07:07:32.642892Z","iopub.execute_input":"2021-06-20T07:07:32.643319Z","iopub.status.idle":"2021-06-20T07:07:33.627811Z","shell.execute_reply.started":"2021-06-20T07:07:32.643286Z","shell.execute_reply":"2021-06-20T07:07:33.626993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We can see all metric except metric 2 saw a huge increase, which correlated previous finding of metric 2\n\nCombinding all our previous finding:\n\n1. Target metric 1 and 3 happen sparsely but metric 1 is even more sparse, also it seems to be from more official source as well.\n2. Only target metric 2 correlate to positive setiment strongly than negative one, and it can be non zero for minor league player\n3. Only target metric 4 has non-zero value after player's retirement/no longer play in MLB\n\nTherefore my bold guess is:\n\n1. Fans engagement of news from MLB.com\n2. Sales/engagement of MLB online shop\n3. Aggregate of all MLB news from major press\n4. Aggregate mention on social media\n\nFor metric 4 it could also be how player trending on https://baseballsavant.mlb.com/ since it's also a digital platform hosted by MLB. \n\nIn any case it's just my guess and if anyone has a better idea welcome to comment below.","metadata":{}}]}