{"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"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":53666,"databundleVersionId":6589269,"sourceType":"competition"}],"dockerImageVersionId":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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-21T06:48:28.93754Z","iopub.execute_input":"2023-11-21T06:48:28.938001Z","iopub.status.idle":"2023-11-21T06:48:29.814267Z","shell.execute_reply.started":"2023-11-21T06:48:28.937962Z","shell.execute_reply":"2023-11-21T06:48:29.812049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install pyarrow","metadata":{"execution":{"iopub.status.busy":"2023-11-21T06:48:32.112915Z","iopub.execute_input":"2023-11-21T06:48:32.113548Z","iopub.status.idle":"2023-11-21T06:48:47.747355Z","shell.execute_reply.started":"2023-11-21T06:48:32.113506Z","shell.execute_reply":"2023-11-21T06:48:47.746032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_events = pd.read_csv(\"/kaggle/input/child-mind-institute-detect-sleep-states/train_events.csv\")\ndf_train_events","metadata":{"execution":{"iopub.status.busy":"2023-11-21T06:49:39.227526Z","iopub.execute_input":"2023-11-21T06:49:39.228085Z","iopub.status.idle":"2023-11-21T06:49:39.271599Z","shell.execute_reply.started":"2023-11-21T06:49:39.228041Z","shell.execute_reply":"2023-11-21T06:49:39.270235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_events['step'].fillna('no_data', inplace=True)\ndf_train_events.rename(columns={'step': 'event_step'}, inplace=True)\ndf_train_events","metadata":{"execution":{"iopub.status.busy":"2023-11-21T07:00:28.084132Z","iopub.execute_input":"2023-11-21T07:00:28.084611Z","iopub.status.idle":"2023-11-21T07:00:28.107435Z","shell.execute_reply.started":"2023-11-21T07:00:28.084574Z","shell.execute_reply":"2023-11-21T07:00:28.106227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pyarrow.parquet as pq\n\n\ntable = pq.read_table(\"/kaggle/input/child-mind-institute-detect-sleep-states/train_series.parquet\")\ndf_train_series = table.to_pandas()\ndf_train_series\n\n","metadata":{"execution":{"iopub.status.busy":"2023-11-21T06:51:32.008198Z","iopub.execute_input":"2023-11-21T06:51:32.00868Z","iopub.status.idle":"2023-11-21T06:53:03.069792Z","shell.execute_reply.started":"2023-11-21T06:51:32.008641Z","shell.execute_reply":"2023-11-21T06:53:03.068527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 假設 df_train_series 和 df_cleaned 是你的兩個 DataFrame\n\n# 將 'event' 列合併到 df_train_series\ndf_train_series_merged = pd.merge(df_train_series, df_train_events[['series_id', 'event_step', 'event','timestamp']], on=['series_id', 'timestamp'], how='left')\n\n# 將沒有匹配到的 'event' 列的 NaN 值填為 'neither'\ndf_train_series_merged['event'].fillna('neither', inplace=True)\ndf_train_series_merged['step'].fillna('neither', inplace=True)\ndf_train_series_merged['event_step'].fillna('neither', inplace=True)\ndf_train_series_merged['timestamp'].fillna('neither', inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-11-21T07:04:28.00595Z","iopub.execute_input":"2023-11-21T07:04:28.007138Z","iopub.status.idle":"2023-11-21T07:08:38.063785Z","shell.execute_reply.started":"2023-11-21T07:04:28.007093Z","shell.execute_reply":"2023-11-21T07:08:38.06231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert the DataFrame to CSV\ndf_train_series_merged.to_csv('df_train.csv', index=False)\ndf_train_series_merged","metadata":{"execution":{"iopub.status.busy":"2023-11-21T07:10:42.277031Z","iopub.execute_input":"2023-11-21T07:10:42.277489Z","iopub.status.idle":"2023-11-21T07:26:56.681958Z","shell.execute_reply.started":"2023-11-21T07:10:42.277449Z","shell.execute_reply":"2023-11-21T07:26:56.681054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pyarrow.parquet as pq\n\n\ntable = pq.read_table(\"/kaggle/input/child-mind-institute-detect-sleep-states/test_series.parquet\")\ndf_test_series = table.to_pandas()\ndf_test_series","metadata":{"execution":{"iopub.status.busy":"2023-11-21T06:06:48.486025Z","iopub.execute_input":"2023-11-21T06:06:48.486325Z","iopub.status.idle":"2023-11-21T06:06:48.527566Z","shell.execute_reply.started":"2023-11-21T06:06:48.486299Z","shell.execute_reply":"2023-11-21T06:06:48.526638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.svm import SVC\nfrom sklearn.metrics import accuracy_score\nimport pandas as pd\n\nimport pandas as pd\n\n# 假設 df_train_series 和 df_cleaned 是你的兩個 DataFrame\n\n# 將 'event' 列合併到 df_train_series\ndf_train_series_merged = pd.merge(df_train_series, df_train_events_cleaned[['series_id', 'step', 'event']], on=['series_id', 'step'], how='left')\n\n# 將沒有匹配到的 'event' 列的 NaN 值填為 'neither'\ndf_train_series_merged['event'].fillna('neither', inplace=True)\n\n# 顯示處理後的 DataFrame\ndf_train_series_merged\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-11-21T06:06:48.529868Z","iopub.execute_input":"2023-11-21T06:06:48.530201Z","iopub.status.idle":"2023-11-21T06:08:26.82566Z","shell.execute_reply.started":"2023-11-21T06:06:48.530174Z","shell.execute_reply":"2023-11-21T06:08:26.824601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 選擇特徵和目標變數\nX = df_train_series_merged[['anglez', 'enmo']]\ny = df_train_series_merged['event']\n\n# 分割數據為訓練集和測試集\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n\n# 創建SVM模型\nsvm_model = SVC(kernel='linear')\n\n# 訓練模型\nsvm_model.fit(X_train, y_train)\n\n# 預測\ny_pred = svm_model.predict(X_test)\n\n# 評估模型性能\naccuracy = accuracy_score(y_test, y_pred)\nprint(f'Accuracy: {accuracy}')","metadata":{"execution":{"iopub.status.busy":"2023-11-21T06:08:59.840724Z","iopub.execute_input":"2023-11-21T06:08:59.841152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 預測新數據\nnew_data = df_test_series[['anglez', 'enmo']]\npredicted_events = svm_model.predict(new_data)\n\ndf_test_series['event'] = predicted_events","metadata":{"execution":{"iopub.status.busy":"2023-11-21T06:08:27.332678Z","iopub.status.idle":"2023-11-21T06:08:27.333167Z","shell.execute_reply.started":"2023-11-21T06:08:27.332953Z","shell.execute_reply":"2023-11-21T06:08:27.332974Z"},"trusted":true},"execution_count":null,"outputs":[]}]}