{"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":"markdown","source":"# MLB Player Digital Engagement Forecasting","metadata":{"_uuid":"bc883192-d767-4cc3-9c0c-e491113a6c8f","_cell_guid":"a72b33b2-d836-43a3-87a0-3e23c7ac42ec","trusted":true}},{"cell_type":"markdown","source":"**메이저리그 야구 선수들의 디지털 engagement 예측 - 2022712470 이하람**\n\n과연 메이저리그 야구 선수들의 기록이나 수상, 특정 feature들이 MLB Digital Engagement에 영향을 주는지 알아보고자 함\n\nI would like to find out if the records, awards, and specific features of Major League Baseball players affect MLB Digital Engagement\n\nsource: https://www.youtube.com/watch?v=Xd79vC-ZDvk\n\n-> 본 competition에 대한 설명\n메이저 리그 베이스볼이 AI/ML을 사용하여 선수-팬 관계를 해결하는 방법을 살펴보고, 메이저 리그 야구의 혁신을 알리는 새로운 예측 모델을 구축하는 방법을 알아보십시오.","metadata":{"_uuid":"654f18f2-cc3a-4bfd-8ff1-dc395ed27506","_cell_guid":"d88a58d2-ac26-4c5e-8629-4d13b5d1c39d","trusted":true}},{"cell_type":"markdown","source":"Original Ideas\n* Do rivalry games create more digital content for players?\n* Are the best players on the best teams the most followed on twitter?\n* Do other sporting events impact the digital content for MLB?\n* Does digital engagement change during the innings?\n* Does the all-star event impact players performances?\n* Do awards benefit players digital content?\n* Do twitter followers engage with the best players?\n\n**Question**\n1) 상을 많이 받은 선수가 popular한지\n\n2) 출신 국가가 popularity에 영향을 주는지\n\n3) popularity 측정은 어떻게 하면 될지? -> Twitter 팔로워 수","metadata":{"_uuid":"a87dd512-f78e-4db4-983f-884d70efe4f8","_cell_guid":"f982fbdf-7acb-4dbe-9093-f893562c90ee","trusted":true}},{"cell_type":"markdown","source":"# 1. Importing the data","metadata":{"_uuid":"1c59e620-9d4c-4d9a-aaaf-12ace5943a68","_cell_guid":"2f3d6b22-1788-480a-aa2d-bc8c42b5dbdd","trusted":true}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport plotly.express as px\n#plotly express: scatterplot matrices, 평형좌표 및 병렬 범주 플롯 등 다차원 플롯 등 다양한 차트 지원\n\n#os.walk(path): 특정 경로 내에 존재하는 폴더(디렉토리)와 파일 리스트 뿐만 아니라, 모든 하위 디렉토리 구조를 다 검색\n#(경로, 경로 내 디렉토리 리스트, 경로 내 파일 리스트)\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n        print(filename)","metadata":{"_uuid":"de0e8757-39ea-44fb-83b2-8389fbfcb1f8","_cell_guid":"d6fc31c4-d8b3-4a60-9f9c-df0f2973836e","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:53:18.711026Z","iopub.execute_input":"2022-12-13T16:53:18.711703Z","iopub.status.idle":"2022-12-13T16:53:18.732942Z","shell.execute_reply.started":"2022-12-13T16:53:18.711598Z","shell.execute_reply":"2022-12-13T16:53:18.731579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Review each of the csv files to understand what is available\n#각 csv 파일에서 어떤 내용을 확인할 수 있는지 확인\n\ndir_name = '/kaggle/input/mlb-player-digital-engagement-forecasting/'\ndata = ['players', 'teams', 'seasons', 'awards']\n\n# Create a list of dataframes\ncsvs = [pd.read_csv(f'{dir_name}{d}.csv') for d in data]\n#print(csvs)","metadata":{"_uuid":"b232e408-e0c8-470e-9bc8-63c689961cf4","_cell_guid":"23ce9f5f-a87c-487d-831b-941f3c4d17a4","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:53:18.735052Z","iopub.execute_input":"2022-12-13T16:53:18.735353Z","iopub.status.idle":"2022-12-13T16:53:18.797058Z","shell.execute_reply.started":"2022-12-13T16:53:18.735325Z","shell.execute_reply":"2022-12-13T16:53:18.795954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2. Exploratory data analysis","metadata":{"_uuid":"bf662044-8e66-4059-b77f-dba74dccbfa8","_cell_guid":"a35f3495-d8ed-476e-846a-7eb50774a4ac","trusted":true}},{"cell_type":"markdown","source":"* **2a. Awards data**","metadata":{"_uuid":"65cccb54-7799-4145-b955-ff8e95737302","_cell_guid":"00257b20-71f7-4939-831a-f83be818a5da","trusted":true}},{"cell_type":"code","source":"# Import the awards data and set date\n# awards 데이터 불러오기\nawards = pd.read_csv(f'{dir_name}awards.csv')\nlen(awards)","metadata":{"_uuid":"00e93a57-d55e-4f43-becc-33171434f3bd","_cell_guid":"aea9b4fe-ca71-484c-8b81-783fe29fc915","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:53:18.799157Z","iopub.execute_input":"2022-12-13T16:53:18.799453Z","iopub.status.idle":"2022-12-13T16:53:18.828581Z","shell.execute_reply.started":"2022-12-13T16:53:18.799425Z","shell.execute_reply":"2022-12-13T16:53:18.827383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(awards)","metadata":{"_uuid":"cbeec76a-0757-42f2-b453-44f3442b2b77","_cell_guid":"764a3926-7035-49a2-93a5-b36ee520ad6f","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:53:18.830509Z","iopub.execute_input":"2022-12-13T16:53:18.830852Z","iopub.status.idle":"2022-12-13T16:53:18.851722Z","shell.execute_reply.started":"2022-12-13T16:53:18.830819Z","shell.execute_reply":"2022-12-13T16:53:18.850513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- awardDate: 수상 날짜\n- awardSeason: 수상한 시즌\n- awardId: 수상 ID\n- awardName: 수상명\n- playerId: 선수 ID(Unique identifier for a player)\n- playerName: 선수이름\n- awardPlayerTeamId: 수상 선수의 팀 ID","metadata":{"_uuid":"3d01e4b0-a314-480e-bc94-acee3242bd41","_cell_guid":"0278d355-7291-469b-a3bd-be6a4d9b4e79","trusted":true}},{"cell_type":"code","source":"# Variable data types\n# 생성한 데이터프레임의 각 열의 데이터 타입을 한 번에 확인 가능\n\nawards.dtypes #awards 데이터프레임의 데이터 타입 확인","metadata":{"_uuid":"0bdfc0c1-adbb-4ba5-b049-4015b936658b","_cell_guid":"c4891f99-dae7-4d7d-a739-215c602a965c","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:53:18.853156Z","iopub.execute_input":"2022-12-13T16:53:18.853467Z","iopub.status.idle":"2022-12-13T16:53:18.865812Z","shell.execute_reply.started":"2022-12-13T16:53:18.853435Z","shell.execute_reply":"2022-12-13T16:53:18.864636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Describe the key variables within the awards DataFrame\n# Including the 'all' parameter allows the string variables to be included in the output\n#'all' 매개 변수를 포함하면 문자열 변수를 출력에 포함 가능\n# 'datetime_is_numeric=True' apply the min, max, and percentiles to your datetimes.\n\nawards.describe(include='all', datetime_is_numeric=True)","metadata":{"_uuid":"15389af7-9e29-45a6-ae43-cc6733e3fe9e","_cell_guid":"fbf506ec-6cb4-4b9f-83a8-bd4c458e431d","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:53:18.867596Z","iopub.execute_input":"2022-12-13T16:53:18.868043Z","iopub.status.idle":"2022-12-13T16:53:18.921028Z","shell.execute_reply.started":"2022-12-13T16:53:18.868002Z","shell.execute_reply":"2022-12-13T16:53:18.919794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"awards.head()","metadata":{"_uuid":"c73e2dc6-0a00-4b07-92bb-ec2fa46fcee4","_cell_guid":"c12488da-f5bb-49a9-a422-e35c1ae9eaa2","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:53:18.923019Z","iopub.execute_input":"2022-12-13T16:53:18.923313Z","iopub.status.idle":"2022-12-13T16:53:18.938703Z","shell.execute_reply.started":"2022-12-13T16:53:18.923283Z","shell.execute_reply":"2022-12-13T16:53:18.937440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Sample of the dataframe\nawards.sample(10)","metadata":{"_uuid":"f46789f9-08c7-4725-a91f-2e041d319978","_cell_guid":"a7340b73-f823-4e77-a2ac-afbc4d3e4f05","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:53:18.940232Z","iopub.execute_input":"2022-12-13T16:53:18.940530Z","iopub.status.idle":"2022-12-13T16:53:18.962080Z","shell.execute_reply.started":"2022-12-13T16:53:18.940500Z","shell.execute_reply":"2022-12-13T16:53:18.960985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- word cloud: 메타 데이터에서 얻어진 태그 분석을 통해 중요도, 인기도 등을 고려하여 시각적으로 늘어놓아 표시\n- 중요도(또는 인기도)에 따라 글자의 색상, 굵기 등 형태 변함\n- 키워드에만 집중하기 위한 시각화 표현 기법\n\n출처: https://ssoonidev.tistory.com/tag/Python%20Wordcloud\nhttps://blog.naver.com/PostView.naver?blogId=pino93&logNo=221998227204","metadata":{"_uuid":"a06c2020-2a0e-4af0-90bd-d1a332fcc2b2","_cell_guid":"d1560f37-0867-4066-804b-aac8d863810f","trusted":true}},{"cell_type":"code","source":"# 기존 코드 제작자가 포함해둔 코드\n#word cloud visualisation to show the popular neighbourhoods\n\nfrom wordcloud import WordCloud\n\nplt.subplots(figsize=(15,15))\nwordcloud = WordCloud(\n                          width=1920,\n                          height=1080\n                         ).generate(\" \".join(awards.playerName)) #join:하나의 문자열로 전환\nplt.imshow(wordcloud)\nplt.title('Word Cloud for Player Name Awards')\nplt.axis('off')\nplt.show()","metadata":{"_uuid":"d9f5f316-d2b6-47fc-ba5c-ef36d26cb845","_cell_guid":"84e2b417-8cbc-4334-aad6-b3532f80e8da","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:53:18.963693Z","iopub.execute_input":"2022-12-13T16:53:18.964068Z","iopub.status.idle":"2022-12-13T16:53:24.285844Z","shell.execute_reply.started":"2022-12-13T16:53:18.964036Z","shell.execute_reply":"2022-12-13T16:53:24.284796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Understand the 10 largest award winners\n# groupby.count().nlargest:그룹 내에서 상위 n개 가져오기\n\ntop_players = awards.groupby('playerName')['playerName'].count().nlargest(n=10, keep='all')\ntop_players","metadata":{"_uuid":"c0e356f2-36db-4101-8cae-3fa7d8123ba0","_cell_guid":"42e6fb6c-010a-4834-918a-129a9a0c7af9","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:53:24.287131Z","iopub.execute_input":"2022-12-13T16:53:24.287431Z","iopub.status.idle":"2022-12-13T16:53:24.303312Z","shell.execute_reply.started":"2022-12-13T16:53:24.287402Z","shell.execute_reply":"2022-12-13T16:53:24.302107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#푸홀스 데이터 확인해보기\nawards[awards['playerName']=='Albert Pujols']","metadata":{"_uuid":"963aaa31-9f27-400f-8404-0ade0c63a4f7","_cell_guid":"cc40eeab-75b2-488a-86da-8001dc8f24b8","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:53:24.305755Z","iopub.execute_input":"2022-12-13T16:53:24.306101Z","iopub.status.idle":"2022-12-13T16:53:24.331475Z","shell.execute_reply.started":"2022-12-13T16:53:24.306068Z","shell.execute_reply":"2022-12-13T16:53:24.330589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- groupby.agg(): 여러개의 함수를 여러 열에 적용\n- 여기서는 딕셔너리 사용해서 칼럼, 함수 매핑 후 특정 groupby 집계 함수 적용\n- pd.Series.nunique: Return number of unique elements in the object, count the unique values\n- nunique()는 데이터 고유값들의 수를 출력해주는 함수","metadata":{"_uuid":"ce8cc28a-85bb-4d2a-be20-b17ab26afa2a","_cell_guid":"a77570a7-1ac0-4959-a890-797b604f8135","trusted":true}},{"cell_type":"code","source":"# For the 10 largest award winners. Lets understand the number of unique awards won\n# Did one player dominate a single award\n\nplayer = awards.groupby('playerName').agg(\n    {\n        'playerName' : 'count',\n        'awardSeason' : ['min', 'max'],\n        'awardId' : pd.Series.nunique\n    }\n).nlargest(10, ('playerName', 'count'))\nplayer","metadata":{"_uuid":"577ac7f6-9cbe-4f51-8294-4eee5b209490","_cell_guid":"0f894802-4ca2-4160-b6db-1754811ba829","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:53:24.333050Z","iopub.execute_input":"2022-12-13T16:53:24.333382Z","iopub.status.idle":"2022-12-13T16:53:24.706877Z","shell.execute_reply.started":"2022-12-13T16:53:24.333328Z","shell.execute_reply":"2022-12-13T16:53:24.705875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Pujols, Trout, Cabrera, Kershaw 등 word cloud에서 크게 나타난 선수들이 상을 많이 수상한 것을 확인할 수 있음","metadata":{"_uuid":"6f4019da-1d43-468a-8026-fccda2ad427a","_cell_guid":"d6d11402-41c6-4cc7-a327-7dbb5ec99c1c","trusted":true}},{"cell_type":"markdown","source":"# distplot, histplot으로 그려보기-awards","metadata":{"_uuid":"91d33c87-12a6-4e86-9791-bdc9efb567d2","_cell_guid":"ab9ba0c3-376e-426b-addc-9129cc917ac2","trusted":true}},{"cell_type":"code","source":"import seaborn as sns\n\nax = sns.distplot(player['playerName'],\n                  hist = True,\n                  kde = True,\n                  bins = 10,\n                  color = 'blue',\n                  hist_kws = {'edgecolor':'gray'},\n                  kde_kws = {'linewidth':2})\nax.set_title('TOP 10 Awards Players')","metadata":{"_uuid":"ee1ab137-5e19-4dec-83fc-328776ff43f0","_cell_guid":"c24671bc-cd07-4711-abc9-376a500d1e06","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:53:24.708494Z","iopub.execute_input":"2022-12-13T16:53:24.708941Z","iopub.status.idle":"2022-12-13T16:53:24.903790Z","shell.execute_reply.started":"2022-12-13T16:53:24.708896Z","shell.execute_reply":"2022-12-13T16:53:24.902707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\n\nax = sns.histplot(player['playerName'],\n                  bins = 10)\nax.set_title('TOP 10 Awards Players')","metadata":{"_uuid":"14e76742-f62c-4a17-818b-5dab655327dc","_cell_guid":"08287e10-e5e3-4758-bc3c-eba004ba7256","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:53:24.905170Z","iopub.execute_input":"2022-12-13T16:53:24.905495Z","iopub.status.idle":"2022-12-13T16:53:25.117012Z","shell.execute_reply.started":"2022-12-13T16:53:24.905453Z","shell.execute_reply":"2022-12-13T16:53:25.115675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert date variable from object to datetime\n# pd.to_datetime(): 시간 형식의 object 자료형 column을 datetime형식으로 변형\n\nawards['awardDate'] = pd.to_datetime(awards['awardDate'])\nprint(awards['awardDate'])\nawards.head()","metadata":{"_uuid":"bad2840b-6aab-44e7-96df-bca0fd8e3897","_cell_guid":"23307bec-6c7e-40a0-b5ac-91380cbbc86e","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:53:25.118556Z","iopub.execute_input":"2022-12-13T16:53:25.119066Z","iopub.status.idle":"2022-12-13T16:53:25.145591Z","shell.execute_reply.started":"2022-12-13T16:53:25.119019Z","shell.execute_reply":"2022-12-13T16:53:25.144502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Animated scattergraph to review player awards by time for the largest award winners\n#가장 큰 수상자에 대한 시간별 플레이어 상을 검토하는 animated scattergraph\n# np.isin(): 내가 찾고자 하는 요소가 있는지를 각 index 위치에 true, false 형태로 출력\n\ntp_array = np.array(awards['playerName'].isin(player.index))\nawards_tp = awards.loc[(tp_array)]\nawards_tp","metadata":{"_uuid":"9fc85d2c-90f4-4f10-b34a-51dd3060475c","_cell_guid":"512cb7a5-e0af-4d66-bcd8-0856c70ca7e3","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:53:25.146885Z","iopub.execute_input":"2022-12-13T16:53:25.147167Z","iopub.status.idle":"2022-12-13T16:53:25.174437Z","shell.execute_reply.started":"2022-12-13T16:53:25.147138Z","shell.execute_reply":"2022-12-13T16:53:25.173210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"awards_tp.head()","metadata":{"_uuid":"c674b5d4-31a5-4851-8a0f-39a018620ee3","_cell_guid":"1d8ee1b9-2c43-441c-8d2f-1db26944c932","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:53:25.177016Z","iopub.execute_input":"2022-12-13T16:53:25.177321Z","iopub.status.idle":"2022-12-13T16:53:25.192347Z","shell.execute_reply.started":"2022-12-13T16:53:25.177290Z","shell.execute_reply":"2022-12-13T16:53:25.191146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"awards_sum = awards_tp.groupby(['playerName', 'awardSeason'])['playerId'].count()\nawards_sum1 = awards_sum.reset_index()\nawards_sum1","metadata":{"_uuid":"331fa0ff-98a9-4cd8-b3f0-a879d6cfc914","_cell_guid":"dcbf818a-c417-45e8-ad84-325ff27d04f7","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:53:25.194371Z","iopub.execute_input":"2022-12-13T16:53:25.194694Z","iopub.status.idle":"2022-12-13T16:53:25.216577Z","shell.execute_reply.started":"2022-12-13T16:53:25.194649Z","shell.execute_reply":"2022-12-13T16:53:25.215624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.bar(awards_sum1, x='playerName', y='playerId', color='playerName',\n            animation_frame='awardSeason')\nfig.show()","metadata":{"_uuid":"ccab44e6-7e2b-487a-b1ca-2fb581a8a229","_cell_guid":"3a31a09a-25e8-4563-939b-51600980ce1c","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:53:25.220196Z","iopub.execute_input":"2022-12-13T16:53:25.220516Z","iopub.status.idle":"2022-12-13T16:53:25.748301Z","shell.execute_reply.started":"2022-12-13T16:53:25.220485Z","shell.execute_reply":"2022-12-13T16:53:25.745134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"-> 2001~2016 시즌 동안 상을 수상한 TOP10 선수 변화 표시","metadata":{"_uuid":"64d67098-dd02-4cd2-aabb-0968ec13f93b","_cell_guid":"13caa165-43c7-4a42-bffe-3aee4d3c2950","trusted":true}},{"cell_type":"markdown","source":"* **2b. Players**","metadata":{"_uuid":"bec09054-bc99-4073-ac46-f8fbceec4b67","_cell_guid":"ba7934b0-dd7b-46a3-8511-4bf428f00a15","trusted":true}},{"cell_type":"code","source":"# Import the players data and set date\ndf_p = pd.read_csv(f'{dir_name}players.csv')","metadata":{"_uuid":"77ae3b89-bfbc-4ee9-8cc1-7a43f343e7c3","_cell_guid":"c4143ca0-e6b9-4557-9399-a8a7b43d4072","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:53:25.750287Z","iopub.execute_input":"2022-12-13T16:53:25.750731Z","iopub.status.idle":"2022-12-13T16:53:25.770571Z","shell.execute_reply.started":"2022-12-13T16:53:25.750685Z","shell.execute_reply":"2022-12-13T16:53:25.769518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_p.dtypes","metadata":{"_uuid":"b666935c-75a3-4004-bcf2-1d318edcc78a","_cell_guid":"d5a0cfa7-c9fd-47a6-8a35-859fe9be911e","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:53:25.772261Z","iopub.execute_input":"2022-12-13T16:53:25.772575Z","iopub.status.idle":"2022-12-13T16:53:25.781008Z","shell.execute_reply.started":"2022-12-13T16:53:25.772545Z","shell.execute_reply":"2022-12-13T16:53:25.779861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_p.head()","metadata":{"_uuid":"0aef3bd3-7567-4660-a303-ad890516fb01","_cell_guid":"af8ce3ee-977e-4417-a7a8-2d2ec6b9f82b","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:53:25.782253Z","iopub.execute_input":"2022-12-13T16:53:25.782754Z","iopub.status.idle":"2022-12-13T16:53:25.801388Z","shell.execute_reply.started":"2022-12-13T16:53:25.782706Z","shell.execute_reply":"2022-12-13T16:53:25.799997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_p.sample(10)","metadata":{"_uuid":"85e177d4-72ce-4717-802b-c552c83c409d","_cell_guid":"e8f657fb-7997-4bd9-8edf-fd99bdab3f7c","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:53:25.803055Z","iopub.execute_input":"2022-12-13T16:53:25.803478Z","iopub.status.idle":"2022-12-13T16:53:25.829771Z","shell.execute_reply.started":"2022-12-13T16:53:25.803440Z","shell.execute_reply":"2022-12-13T16:53:25.828613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Distribution of players by country\ndf_p_s1 = df_p.groupby('birthCountry').agg(\n    {\n        'playerName' : 'count'\n    }\n)\n\n# Bar chart\nfig = px.bar(df_p_s1, x=df_p_s1.index, y=\"playerName\", title=\"Distribution by Country\")\nfig.show()","metadata":{"_uuid":"05e3f7aa-7226-473d-988a-23a61f25a047","_cell_guid":"2a37fb2e-7e2e-4c72-a1f7-33bdf3210bba","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:53:25.831165Z","iopub.execute_input":"2022-12-13T16:53:25.831510Z","iopub.status.idle":"2022-12-13T16:53:25.913791Z","shell.execute_reply.started":"2022-12-13T16:53:25.831477Z","shell.execute_reply":"2022-12-13T16:53:25.912771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"top_countries = df_p.groupby('birthCountry')['playerName'].count().nlargest(n=10, keep='all')\ntop_countries","metadata":{"_uuid":"94c2807f-e975-439a-a8fe-2e2db3150087","_cell_guid":"18086519-6f58-483f-bb76-696a352d331f","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:53:25.915056Z","iopub.execute_input":"2022-12-13T16:53:25.915339Z","iopub.status.idle":"2022-12-13T16:53:25.927018Z","shell.execute_reply.started":"2022-12-13T16:53:25.915310Z","shell.execute_reply":"2022-12-13T16:53:25.925792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert the mlbDebutDate and DOB to datetime\n# MLB데뷔일과 생년월일을 datetime으로 변환(.to_datetime 활용)\n\ndf_p['mlbDebutDate'] = pd.to_datetime(df_p['mlbDebutDate'])\ndf_p['DOB'] = pd.to_datetime(df_p['DOB'])","metadata":{"_uuid":"ac3b9c18-7951-4f12-8751-325200b778f4","_cell_guid":"e8808b45-8c96-4cb1-afc4-e47ce1127b8f","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:53:25.928483Z","iopub.execute_input":"2022-12-13T16:53:25.929074Z","iopub.status.idle":"2022-12-13T16:53:25.939453Z","shell.execute_reply.started":"2022-12-13T16:53:25.929027Z","shell.execute_reply":"2022-12-13T16:53:25.938577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Debut year and birth year\n# .dt.year을 활용하여 4자리 연도로 설정\n\ndf_p['mlbDebutYear'] = df_p['mlbDebutDate'].dt.year\ndf_p['DOBYear'] = df_p['DOB'].dt.year\n\n# What age is average for starting in MLB (MLB에 데뷔하는 평균 연령)\n# 데뷔연도='MLB데뷔연도-출생연도'\ndf_p['DebutAge'] = df_p['mlbDebutYear'] - df_p['DOBYear']","metadata":{"_uuid":"9cf5eda7-5a93-48b9-b40e-233fc566c923","_cell_guid":"351cb05e-a936-486f-aead-ff797c36e78d","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:53:25.940711Z","iopub.execute_input":"2022-12-13T16:53:25.941010Z","iopub.status.idle":"2022-12-13T16:53:25.953977Z","shell.execute_reply.started":"2022-12-13T16:53:25.940982Z","shell.execute_reply":"2022-12-13T16:53:25.952762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# summary of the numeric values\ndf_p.describe()","metadata":{"_uuid":"1352aef8-8f76-472e-aded-9e9a5638efd9","_cell_guid":"7c0c8912-100b-4822-81b9-3b169f6b9950","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:53:25.955335Z","iopub.execute_input":"2022-12-13T16:53:25.955675Z","iopub.status.idle":"2022-12-13T16:53:25.994996Z","shell.execute_reply.started":"2022-12-13T16:53:25.955625Z","shell.execute_reply":"2022-12-13T16:53:25.993994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Has the distribution of new players got younger over time?\n# 새로운 선수들의 분포는 시간이 지남에 따라 젊어졌는지 알아보고자 함 - 기존 kaggle 코드\n\nage_sum = df_p.groupby(['mlbDebutYear', 'DebutAge'])['playerName'].count()\nage_sum = age_sum.reset_index()\nage_sum\nfig = px.bar(age_sum, x=\"mlbDebutYear\", y=\"playerName\", color=\"DebutAge\")\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-13T17:06:14.220463Z","iopub.execute_input":"2022-12-13T17:06:14.220939Z","iopub.status.idle":"2022-12-13T17:06:14.304741Z","shell.execute_reply.started":"2022-12-13T17:06:14.220899Z","shell.execute_reply":"2022-12-13T17:06:14.303933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This output will be impacted by players who have left the baseball dataset over time. The players that remain are only included. Therefore the most recent years provide a fairer reflection of the age distribution for MLB debut's.\n\n이 결과는 시간이 지남에 따라 야구 data set을 떠난(=은퇴 또는 방출?) 선수들에 의해 영향을 받을 것이다. 남은 선수들만 포함되어 있다. 그러므로 가장 최근의 해들은 MLB 데뷔를 위한 연령 분포를 더 공정하게 반영한다.","metadata":{}},{"cell_type":"code","source":"# Review a scatter plot\nfig = px.scatter(\n    age_sum, x='mlbDebutYear', y='DebutAge', opacity=0.65, size=\"playerName\",\n    trendline='ols', trendline_color_override='darkblue'\n)\nfig.show()","metadata":{"_uuid":"aacd2d7b-18c2-44c6-99a7-debeb6f04eca","_cell_guid":"274b8c57-71ad-4684-9426-2ad394687a56","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T17:06:31.978377Z","iopub.execute_input":"2022-12-13T17:06:31.979147Z","iopub.status.idle":"2022-12-13T17:06:32.063388Z","shell.execute_reply.started":"2022-12-13T17:06:31.979095Z","shell.execute_reply":"2022-12-13T17:06:32.062316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"22~26세 사이에 MLB 데뷔하는 선수들이 지속해서 증가하고 있음\n\n보통 평균 MLB 평균 데뷔 연령이 22~26세 정도인 것을 확인 가능함","metadata":{"_uuid":"fc4b8be2-7952-4e7b-ba06-22fb675a7926","_cell_guid":"c88fa75d-ab16-480d-a667-d5831adacc3e","trusted":true}},{"cell_type":"markdown","source":"* **2.3 Twitter**","metadata":{"_uuid":"47a97425-e7da-4bce-9366-d3f2188c330f","_cell_guid":"81c86ba8-6f90-43af-9439-918515d28380","trusted":true}},{"cell_type":"markdown","source":"Twitter 데이터 추출을 위한 코드 참고\n\n출처: https://www.kaggle.com/code/alokpattani/mlb-player-digital-engagement-data-exploration/notebook#Look-at-Relationships-Between-Various-Factors-and-Target-Variables","metadata":{"_uuid":"68a4a9b0-7655-467a-9eba-36028761b477","_cell_guid":"57b750e8-81a3-47c8-b73c-0f1b0a8ba7c4","trusted":true}},{"cell_type":"code","source":"#### Import Python Libraries and Set Script Options ####\nimport numpy as np\nimport pandas as pd\n\n# Plotly libraries\nimport plotly as pl\nimport plotly.express as px\nimport plotly.offline as pyo\nimport plotly.graph_objs as go\n\n# Library for interactive Python widgets\nimport ipywidgets as widgets\n\n# Utility libraries\nimport gc\nfrom pathlib import Path\n\n# Set notebook mode to make plotly graphics offline\npyo.init_notebook_mode()\n\n# Expand max column width when displaying data frames \npd.set_option('display.max_colwidth', 100)\n\n# Lists all input data files from \"../input/\" directory\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"9b02a6ce-eb54-4200-a166-6a75e084e027","_cell_guid":"6b5b510f-5964-46a4-9640-99b8a905e0c6","collapsed":false,"_kg_hide-input":true,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:53:26.086501Z","iopub.execute_input":"2022-12-13T16:53:26.087079Z","iopub.status.idle":"2022-12-13T16:53:26.216135Z","shell.execute_reply.started":"2022-12-13T16:53:26.087015Z","shell.execute_reply":"2022-12-13T16:53:26.215146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Start with input file path\ninput_file_path = Path('/kaggle/input/mlb-player-digital-engagement-forecasting/')\n\n# Create table with list of CSV files to be read in, w/ corresponding df name\n# This does include large 'train' data set (read in separately)\ncsv_and_df_names = pd.DataFrame(data = {\n  'csv_name': ['seasons', 'teams', 'players', 'awards',\n    'example_test', 'example_sample_submission'],\n  'df_name': ['seasons', 'teams', 'players', 'awards_pre2018',\n    'example_test', 'example_sample_submission'] \n  })\n\n# Set up for tabbed output\nkaggle_data_tabs = widgets.Tab()\n\n# Add Output widgets for each (eventual) DF as tabs' children\nkaggle_data_tabs.children = list([widgets.Output() for df_name \n  in csv_and_df_names['df_name']])\n\nfor index, row in csv_and_df_names.iterrows():\n    \n    csv_name = row['csv_name']\n    df_name = row['df_name']\n    \n    # Read from CSV and create df with specified name in environment\n    globals()[df_name] = pd.read_csv(input_file_path / f\"{csv_name}.csv\")\n\n    # Set tab title to df name\n    kaggle_data_tabs.set_title(index, df_name)\n    \n    # Display corresponding table output for this tab name\n    with kaggle_data_tabs.children[index]:\n        display(eval(df_name))\n\ndisplay(kaggle_data_tabs)","metadata":{"_uuid":"b78747e7-ad96-44ae-91ed-9d82b256e38b","_cell_guid":"1514c7dd-b97b-4176-9302-d79b2704a57d","collapsed":false,"_kg_hide-input":true,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:53:26.217444Z","iopub.execute_input":"2022-12-13T16:53:26.217760Z","iopub.status.idle":"2022-12-13T16:53:26.926714Z","shell.execute_reply.started":"2022-12-13T16:53:26.217729Z","shell.execute_reply":"2022-12-13T16:53:26.925617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(input_file_path / 'train.csv')\n\n# Convert training data date field to pandas datetime type\ntrain['date'] = pd.to_datetime(train['date'], format = \"%Y%m%d\")\n\ndisplay(train.info())\n\ndisplay(train)","metadata":{"_uuid":"d223c324-6180-49c2-b642-4f7a72329269","_cell_guid":"20100f3b-239f-4cb3-9498-4b28ae652d67","collapsed":false,"_kg_hide-input":true,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:53:26.928163Z","iopub.execute_input":"2022-12-13T16:53:26.928599Z","iopub.status.idle":"2022-12-13T16:54:13.647436Z","shell.execute_reply.started":"2022-12-13T16:53:26.928565Z","shell.execute_reply":"2022-12-13T16:54:13.646165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get names of all \"nested\" data frames in daily training set\ndaily_data_nested_df_names = train.drop('date', axis = 1).columns.values.tolist()\n\nfor df_name in daily_data_nested_df_names:\n    date_nested_table = train[['date', df_name]]\n\n    date_nested_table = (date_nested_table[\n      ~pd.isna(date_nested_table[df_name])\n      ].\n      reset_index(drop = True)\n      )\n    \n    daily_dfs_collection = []\n    \n    for date_index, date_row in date_nested_table.iterrows():\n        daily_df = pd.read_json(date_row[df_name])\n        \n        daily_df['dailyDataDate'] = date_row['date']\n        \n        daily_dfs_collection = daily_dfs_collection + [daily_df]\n\n    # Concatenate all daily dfs into single df for each row\n    unnested_table = (pd.concat(daily_dfs_collection,\n      ignore_index = True).\n      # Set and reset index to move 'dailyDataDate' to front of df\n      set_index('dailyDataDate').\n      reset_index()\n      )\n    \n    # Creates 1 pandas df per unnested df from daily data read in, with same name\n    globals()[df_name] = unnested_table    \n    \n    # Clean up tables and collection of daily data frames for this df\n    del(date_nested_table, daily_dfs_collection, unnested_table)\n\n# Set up for tabbed output\ndaily_data_unnested_tabs = widgets.Tab()\n\n# Add Output widgets for each (eventual) DF as tabs' children\ndaily_data_unnested_tabs.children = list([widgets.Output() \n  for df_name in daily_data_nested_df_names])\n\nfor index in range(0, len(daily_data_nested_df_names)):\n    df_name = daily_data_nested_df_names[index]\n    \n    # Rename tab bar titles to df names\n    daily_data_unnested_tabs.set_title(index, df_name)\n\n    # Display corresponding table output for this tab name\n    with daily_data_unnested_tabs.children[index]:\n        display(eval(df_name))\n\ndisplay(daily_data_unnested_tabs)","metadata":{"_uuid":"956223a0-90c3-4362-964c-96a1462ebcb5","_cell_guid":"5365eed9-5202-4348-889a-0e6640f1b73a","collapsed":false,"_kg_hide-input":true,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:54:13.649425Z","iopub.execute_input":"2022-12-13T16:54:13.649923Z","iopub.status.idle":"2022-12-13T16:58:36.160786Z","shell.execute_reply.started":"2022-12-13T16:54:13.649875Z","shell.execute_reply":"2022-12-13T16:58:36.159702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extract only desired fields, rename some fields (for clarity later)\nplayer_twitter_followers_for_merge = (playerTwitterFollowers\n  [['date', 'playerName', 'playerId', 'numberOfFollowers']].\n  rename(columns = {\n    'date': 'playerTwitterDataDate',\n    'numberOfFollowers': 'playerTwitterFollowers'\n    })\n  )\n\nfollowers = pd.DataFrame(player_twitter_followers_for_merge) #데이터프레임으로 설정\n\ndisplay(player_twitter_followers_for_merge)","metadata":{"_uuid":"1915c556-6350-443f-8129-964c4bccf574","_cell_guid":"f937a02e-e7d1-481b-bdc8-e118d7bec59f","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:58:36.162167Z","iopub.execute_input":"2022-12-13T16:58:36.162481Z","iopub.status.idle":"2022-12-13T16:58:36.190495Z","shell.execute_reply.started":"2022-12-13T16:58:36.162448Z","shell.execute_reply":"2022-12-13T16:58:36.189425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 트위터 데이터를 통해 도출하고자 하는 결과\n\n1. 상을 가장 많이 받은 Albert Pujols(70), Mike Trout(64), Miguel Cabrera(58), Clayton Kershaw(47)의 트위터 팔로워 수 비교\n\n2. 출신 국가별 팔로워 수 비교","metadata":{"_uuid":"2545d9fa-559f-4bb2-bd87-92bc05c6e899","_cell_guid":"00c284ed-3a20-4d49-8c57-d370eeae1029","trusted":true}},{"cell_type":"code","source":"followers","metadata":{"_uuid":"d81363b9-b5b0-4014-91ff-3ff50042e119","_cell_guid":"4bbeab81-8cc6-4ab8-912c-63ed06e5a2aa","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:58:36.197058Z","iopub.execute_input":"2022-12-13T16:58:36.197405Z","iopub.status.idle":"2022-12-13T16:58:36.212793Z","shell.execute_reply.started":"2022-12-13T16:58:36.197373Z","shell.execute_reply":"2022-12-13T16:58:36.211977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#팔로워수의 평균\nf_means = followers['playerTwitterFollowers'].mean()\nf_means","metadata":{"_uuid":"79ada86e-3987-4e46-81c7-a620a764e608","_cell_guid":"5ab2f480-4641-4c42-a902-61c7acc72ea4","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:58:36.215202Z","iopub.execute_input":"2022-12-13T16:58:36.215759Z","iopub.status.idle":"2022-12-13T16:58:36.225316Z","shell.execute_reply.started":"2022-12-13T16:58:36.215708Z","shell.execute_reply":"2022-12-13T16:58:36.224287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# follower수가 높은 선수 순으로 정리\nfollowers.sort_values(by='playerTwitterFollowers', ascending=False)","metadata":{"_uuid":"41856fdb-0b69-488d-b163-77431e738d45","_cell_guid":"cdfeb656-32c1-4a16-9389-9edbb5a52d4c","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:58:36.226584Z","iopub.execute_input":"2022-12-13T16:58:36.226981Z","iopub.status.idle":"2022-12-13T16:58:36.253598Z","shell.execute_reply.started":"2022-12-13T16:58:36.226924Z","shell.execute_reply":"2022-12-13T16:58:36.252673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"idxmax 코드 참고: https://pinkwink.kr/1134\n\naward에서 상을 많이 받은 선수 4명의 가장 높은 팔로워수 탐색","metadata":{"_uuid":"f94a0fd0-049e-4492-81fa-e8975232ff11","_cell_guid":"a8ebeae3-0e37-4b17-8504-c63c37705417","trusted":true}},{"cell_type":"code","source":"award1 = followers[followers['playerName']=='Albert Pujols']\naward1.loc[award1['playerTwitterFollowers'].idxmax()]","metadata":{"_uuid":"cfd17227-849f-4c5c-b221-cd9b596f9b11","_cell_guid":"4fea7e2a-dbc4-477e-997e-ede7a08df932","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:58:36.254893Z","iopub.execute_input":"2022-12-13T16:58:36.255172Z","iopub.status.idle":"2022-12-13T16:58:36.276553Z","shell.execute_reply.started":"2022-12-13T16:58:36.255144Z","shell.execute_reply":"2022-12-13T16:58:36.275565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"award2 = followers[followers['playerName']=='Mike Trout']\naward2.loc[award2['playerTwitterFollowers'].idxmax()]","metadata":{"_uuid":"3efa92ab-12f4-4c75-8990-6028365d49be","_cell_guid":"62ff3e5f-e4a7-43c5-a449-27351f0cfb66","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:58:36.278262Z","iopub.execute_input":"2022-12-13T16:58:36.278582Z","iopub.status.idle":"2022-12-13T16:58:36.299767Z","shell.execute_reply.started":"2022-12-13T16:58:36.278550Z","shell.execute_reply":"2022-12-13T16:58:36.298706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"award3 = followers[followers['playerName']=='Miguel Cabrera']\naward3.loc[award3['playerTwitterFollowers'].idxmax()]","metadata":{"_uuid":"ff42ad09-c620-4e6b-973c-46b8349b37c3","_cell_guid":"cd42cbfa-8841-47bc-9b7f-9c5701dcdcc9","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:58:36.301221Z","iopub.execute_input":"2022-12-13T16:58:36.301572Z","iopub.status.idle":"2022-12-13T16:58:36.323760Z","shell.execute_reply.started":"2022-12-13T16:58:36.301540Z","shell.execute_reply":"2022-12-13T16:58:36.322579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"award4 = followers[followers['playerName']=='Clayton Kershaw']\naward4.loc[award4['playerTwitterFollowers'].idxmax()]","metadata":{"_uuid":"75101cb6-558d-4029-9f92-636b2f4b2c71","_cell_guid":"62d89c52-2f81-404d-a595-8cbdcb814066","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:58:36.325477Z","iopub.execute_input":"2022-12-13T16:58:36.325806Z","iopub.status.idle":"2022-12-13T16:58:36.347973Z","shell.execute_reply.started":"2022-12-13T16:58:36.325772Z","shell.execute_reply":"2022-12-13T16:58:36.346759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#네명의 선수 중 가장 많은 팔로워를 보유한 선수\n\na = pd.concat([award1, award2, award3, award4])\na.max()","metadata":{"_uuid":"388dee92-e594-418f-a262-d84fb913de5d","_cell_guid":"8311608a-00f4-4f22-a234-04025e64836d","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:58:36.349529Z","iopub.execute_input":"2022-12-13T16:58:36.349974Z","iopub.status.idle":"2022-12-13T16:58:36.363396Z","shell.execute_reply.started":"2022-12-13T16:58:36.349941Z","shell.execute_reply":"2022-12-13T16:58:36.362410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* award와 follower의 상관관계 분석","metadata":{}},{"cell_type":"code","source":"import plotly.graph_objects as go\n\ncorr = pd.concat([awards, followers])\ncorr","metadata":{"execution":{"iopub.status.busy":"2022-12-13T16:58:36.365047Z","iopub.execute_input":"2022-12-13T16:58:36.365482Z","iopub.status.idle":"2022-12-13T16:58:36.410991Z","shell.execute_reply.started":"2022-12-13T16:58:36.365434Z","shell.execute_reply":"2022-12-13T16:58:36.409960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corr.corr()","metadata":{"execution":{"iopub.status.busy":"2022-12-13T16:58:36.412307Z","iopub.execute_input":"2022-12-13T16:58:36.412599Z","iopub.status.idle":"2022-12-13T16:58:36.427193Z","shell.execute_reply.started":"2022-12-13T16:58:36.412571Z","shell.execute_reply":"2022-12-13T16:58:36.426136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"corr()는 절댓값 0.4이상이면 상관관계를 갖는다고 봅니다.\n\n다만 이 데이터프레임에서는 선수가 상을 많이 받았는지 확인할 수가 없기 때문에 향후 데이터프레임을 수정해 업데이트를 해야할 것 같습니다.\n\n다양하게 시도해보았지만 계속 되지 않아 보충이 필요한 내용입니다.","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(15,15))\nsns.heatmap(data = corr.corr(), annot=True, \nfmt = '.2f', linewidths=.5, cmap='Blues', annot_kws = {\"size\" : 16})","metadata":{"execution":{"iopub.status.busy":"2022-12-13T16:58:36.430644Z","iopub.execute_input":"2022-12-13T16:58:36.431002Z","iopub.status.idle":"2022-12-13T16:58:36.758438Z","shell.execute_reply.started":"2022-12-13T16:58:36.430969Z","shell.execute_reply":"2022-12-13T16:58:36.757462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"1. 상을 가장 많이 받은 Albert Pujols(70), Mike Trout(64), Miguel Cabrera(58), Clayton Kershaw(47)의 트위터 팔로워 수 비교\n\n네 명의 선수 모두 팔로워수 평균(63411명)보다 높은 팔로워수 보유\n\n네 명의 선수 중 **Mike Trout** 선수가 가장 많은 팔로워를 보유하고 있음\n\n그렇다면 Mike Trout의 출신 지역은 어디일까?","metadata":{"_uuid":"77349cba-a254-4c22-8eb9-2a1961bd7d92","_cell_guid":"327149e8-7ca4-4583-9337-98b29a621a61","trusted":true}},{"cell_type":"code","source":"df_p[df_p['playerName']=='Mike Trout']","metadata":{"_uuid":"da5dbafd-0a68-481c-9547-12ee0da9aac1","_cell_guid":"4b9941dd-2ee7-4b82-8657-c934f9594d90","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:58:36.760204Z","iopub.execute_input":"2022-12-13T16:58:36.760710Z","iopub.status.idle":"2022-12-13T16:58:36.784265Z","shell.execute_reply.started":"2022-12-13T16:58:36.760644Z","shell.execute_reply":"2022-12-13T16:58:36.783045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Mike Trout은 USA 선수, 1991년생, 20세인 2011년에 MLB 데뷔\n\n2. 출신 국가별 팔로워 수 비교","metadata":{"_uuid":"e3b17d72-1adc-4346-8729-66ad9f77bf1b","_cell_guid":"69f99a5b-97ee-447f-a1f2-388232ffdbd8","trusted":true}},{"cell_type":"code","source":"df_p['followers'] = followers['playerTwitterFollowers']\ndf_p","metadata":{"_uuid":"e2744c8e-8fb1-492b-a0d0-3bc5d8b170cd","_cell_guid":"b8bd1970-6289-4875-8bdf-2b0423956608","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:58:36.786022Z","iopub.execute_input":"2022-12-13T16:58:36.786604Z","iopub.status.idle":"2022-12-13T16:58:36.827854Z","shell.execute_reply.started":"2022-12-13T16:58:36.786552Z","shell.execute_reply":"2022-12-13T16:58:36.826808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_p.loc[df_p['followers'].idxmax()]","metadata":{"_uuid":"382b1afd-c982-4f6f-bcdb-cb496712f5e1","_cell_guid":"ec67655e-05e1-4123-8d34-6fe38a8a3286","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:58:36.829397Z","iopub.execute_input":"2022-12-13T16:58:36.829780Z","iopub.status.idle":"2022-12-13T16:58:36.838800Z","shell.execute_reply.started":"2022-12-13T16:58:36.829740Z","shell.execute_reply":"2022-12-13T16:58:36.837947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_p.sort_values(by='followers', ascending=False)","metadata":{"_uuid":"39900175-a31f-4d08-9ad0-cd13abc9df82","_cell_guid":"2280cc28-b6df-4d96-b0b9-122bb0a959f9","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:58:36.840248Z","iopub.execute_input":"2022-12-13T16:58:36.840569Z","iopub.status.idle":"2022-12-13T16:58:36.884750Z","shell.execute_reply.started":"2022-12-13T16:58:36.840537Z","shell.execute_reply":"2022-12-13T16:58:36.883491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#선수명, 출신국가, 팔로워 column만 보이게 정리\nf_sum = df_p[df_p.columns[df_p.columns.isin(['playerName', 'birthCountry', 'followers'])]]\n\n#출신 국가별 가장 높은 팔로워수로 정리 후 내림차\nf_sum2 = f_sum.groupby('birthCountry').agg({'followers':'max'})\nf_sum2.sort_values(by=['followers'], ascending = False)","metadata":{"_uuid":"1c84083c-27e7-4ff2-94d3-68829eb50620","_cell_guid":"b8d07f50-0b0f-47b7-b502-1282daccba23","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-13T16:58:36.886763Z","iopub.execute_input":"2022-12-13T16:58:36.887203Z","iopub.status.idle":"2022-12-13T16:58:36.907187Z","shell.execute_reply.started":"2022-12-13T16:58:36.887156Z","shell.execute_reply":"2022-12-13T16:58:36.906050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"2. 베네수엘라 선수가 가장 높은 팔로워 수를 보유하고 있음","metadata":{"_uuid":"f782e459-52c5-48b4-beda-ef925b023207","_cell_guid":"78e308bf-50c4-4dfa-afd7-7694bf16192a","trusted":true}},{"cell_type":"markdown","source":"# **Summary**\n\n- 기존 데이터를 탐색해보며 선수의 수상, 출신 국가, MLB 평균 데뷔 나이 등을 확인함\n\n- 기존 코드들을 활용해 궁금한 점들을 확인함\n\n1) 상을 많이 받은 선수들의 팔로워 수 비교: 상을 많이 받으면 이름이 알려지는만큼 팔로워수도 높을 것이다 --> 평균보다 많은 팔로워를 보유함\n\n2) 출신 국가별 팔로워 수 비교: 1번에서 상도 많이 받고 팔로워 수도 높은 선수의 출신 국가 선수가 많은 팔로워를 보유할 것이다 --> 두번째로 많은 팔로워 보유\n\n\n--> 발표 후 피드백을 토대로 상관관계를 분석해보고자 하였지만 완벽하게 분석하지 못하였습니다. 2.3 award와 twitter follower 분석 파트에서 corr()부분에 작성해두었지만 향후 보충해 업데이트를 해야할 것 같습니다. 출신국가와 팔로워수의 상관관계도 확인해볼 수 있는 방법에 대해서도 계속 찾아보고 고민해보겠습니다.\n         \n\n* **Future development**\n    \n    1. 베테랑 선수 vs. 어린 스타플레이어의 인기 척도 비교를 통한 신인 선수들의 인기, engagement가 높아지기 위한 feature 탐색\n    \n    2. 팬들이 중시하는 포지션별 feature 탐색을 통한 MLB 인기 상승 방안 모색\n    \n    3. 선수가 그 전날 잘했다면 그 다음날에도 엔트리(로스터)에 등록될지, 꾸준히 잘하는 모습을 보여준다면 팔로워도 증가할지에 대한 분석을 통한 예측 모델 구축","metadata":{"_uuid":"90a85f0e-4146-4590-b06a-19e7edcf9a6c","_cell_guid":"37b5736a-3beb-4d1e-bf98-59ed9aa4a078","trusted":true}},{"cell_type":"markdown","source":"# Module Submission\n\nAccording to this competitions' evaluation category, \"The mlb module writes the submission.csv file automatically, based on the predictions you make each for each date in the test set. To use the module, follow the this template in Kaggle Notebooks:\"","metadata":{}},{"cell_type":"code","source":"import mlb\nenv = mlb.make_env() # initialize the environment\niter_test = env.iter_test() # iterator which loops over each date in test set\n\nfor (test_df, sample_prediction_df) in iter_test:\n    sample_prediction_df['target1'] = 100 #make predictions here\n    env.predict(sample_prediction_df)","metadata":{"execution":{"iopub.status.busy":"2022-12-13T17:00:43.061849Z","iopub.execute_input":"2022-12-13T17:00:43.062244Z","iopub.status.idle":"2022-12-13T17:00:43.098294Z","shell.execute_reply.started":"2022-12-13T17:00:43.062212Z","shell.execute_reply":"2022-12-13T17:00:43.097047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# References\n\n1. https://www.youtube.com/watch?v=Xd79vC-ZDvk\n\n2. 원본 노트북: https://www.kaggle.com/code/datajmcn/mlb-player-digital-engagement-forecasting-eda \n\n3. word cloud: https://ssoonidev.tistory.com/tag/Python%20Wordcloud https://blog.naver.com/PostView.naver?blogId=pino93&logNo=221998227204\n\n4. idxmax() 참고: https://pinkwink.kr/1134\n\n5. twitter 데이터 추출 코드 참고: https://www.kaggle.com/code/alokpattani/mlb-player-digital-engagement-data-exploration/notebook#Look-at-Relationships-Between-Various-Factors-and-Target-Variables\n\n6. corr() 참고: https://hong-yp-ml-records.tistory.com/33","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}