{"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)\nimport polars as pl\nimport datetime\nfrom matplotlib import pyplot as plt\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-09T15:32:21.431821Z","iopub.execute_input":"2023-11-09T15:32:21.432201Z","iopub.status.idle":"2023-11-09T15:32:22.038035Z","shell.execute_reply.started":"2023-11-09T15:32:21.432169Z","shell.execute_reply":"2023-11-09T15:32:22.037034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#df_submission = pd.read_csv('/kaggle/input/child-mind-institute-detect-sleep-states/sample_submission.csv')\n#df_pq_test = pd.read_parquet('/kaggle/input/child-mind-institute-detect-sleep-states/test_series.parquet')\n#df_pq_train = pd.read_parquet('/kaggle/input/child-mind-institute-detect-sleep-states/train_series.parquet')\ndf_train_events = pl.read_csv(\"/kaggle/input/child-mind-institute-detect-sleep-states/train_events.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-11-09T15:32:27.496505Z","iopub.execute_input":"2023-11-09T15:32:27.497006Z","iopub.status.idle":"2023-11-09T15:32:27.660739Z","shell.execute_reply.started":"2023-11-09T15:32:27.496975Z","shell.execute_reply":"2023-11-09T15:32:27.659337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_events = df_train_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)","metadata":{"execution":{"iopub.status.busy":"2023-11-09T15:32:31.156541Z","iopub.execute_input":"2023-11-09T15:32:31.156934Z","iopub.status.idle":"2023-11-09T15:32:31.389790Z","shell.execute_reply.started":"2023-11-09T15:32:31.156904Z","shell.execute_reply":"2023-11-09T15:32:31.389006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Analyzing data in night column","metadata":{}},{"cell_type":"code","source":"df_train_events['night'].describe()","metadata":{"execution":{"iopub.status.busy":"2023-11-09T15:32:35.701402Z","iopub.execute_input":"2023-11-09T15:32:35.701779Z","iopub.status.idle":"2023-11-09T15:32:35.733960Z","shell.execute_reply.started":"2023-11-09T15:32:35.701748Z","shell.execute_reply":"2023-11-09T15:32:35.732836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=[20,10])\ndata = df_train_events[\"night\"].to_numpy()\nhist, edges = np.histogram(data, bins=20)\nfreq = hist / sum(hist)\ncolors = plt.cm.rainbow(freq)\nplt.bar(edges[:-1], hist, width=np.diff(edges), color=colors, alpha=0.7)\nplt.title(\"Nights vs Sensor Count\")\nplt.xlabel(\"Nights\")\nplt.ylabel(\"Number of Sensors\")\nplt.xlim([0,100]) ","metadata":{"execution":{"iopub.status.busy":"2023-11-09T15:32:39.771241Z","iopub.execute_input":"2023-11-09T15:32:39.771606Z","iopub.status.idle":"2023-11-09T15:32:40.206990Z","shell.execute_reply.started":"2023-11-09T15:32:39.771555Z","shell.execute_reply":"2023-11-09T15:32:40.206051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"hour distribution","metadata":{}},{"cell_type":"code","source":"def show_df(df, num=3, tail=True):\n    print(df.shape)\n    display(df.head(num))\n    if tail:\n        display(df.tail(num))\nshow_df(df_train_events)","metadata":{"execution":{"iopub.status.busy":"2023-11-09T15:33:37.191624Z","iopub.execute_input":"2023-11-09T15:33:37.191996Z","iopub.status.idle":"2023-11-09T15:33:37.205245Z","shell.execute_reply.started":"2023-11-09T15:33:37.191967Z","shell.execute_reply":"2023-11-09T15:33:37.204131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_events = df_train_events.with_columns(\n    df_train_events['timestamp'].apply(lambda x: x.year).alias(\"year\"),\n    df_train_events['timestamp'].apply(lambda x: x.month).alias(\"month\"),\n    df_train_events['timestamp'].apply(lambda x: x.day).alias(\"day\"),\n    df_train_events['timestamp'].apply(lambda x: x.hour).alias(\"hour\"),\n    df_train_events['timestamp'].apply(lambda x: x.minute).alias(\"minute\"),\n    df_train_events['timestamp'].apply(lambda x: x.second).alias(\"second\"),\n)\n\nshow_df(df_train_events)","metadata":{"execution":{"iopub.status.busy":"2023-11-09T15:34:54.061394Z","iopub.execute_input":"2023-11-09T15:34:54.061776Z","iopub.status.idle":"2023-11-09T15:34:54.170897Z","shell.execute_reply.started":"2023-11-09T15:34:54.061747Z","shell.execute_reply":"2023-11-09T15:34:54.169745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def 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-09T15:35:35.291634Z","iopub.execute_input":"2023-11-09T15:35:35.292008Z","iopub.status.idle":"2023-11-09T15:35:35.298956Z","shell.execute_reply.started":"2023-11-09T15:35:35.291977Z","shell.execute_reply":"2023-11-09T15:35:35.297818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# hour distribution\nfor _event in [\"wakeup\", \"onset\"]:\n    show_hist(df_train_events.filter(pl.col(\"event\")==_event), \"hour\", f\"{_event} of hour distribution\", [0,24], 24)","metadata":{"execution":{"iopub.status.busy":"2023-11-09T15:36:31.466900Z","iopub.execute_input":"2023-11-09T15:36:31.467271Z","iopub.status.idle":"2023-11-09T15:36:32.136040Z","shell.execute_reply.started":"2023-11-09T15:36:31.467243Z","shell.execute_reply":"2023-11-09T15:36:32.134661Z"},"trusted":true},"execution_count":null,"outputs":[]}]}