{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport matplotlib.pyplot as plt\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session\nTARGETS = ('target1', 'target2', 'target3', 'target4')\nnull = 'null'","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-30T07:50:26.671508Z","iopub.execute_input":"2021-06-30T07:50:26.671908Z","iopub.status.idle":"2021-06-30T07:50:26.689905Z","shell.execute_reply.started":"2021-06-30T07:50:26.671864Z","shell.execute_reply":"2021-06-30T07:50:26.688676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"../input/mlb-player-digital-engagement-forecasting/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-06-30T07:50:28.978124Z","iopub.execute_input":"2021-06-30T07:50:28.978678Z","iopub.status.idle":"2021-06-30T07:51:57.381279Z","shell.execute_reply.started":"2021-06-30T07:50:28.978611Z","shell.execute_reply":"2021-06-30T07:51:57.379859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"first_day_df = pd.DataFrame(eval(train_df.iloc[0]['nextDayPlayerEngagement']))\nplayer_id_top_player_t1 = first_day_df.iloc[first_day_df['target1'].idxmax()]['playerId']\nplayer_id_random = first_day_df.iloc[801]['playerId']","metadata":{"execution":{"iopub.status.busy":"2021-06-30T07:51:57.384263Z","iopub.execute_input":"2021-06-30T07:51:57.384750Z","iopub.status.idle":"2021-06-30T07:51:57.483683Z","shell.execute_reply.started":"2021-06-30T07:51:57.384699Z","shell.execute_reply":"2021-06-30T07:51:57.482735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import datetime\ndfs = []\nfor i in range(len(train_df)):\n    row = train_df.iloc[i]\n    date = datetime.datetime.strptime(str(row['date']), '%Y%m%d')\n    day_df = pd.DataFrame(eval(row['nextDayPlayerEngagement']))\n    dfs.append(day_df)\nengs = pd.concat(dfs)","metadata":{"execution":{"iopub.status.busy":"2021-06-30T07:51:57.485462Z","iopub.execute_input":"2021-06-30T07:51:57.485983Z","iopub.status.idle":"2021-06-30T07:53:15.476773Z","shell.execute_reply.started":"2021-06-30T07:51:57.485908Z","shell.execute_reply":"2021-06-30T07:53:15.475611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"engs = pd.concat(dfs)","metadata":{"execution":{"iopub.status.busy":"2021-06-30T07:53:15.478488Z","iopub.execute_input":"2021-06-30T07:53:15.479013Z","iopub.status.idle":"2021-06-30T07:53:15.962874Z","shell.execute_reply.started":"2021-06-30T07:53:15.478971Z","shell.execute_reply":"2021-06-30T07:53:15.961983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"players = set(engs['playerId'])\naverages = {}\nfor playerId in players:\n    averages[playerId] = engs[engs['playerId'] == playerId].mean()","metadata":{"execution":{"iopub.status.busy":"2021-06-30T07:53:15.964220Z","iopub.execute_input":"2021-06-30T07:53:15.964717Z","iopub.status.idle":"2021-06-30T07:53:30.218664Z","shell.execute_reply.started":"2021-06-30T07:53:15.964666Z","shell.execute_reply":"2021-06-30T07:53:30.217441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_history(playeriD):\n    history = {}\n    for i in range(len(train_df)):\n        row = train_df.iloc[i]\n        date = datetime.datetime.strptime(str(row['date']), '%Y%m%d')\n        day_df = pd.DataFrame(eval(row['nextDayPlayerEngagement']))\n        top_player_row = day_df[day_df['playerId'] == playeriD]\n        history[date] = [float(top_player_row[t])/day_df['target1'].sum() for t in TARGETS]\n    return history","metadata":{"execution":{"iopub.status.busy":"2021-06-30T07:53:46.006317Z","iopub.execute_input":"2021-06-30T07:53:46.006752Z","iopub.status.idle":"2021-06-30T07:53:46.014279Z","shell.execute_reply.started":"2021-06-30T07:53:46.006713Z","shell.execute_reply":"2021-06-30T07:53:46.013276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = get_history(player_id_random)\nplt.plot(sorted(history.keys()), [history[d][0] for d in sorted(history.keys())][:100])\nplt.plot(sorted(history.keys()), [history[d][1] for d in sorted(history.keys())][:100])\nplt.plot(sorted(history.keys()), [history[d][2] for d in sorted(history.keys())][:100])\nplt.plot(sorted(history.keys()), [history[d][3] for d in sorted(history.keys())][:100])","metadata":{"execution":{"iopub.status.busy":"2021-06-30T07:53:46.984179Z","iopub.execute_input":"2021-06-30T07:53:46.984564Z","iopub.status.idle":"2021-06-30T07:55:05.676012Z","shell.execute_reply.started":"2021-06-30T07:53:46.984529Z","shell.execute_reply":"2021-06-30T07:55:05.674354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = get_history(player_id_top_player_t1)","metadata":{"execution":{"iopub.status.busy":"2021-06-30T07:59:34.721476Z","iopub.execute_input":"2021-06-30T07:59:34.721838Z","iopub.status.idle":"2021-06-30T08:00:54.828655Z","shell.execute_reply.started":"2021-06-30T07:59:34.721803Z","shell.execute_reply":"2021-06-30T08:00:54.827427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"range_ = slice(0, 50)\nplt.plot(sorted(history.keys())[range_], [history[d][0] for d in sorted(history.keys())[range_]])\nplt.plot(sorted(history.keys())[range_], [history[d][1] for d in sorted(history.keys())[range_]])\nplt.plot(sorted(history.keys())[range_], [history[d][2] for d in sorted(history.keys())[range_]])\nplt.plot(sorted(history.keys())[range_], [history[d][3] for d in sorted(history.keys())[range_]])","metadata":{"execution":{"iopub.status.busy":"2021-06-30T07:59:01.552650Z","iopub.execute_input":"2021-06-30T07:59:01.553073Z","iopub.status.idle":"2021-06-30T07:59:01.762389Z","shell.execute_reply.started":"2021-06-30T07:59:01.553039Z","shell.execute_reply":"2021-06-30T07:59:01.761502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_random = get_history(player_id_random)","metadata":{"execution":{"iopub.status.busy":"2021-06-30T08:01:17.945967Z","iopub.execute_input":"2021-06-30T08:01:17.946369Z","iopub.status.idle":"2021-06-30T08:02:40.338174Z","shell.execute_reply.started":"2021-06-30T08:01:17.946338Z","shell.execute_reply":"2021-06-30T08:02:40.336923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"range_ = slice(0, 50)\nhistory = history_random\nplt.plot(sorted(history.keys())[range_], [history[d][0] for d in sorted(history.keys())[range_]])\nplt.plot(sorted(history.keys())[range_], [history[d][1] for d in sorted(history.keys())[range_]])\nplt.plot(sorted(history.keys())[range_], [history[d][2] for d in sorted(history.keys())[range_]])\nplt.plot(sorted(history.keys())[range_], [history[d][3] for d in sorted(history.keys())[range_]])","metadata":{"execution":{"iopub.status.busy":"2021-06-30T08:09:33.502257Z","iopub.execute_input":"2021-06-30T08:09:33.502706Z","iopub.status.idle":"2021-06-30T08:09:33.718863Z","shell.execute_reply.started":"2021-06-30T08:09:33.502671Z","shell.execute_reply":"2021-06-30T08:09:33.717435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}