{"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 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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-09T08:18:48.037890Z","iopub.execute_input":"2023-11-09T08:18:48.038353Z","iopub.status.idle":"2023-11-09T08:18:48.467743Z","shell.execute_reply.started":"2023-11-09T08:18:48.038315Z","shell.execute_reply":"2023-11-09T08:18:48.466058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data=pd.read_parquet('/kaggle/input/child-mind-institute-detect-sleep-states/train_series.parquet')\ntrain_data","metadata":{"execution":{"iopub.status.busy":"2023-11-09T08:19:01.615306Z","iopub.execute_input":"2023-11-09T08:19:01.615847Z","iopub.status.idle":"2023-11-09T08:19:54.090974Z","shell.execute_reply.started":"2023-11-09T08:19:01.615816Z","shell.execute_reply":"2023-11-09T08:19:54.089985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_series=pd.read_parquet('/kaggle/input/child-mind-institute-detect-sleep-states/train_series.parquet')\ntest_series","metadata":{"execution":{"iopub.status.busy":"2023-11-09T08:20:26.723496Z","iopub.execute_input":"2023-11-09T08:20:26.723932Z","iopub.status.idle":"2023-11-09T08:21:11.426656Z","shell.execute_reply.started":"2023-11-09T08:20:26.723897Z","shell.execute_reply":"2023-11-09T08:21:11.425507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_events=pd.read_csv('/kaggle/input/child-mind-institute-detect-sleep-states/train_events.csv')\ntrain_events","metadata":{"execution":{"iopub.status.busy":"2023-11-09T08:21:42.705629Z","iopub.execute_input":"2023-11-09T08:21:42.706047Z","iopub.status.idle":"2023-11-09T08:21:42.750246Z","shell.execute_reply.started":"2023-11-09T08:21:42.706015Z","shell.execute_reply":"2023-11-09T08:21:42.748922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub=pd.read_csv('/kaggle/input/child-mind-institute-detect-sleep-states/sample_submission.csv')\nsample_sub\n","metadata":{"execution":{"iopub.status.busy":"2023-11-09T08:21:53.866861Z","iopub.execute_input":"2023-11-09T08:21:53.867311Z","iopub.status.idle":"2023-11-09T08:21:53.887655Z","shell.execute_reply.started":"2023-11-09T08:21:53.867274Z","shell.execute_reply":"2023-11-09T08:21:53.886114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import polars as pl\nimport datetime\n\ndef show_df(df, num=3, tail=True):\n    print(df.shape)\n    display(df.head(num))\n    if tail:\n        display(df.tail(num))\n\nevents = pl.read_csv(\"/kaggle/input/child-mind-institute-detect-sleep-states/train_events.csv\")\nevents = events.with_columns(\n    pl.col(\"timestamp\").apply(lambda x: x[-5:]).alias(\"timezone_offset\"),\n    pl.col(\"timestamp\").apply(lambda x: datetime.datetime.strptime(str(x), '%Y-%m-%dT%H:%M:%S%z')).alias(\"timestamp\"),\n)\n\nshow_df(events)","metadata":{"execution":{"iopub.status.busy":"2023-11-09T08:24:47.836754Z","iopub.execute_input":"2023-11-09T08:24:47.837979Z","iopub.status.idle":"2023-11-09T08:24:48.083946Z","shell.execute_reply.started":"2023-11-09T08:24:47.837929Z","shell.execute_reply":"2023-11-09T08:24:48.083216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"events = events.with_columns(\n    events['timestamp'].apply(lambda x: x.year).alias(\"year\"),\n    events['timestamp'].apply(lambda x: x.month).alias(\"month\"),\n    events['timestamp'].apply(lambda x: x.day).alias(\"day\"),\n    events['timestamp'].apply(lambda x: x.hour).alias(\"hour\"),\n    events['timestamp'].apply(lambda x: x.minute).alias(\"minute\"),\n    events['timestamp'].apply(lambda x: x.second).alias(\"second\"),\n)\n\nshow_df(events)","metadata":{"execution":{"iopub.status.busy":"2023-11-09T08:31:44.736236Z","iopub.execute_input":"2023-11-09T08:31:44.736639Z","iopub.status.idle":"2023-11-09T08:31:44.858762Z","shell.execute_reply.started":"2023-11-09T08:31:44.736607Z","shell.execute_reply":"2023-11-09T08:31:44.857259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib import pyplot as plt\n\ndef show_hist(df:pd.DataFrame, col:str, title:str=None, xlim:list=[None,None], bins=20)->None:\n    \"\"\"show histgram\n\n    Args:\n        df (pl.DataFrame): input data\n        col (str): show column\n        title (str, optional): title. Defaults to None.\n        xlim (list, optional): range of x-axis. Defaults to [None,None].\n    \"\"\"\n    fig = plt.figure(figsize=[20,4])\n    tmp = df[col].to_numpy()\n    plt.hist(tmp, alpha=0.5, range=(xlim[0], xlim[1]), bins=bins)  # 範囲とビンの数を指定\n    plt.title(title) # カラム名をタイトルとして設定\n    plt.xlabel(col)\n    plt.xlim(xlim) ","metadata":{"execution":{"iopub.status.busy":"2023-11-09T08:31:50.429673Z","iopub.execute_input":"2023-11-09T08:31:50.430039Z","iopub.status.idle":"2023-11-09T08:31:50.436968Z","shell.execute_reply.started":"2023-11-09T08:31:50.430009Z","shell.execute_reply":"2023-11-09T08:31:50.435833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# hour distribution\nfor _event in [\"wakeup\", \"onset\"]:\n    show_hist(events.filter(pl.col(\"event\")==_event), \"hour\", f\"{_event} of hour distribution\", [0,24], 24)","metadata":{"execution":{"iopub.status.busy":"2023-11-09T08:31:55.282894Z","iopub.execute_input":"2023-11-09T08:31:55.283344Z","iopub.status.idle":"2023-11-09T08:31:55.999080Z","shell.execute_reply.started":"2023-11-09T08:31:55.283307Z","shell.execute_reply":"2023-11-09T08:31:55.997899Z"},"trusted":true},"execution_count":null,"outputs":[]}]}