{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"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-09-14T05:25:14.959031Z","iopub.execute_input":"2023-09-14T05:25:14.959616Z","iopub.status.idle":"2023-09-14T05:25:14.969311Z","shell.execute_reply.started":"2023-09-14T05:25:14.959583Z","shell.execute_reply":"2023-09-14T05:25:14.967968Z"},"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\")\n\ntrain_events.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-14T05:25:14.971439Z","iopub.execute_input":"2023-09-14T05:25:14.971841Z","iopub.status.idle":"2023-09-14T05:25:15.051837Z","shell.execute_reply.started":"2023-09-14T05:25:14.971811Z","shell.execute_reply":"2023-09-14T05:25:15.050521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pyarrow.parquet as pa\ndf = pa.read_table(\"/kaggle/input/child-mind-institute-detect-sleep-states/train_series.parquet\")\n\ntrain_series = df.to_pandas()\ntrain_series.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-14T05:25:15.054353Z","iopub.execute_input":"2023-09-14T05:25:15.055614Z","iopub.status.idle":"2023-09-14T05:26:13.743579Z","shell.execute_reply.started":"2023-09-14T05:25:15.055568Z","shell.execute_reply":"2023-09-14T05:26:13.742394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = train_events.head(400).merge(train_series.head(100), on='series_id')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-14T05:27:05.548205Z","iopub.execute_input":"2023-09-14T05:27:05.548646Z","iopub.status.idle":"2023-09-14T05:27:05.572659Z","shell.execute_reply.started":"2023-09-14T05:27:05.548608Z","shell.execute_reply":"2023-09-14T05:27:05.571819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import LogisticRegression\n\n\n# Extract the anglez and enmo columns\nanglez = df[\"anglez\"]\nenmo = df[\"enmo\"]\nevent = df[\"event\"]\n# Create a new dataframe called \"sleep_df\" that contains the anglez and enmo columns\nsleep_df = pd.DataFrame({\"anglez\": anglez, \"enmo\": enmo, \"event\": event})\n\n# Split the sleep_df dataframe into a training set and a test set\ntrain_set, test_set = train_test_split(sleep_df, test_size=0.2)\n\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train model to predict sleep event based on the anglez and enmo columns\nmodel = LogisticRegression()\nmodel.fit(train_set[[\"anglez\", \"enmo\"]], train_set[\"event\"])","metadata":{"execution":{"iopub.status.busy":"2023-09-14T05:36:12.849134Z","iopub.execute_input":"2023-09-14T05:36:12.849565Z","iopub.status.idle":"2023-09-14T05:36:12.907844Z","shell.execute_reply.started":"2023-09-14T05:36:12.849534Z","shell.execute_reply":"2023-09-14T05:36:12.906449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predict sleep event on the test set\npredictions = model.predict(test_set[[\"anglez\", \"enmo\"]])\n\naccuracy = np.mean(predictions == test_set[\"event\"])\nprint(\"Accuracy:\", accuracy)","metadata":{"execution":{"iopub.status.busy":"2023-09-14T05:37:23.360367Z","iopub.execute_input":"2023-09-14T05:37:23.360805Z","iopub.status.idle":"2023-09-14T05:37:23.375337Z","shell.execute_reply.started":"2023-09-14T05:37:23.360772Z","shell.execute_reply":"2023-09-14T05:37:23.373737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nplt.title(\"Model Accuracy Curve\")\nplt.plot(accuracy, color=\"blue\", linewidth=2)\nplt.legend([\"Accuracy\"])\nplt.grid(True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-14T05:39:38.020414Z","iopub.execute_input":"2023-09-14T05:39:38.020875Z","iopub.status.idle":"2023-09-14T05:39:38.342613Z","shell.execute_reply.started":"2023-09-14T05:39:38.020842Z","shell.execute_reply":"2023-09-14T05:39:38.341435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}],"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"}}