{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":53666,"databundleVersionId":6589269,"sourceType":"competition"}],"dockerImageVersionId":30587,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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-28T17:19:04.845959Z","iopub.execute_input":"2023-11-28T17:19:04.846715Z","iopub.status.idle":"2023-11-28T17:19:05.232295Z","shell.execute_reply.started":"2023-11-28T17:19:04.846677Z","shell.execute_reply":"2023-11-28T17:19:05.231282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')\nimport pandas as pd\nfrom sklearn.model_selection import KFold\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.metrics import accuracy_score\n","metadata":{"execution":{"iopub.status.busy":"2023-11-28T17:19:05.234228Z","iopub.execute_input":"2023-11-28T17:19:05.234639Z","iopub.status.idle":"2023-11-28T17:19:05.677361Z","shell.execute_reply.started":"2023-11-28T17:19:05.234611Z","shell.execute_reply":"2023-11-28T17:19:05.676327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load data\ntrain_series = pd.read_parquet('/kaggle/input/child-mind-institute-detect-sleep-states/train_series.parquet')  # Replace 'train_series.csv' with the actual file path\ntrain_events = pd.read_csv('/kaggle/input/child-mind-institute-detect-sleep-states/train_events.csv')  # Replace 'train_events.csv' with the actual file path\ntest_set = pd.read_parquet('/kaggle/input/child-mind-institute-detect-sleep-states/test_series.parquet')","metadata":{"execution":{"iopub.status.busy":"2023-11-28T17:19:05.679032Z","iopub.execute_input":"2023-11-28T17:19:05.679338Z","iopub.status.idle":"2023-11-28T17:19:45.689119Z","shell.execute_reply.started":"2023-11-28T17:19:05.679312Z","shell.execute_reply":"2023-11-28T17:19:45.687938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_series","metadata":{"execution":{"iopub.status.busy":"2023-11-28T17:19:45.692089Z","iopub.execute_input":"2023-11-28T17:19:45.692427Z","iopub.status.idle":"2023-11-28T17:19:45.711296Z","shell.execute_reply.started":"2023-11-28T17:19:45.692399Z","shell.execute_reply":"2023-11-28T17:19:45.710242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_events","metadata":{"execution":{"iopub.status.busy":"2023-11-28T17:19:45.712537Z","iopub.execute_input":"2023-11-28T17:19:45.712827Z","iopub.status.idle":"2023-11-28T17:19:45.730424Z","shell.execute_reply.started":"2023-11-28T17:19:45.712802Z","shell.execute_reply":"2023-11-28T17:19:45.729468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_set","metadata":{"execution":{"iopub.status.busy":"2023-11-28T17:19:45.731514Z","iopub.execute_input":"2023-11-28T17:19:45.731812Z","iopub.status.idle":"2023-11-28T17:19:45.749593Z","shell.execute_reply.started":"2023-11-28T17:19:45.731778Z","shell.execute_reply":"2023-11-28T17:19:45.748684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = pd.read_csv('/kaggle/input/child-mind-institute-detect-sleep-states/sample_submission.csv')\nsample_submission","metadata":{"execution":{"iopub.status.busy":"2023-11-28T17:19:45.750806Z","iopub.execute_input":"2023-11-28T17:19:45.751189Z","iopub.status.idle":"2023-11-28T17:19:45.770736Z","shell.execute_reply.started":"2023-11-28T17:19:45.751152Z","shell.execute_reply":"2023-11-28T17:19:45.769882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# รวมข้อมูล train_series และ train_events\ntrain_data = pd.merge(train_series, train_events, on=['series_id', 'step'])\n\n# ตรวจสอบข้อมูลหลังจากรวม\nprint(train_data.head())\n","metadata":{"execution":{"iopub.status.busy":"2023-11-28T17:19:45.771818Z","iopub.execute_input":"2023-11-28T17:19:45.772189Z","iopub.status.idle":"2023-11-28T17:20:45.511813Z","shell.execute_reply.started":"2023-11-28T17:19:45.772155Z","shell.execute_reply":"2023-11-28T17:20:45.510770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# แปลงค่า 'event' เป็น binary\ntrain_data['event'] = train_data['event'].map({'onset': 1, 'wakeup': 0})\n\n# ตรวจสอบข้อมูลหลังจากแปลง\nprint(train_data.head())","metadata":{"execution":{"iopub.status.busy":"2023-11-28T17:20:45.513152Z","iopub.execute_input":"2023-11-28T17:20:45.513518Z","iopub.status.idle":"2023-11-28T17:20:45.524346Z","shell.execute_reply.started":"2023-11-28T17:20:45.513489Z","shell.execute_reply":"2023-11-28T17:20:45.523247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_and_predict(train_data, test_set):\n    features = ['anglez', 'enmo']\n    target = 'event'\n    \n    submission = pd.DataFrame(columns=['row_id', 'series_id', 'step', 'event', 'score'])\n\n    # ใช้ KFold สำหรับการทำ k-fold validation\n    kf = KFold(n_splits=5, shuffle=True, random_state=42)\n\n    for fold, (train_idx, val_idx) in enumerate(kf.split(train_data)):\n        train_fold, val_fold = train_data.iloc[train_idx], train_data.iloc[val_idx]\n\n        # สร้างและ train model\n        model = LogisticRegression()\n        model.fit(train_fold[features], train_fold[target])\n\n        # ทำนายบนชุด validation\n        val_preds = model.predict(val_fold[features])\n\n        # ประเมินความแม่นยำ\n        accuracy = accuracy_score(val_fold[target], val_preds)\n        print(f'Fold {fold + 1}, Accuracy: {accuracy}')\n\n        # ทำนายบน test set\n        test_preds = model.predict(test_set[features])\n\n        # ตรวจสอบว่ามีค่า event เป็น 1 (onset) หรือ 0 (wakeup) ใน series_id เดียวกันหรือไม่\n        unique_series = test_set['series_id'].unique()\n        for series_id in unique_series:\n            subset = test_set[test_set['series_id'] == series_id]\n            onset_pred = 1 in test_preds[subset.index]\n            wakeup_pred = 0 in test_preds[subset.index]\n\n            # เพิ่มผลลัพธ์ลงใน submission\n            submission = pd.concat([submission, pd.DataFrame({\n                'row_id': [subset.index[0], subset.index[1]],\n                'series_id': [series_id, series_id],\n                'step': [100, 105] if onset_pred else [105, 100],\n                'event': ['onset', 'wakeup'] if onset_pred else ['wakeup', 'onset'],\n                'score': [1.0, 0.0] if onset_pred else [0.0, 1.0]\n            })])\n\n    return submission\n\n# ทำการทดสอบ\nsubmission = train_and_predict(train_data, test_set)\nprint(submission)\n","metadata":{"execution":{"iopub.status.busy":"2023-11-28T17:20:45.528505Z","iopub.execute_input":"2023-11-28T17:20:45.528842Z","iopub.status.idle":"2023-11-28T17:20:45.971978Z","shell.execute_reply.started":"2023-11-28T17:20:45.528816Z","shell.execute_reply":"2023-11-28T17:20:45.970582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2023-11-28T17:20:45.974173Z","iopub.execute_input":"2023-11-28T17:20:45.975083Z","iopub.status.idle":"2023-11-28T17:20:46.009630Z","shell.execute_reply.started":"2023-11-28T17:20:45.975028Z","shell.execute_reply":"2023-11-28T17:20:46.008412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def select_highest_scoring_pair(df):\n        # Filter only 'onset' and 'wakeup' events\n        filtered_df = df[df['event'].isin(['onset', 'wakeup'])]\n\n        # Select the highest-scoring 'onset' event for each series_id\n        onset_df = filtered_df[filtered_df['event'] == 'onset']\n        onset_idx = onset_df.groupby('series_id')['score'].transform(max) == onset_df['score']\n        selected_onset_df = onset_df[onset_idx]\n\n        # Select the highest-scoring 'wakeup' event for each series_id\n        wakeup_df = filtered_df[filtered_df['event'] == 'wakeup']\n        wakeup_idx = wakeup_df.groupby('series_id')['score'].transform(max) == wakeup_df['score']\n        selected_wakeup_df = wakeup_df[wakeup_idx]\n\n        # Merge the selected 'onset' and 'wakeup' events\n        selected_df = pd.concat([selected_onset_df, selected_wakeup_df])\n\n        selected_df = selected_df.sort_values(by=['series_id', 'step']).reset_index(drop=True)\n\n        return selected_df","metadata":{"execution":{"iopub.status.busy":"2023-11-28T17:20:46.011523Z","iopub.execute_input":"2023-11-28T17:20:46.012283Z","iopub.status.idle":"2023-11-28T17:20:46.026858Z","shell.execute_reply.started":"2023-11-28T17:20:46.012239Z","shell.execute_reply":"2023-11-28T17:20:46.025361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = select_highest_scoring_pair(submission)","metadata":{"execution":{"iopub.status.busy":"2023-11-28T17:20:46.033557Z","iopub.execute_input":"2023-11-28T17:20:46.035039Z","iopub.status.idle":"2023-11-28T17:20:46.054481Z","shell.execute_reply.started":"2023-11-28T17:20:46.034968Z","shell.execute_reply":"2023-11-28T17:20:46.053187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2023-11-28T17:20:46.056733Z","iopub.execute_input":"2023-11-28T17:20:46.057719Z","iopub.status.idle":"2023-11-28T17:20:46.084169Z","shell.execute_reply.started":"2023-11-28T17:20:46.057667Z","shell.execute_reply":"2023-11-28T17:20:46.083158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = submission.drop_duplicates(subset=['series_id', 'step'])","metadata":{"execution":{"iopub.status.busy":"2023-11-28T17:20:46.085478Z","iopub.execute_input":"2023-11-28T17:20:46.085877Z","iopub.status.idle":"2023-11-28T17:20:46.092363Z","shell.execute_reply.started":"2023-11-28T17:20:46.085840Z","shell.execute_reply":"2023-11-28T17:20:46.091272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = submission.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-11-28T17:20:46.093443Z","iopub.execute_input":"2023-11-28T17:20:46.093734Z","iopub.status.idle":"2023-11-28T17:20:46.102781Z","shell.execute_reply.started":"2023-11-28T17:20:46.093707Z","shell.execute_reply":"2023-11-28T17:20:46.101552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = submission[['row_id', 'series_id', 'step', 'event','score']]","metadata":{"execution":{"iopub.status.busy":"2023-11-28T17:20:46.104021Z","iopub.execute_input":"2023-11-28T17:20:46.104341Z","iopub.status.idle":"2023-11-28T17:20:46.114869Z","shell.execute_reply.started":"2023-11-28T17:20:46.104313Z","shell.execute_reply":"2023-11-28T17:20:46.114042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['row_id'] = range(len(submission))","metadata":{"execution":{"iopub.status.busy":"2023-11-28T17:20:46.116160Z","iopub.execute_input":"2023-11-28T17:20:46.116486Z","iopub.status.idle":"2023-11-28T17:20:46.126794Z","shell.execute_reply.started":"2023-11-28T17:20:46.116460Z","shell.execute_reply":"2023-11-28T17:20:46.125906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2023-11-28T17:20:46.127946Z","iopub.execute_input":"2023-11-28T17:20:46.130400Z","iopub.status.idle":"2023-11-28T17:20:46.143096Z","shell.execute_reply.started":"2023-11-28T17:20:46.130365Z","shell.execute_reply":"2023-11-28T17:20:46.142023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-11-28T17:22:02.043211Z","iopub.execute_input":"2023-11-28T17:22:02.044124Z","iopub.status.idle":"2023-11-28T17:22:02.051640Z","shell.execute_reply.started":"2023-11-28T17:22:02.044089Z","shell.execute_reply":"2023-11-28T17:22:02.050740Z"},"trusted":true},"execution_count":null,"outputs":[]}]}