{"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-10-30T06:11:43.034505Z","iopub.execute_input":"2023-10-30T06:11:43.034885Z","iopub.status.idle":"2023-10-30T06:11:43.354898Z","shell.execute_reply.started":"2023-10-30T06:11:43.034852Z","shell.execute_reply":"2023-10-30T06:11:43.353872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport os\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom matplotlib.pyplot import subplot\nimport seaborn as sns\nfrom sklearn import datasets\nfrom sklearn.model_selection import train_test_split\nfrom sklearn import svm","metadata":{"execution":{"iopub.status.busy":"2023-10-30T06:12:46.772124Z","iopub.execute_input":"2023-10-30T06:12:46.772594Z","iopub.status.idle":"2023-10-30T06:12:47.842684Z","shell.execute_reply.started":"2023-10-30T06:12:46.772566Z","shell.execute_reply":"2023-10-30T06:12:47.841037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"train_series = pd.read_parquet('/kaggle/input/child-mind-institute-detect-sleep-states/train_series.parquet')\ntest_series = pd.read_parquet('/kaggle/input/child-mind-institute-detect-sleep-states/test_series.parquet')\ntrain_events = pd.read_csv('/kaggle/input/child-mind-institute-detect-sleep-states/train_events.csv')","metadata":{"execution":{"iopub.status.busy":"2023-10-30T06:18:38.513434Z","iopub.execute_input":"2023-10-30T06:18:38.513932Z","iopub.status.idle":"2023-10-30T06:19:32.236429Z","shell.execute_reply.started":"2023-10-30T06:18:38.513904Z","shell.execute_reply":"2023-10-30T06:19:32.235021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# get the data from the first series_id\nt1 = train_series.query(\"series_id == '038441c925bb'\")\nt1","metadata":{"execution":{"iopub.status.busy":"2023-10-30T06:33:43.490606Z","iopub.execute_input":"2023-10-30T06:33:43.491000Z","iopub.status.idle":"2023-10-30T06:33:45.824235Z","shell.execute_reply.started":"2023-10-30T06:33:43.490969Z","shell.execute_reply":"2023-10-30T06:33:45.823072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train feature : delta_angle\ntest = []\ntemp = 0\nfor angle in t1['anglez']:\n    test.append(angle - temp)\n    temp = angle\n    \ndelta_angle_df = pd.DataFrame(test, columns=['delta_angle'])\n\ndelta_angle_df","metadata":{"execution":{"iopub.status.busy":"2023-10-30T06:37:09.465412Z","iopub.execute_input":"2023-10-30T06:37:09.465798Z","iopub.status.idle":"2023-10-30T06:37:09.611526Z","shell.execute_reply.started":"2023-10-30T06:37:09.465766Z","shell.execute_reply":"2023-10-30T06:37:09.610404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# conbine t1 and delta_angle_df\nt1 = pd.concat([t1,delta_angle_df], axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:31:54.032833Z","iopub.execute_input":"2023-10-30T07:31:54.033232Z","iopub.status.idle":"2023-10-30T07:31:54.048536Z","shell.execute_reply.started":"2023-10-30T07:31:54.033202Z","shell.execute_reply":"2023-10-30T07:31:54.046430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test feature : delta_angle\ntest_submit = []\ntemp = 0\nfor angle in test_series['anglez']:\n    test_submit.append(angle - temp)\n    temp = angle\n    \ndelta_angle_test_df = pd.DataFrame(test_submit, columns=['delta_angle'])\n\ndelta_angle_test_df","metadata":{"execution":{"iopub.status.busy":"2023-10-30T06:42:50.011597Z","iopub.execute_input":"2023-10-30T06:42:50.012008Z","iopub.status.idle":"2023-10-30T06:42:50.026724Z","shell.execute_reply.started":"2023-10-30T06:42:50.011974Z","shell.execute_reply":"2023-10-30T06:42:50.025251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# conbine test_series and delta_angle_test_df\ntest_series = pd.concat([test_series,delta_angle_test_df], axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:24:25.463332Z","iopub.execute_input":"2023-10-30T07:24:25.463740Z","iopub.status.idle":"2023-10-30T07:24:25.471368Z","shell.execute_reply.started":"2023-10-30T07:24:25.463709Z","shell.execute_reply":"2023-10-30T07:24:25.469498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_series","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:38:42.807453Z","iopub.execute_input":"2023-10-30T07:38:42.807872Z","iopub.status.idle":"2023-10-30T07:38:42.830388Z","shell.execute_reply.started":"2023-10-30T07:38:42.807842Z","shell.execute_reply":"2023-10-30T07:38:42.828827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train feature : weighted_angle\nweighted_angle = []\nfor x in t1['step'] :\n    weighted_angle.append(t1['enmo'][x] * t1['delta_angle'][x])\n\nweighted_angle_df = pd.DataFrame(weighted_angle, columns=['weighted_angle'])\nweighted_angle_df","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:36:08.995550Z","iopub.execute_input":"2023-10-30T07:36:08.996039Z","iopub.status.idle":"2023-10-30T07:36:14.091690Z","shell.execute_reply.started":"2023-10-30T07:36:08.996006Z","shell.execute_reply":"2023-10-30T07:36:14.090711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test feature : weighted_angle\nweighted_angle = []\nfor x in test_series['step'] :\n    weighted_angle.append(test_series['enmo'][x] * test_series['delta_angle'][x])\n\nweighted_angle_test_df = pd.DataFrame(weighted_angle, columns=['weighted_angle'])\nweighted_angle_test_df","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:39:57.940169Z","iopub.execute_input":"2023-10-30T07:39:57.940656Z","iopub.status.idle":"2023-10-30T07:39:57.964804Z","shell.execute_reply.started":"2023-10-30T07:39:57.940618Z","shell.execute_reply":"2023-10-30T07:39:57.963639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# conbine weighted_angle\nt1 = pd.concat([t1,weighted_angle_df], axis=1)\ntest_series = pd.concat([test_series,weighted_angle_test_df], axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:41:59.725569Z","iopub.execute_input":"2023-10-30T07:41:59.725988Z","iopub.status.idle":"2023-10-30T07:41:59.744621Z","shell.execute_reply.started":"2023-10-30T07:41:59.725922Z","shell.execute_reply":"2023-10-30T07:41:59.743172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train feature : angle_a\nangle_a = []\ntemp = 0\nfor delta_angle in t1['delta_angle']:\n    angle_a.append(delta_angle - temp)\n    temp = delta_angle\n    \nangle_a_df = pd.DataFrame(angle_a, columns=['angle_a'])\nangle_a_df","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:44:42.493396Z","iopub.execute_input":"2023-10-30T07:44:42.493802Z","iopub.status.idle":"2023-10-30T07:44:42.632881Z","shell.execute_reply.started":"2023-10-30T07:44:42.493768Z","shell.execute_reply":"2023-10-30T07:44:42.631961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test feature : angle_a\nangle_a = []\ntemp = 0\nfor delta_angle in test_series['delta_angle']:\n    angle_a.append(delta_angle - temp)\n    temp = delta_angle\n    \nangle_a_test_df = pd.DataFrame(angle_a, columns=['angle_a'])\nangle_a_test_df","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:50:47.806126Z","iopub.execute_input":"2023-10-30T07:50:47.806506Z","iopub.status.idle":"2023-10-30T07:50:47.823022Z","shell.execute_reply.started":"2023-10-30T07:50:47.806479Z","shell.execute_reply":"2023-10-30T07:50:47.821395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# conbine angle_a\nt1 = pd.concat([t1,angle_a_df], axis=1)\ntest_series = pd.concat([test_series,angle_a_test_df], axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:52:02.954700Z","iopub.execute_input":"2023-10-30T07:52:02.955123Z","iopub.status.idle":"2023-10-30T07:52:02.974232Z","shell.execute_reply.started":"2023-10-30T07:52:02.955090Z","shell.execute_reply":"2023-10-30T07:52:02.973143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"event_1 = train_events.query(\"series_id == '038441c925bb'\")\nevent_1","metadata":{"execution":{"iopub.status.busy":"2023-10-30T07:56:27.061235Z","iopub.execute_input":"2023-10-30T07:56:27.061661Z","iopub.status.idle":"2023-10-30T07:56:27.086417Z","shell.execute_reply.started":"2023-10-30T07:56:27.061631Z","shell.execute_reply":"2023-10-30T07:56:27.085020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"event_1.fillna('blank',inplace= True)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T08:25:01.486101Z","iopub.execute_input":"2023-10-30T08:25:01.486549Z","iopub.status.idle":"2023-10-30T08:25:01.495236Z","shell.execute_reply.started":"2023-10-30T08:25:01.486517Z","shell.execute_reply":"2023-10-30T08:25:01.493584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"delta_step = 0\nevent = 'wakeup'\nstate = []\ntemps = 0\nclk = -1\nfor counts in event_1['step'] :\n    if counts == 'blank':\n        counts = temps\n    delta_step = counts - temps\n    for x in range(int(delta_step)):   \n        state.append(event)    \n    temps = counts\n    clk = clk + 1\n    event = event_1['event'][clk]\n    \nstate_df = pd.DataFrame(state, columns= ['event'])\nstate_df","metadata":{"execution":{"iopub.status.busy":"2023-10-30T08:25:18.670401Z","iopub.execute_input":"2023-10-30T08:25:18.670859Z","iopub.status.idle":"2023-10-30T08:25:18.742946Z","shell.execute_reply.started":"2023-10-30T08:25:18.670831Z","shell.execute_reply":"2023-10-30T08:25:18.741676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# conbine train_series and train_event\n\nmerge = pd.concat([t1,state_df],axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T08:29:41.256809Z","iopub.execute_input":"2023-10-30T08:29:41.257214Z","iopub.status.idle":"2023-10-30T08:29:41.301730Z","shell.execute_reply.started":"2023-10-30T08:29:41.257184Z","shell.execute_reply":"2023-10-30T08:29:41.300372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"merge.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2023-10-30T08:31:05.741194Z","iopub.execute_input":"2023-10-30T08:31:05.741655Z","iopub.status.idle":"2023-10-30T08:31:05.793160Z","shell.execute_reply.started":"2023-10-30T08:31:05.741623Z","shell.execute_reply":"2023-10-30T08:31:05.791533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"merge.dropna(inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T08:33:13.903234Z","iopub.execute_input":"2023-10-30T08:33:13.903590Z","iopub.status.idle":"2023-10-30T08:33:13.976475Z","shell.execute_reply.started":"2023-10-30T08:33:13.903561Z","shell.execute_reply":"2023-10-30T08:33:13.975360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# manual drop some missing data\nedit_data = merge.drop(merge.index[219384:253704], axis=0)\nedit_data = edit_data.drop(edit_data.index[133332:167400], axis=0)\nedit_data = edit_data.drop(edit_data.index[62856:97860], axis=0)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T08:43:20.687761Z","iopub.execute_input":"2023-10-30T08:43:20.688178Z","iopub.status.idle":"2023-10-30T08:43:20.792962Z","shell.execute_reply.started":"2023-10-30T08:43:20.688149Z","shell.execute_reply":"2023-10-30T08:43:20.791515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.lineplot(data = edit_data, x= 'step', y='angle_a')\nsns.lineplot(data = edit_data, x= 'step', y='event')\n","metadata":{"execution":{"iopub.status.busy":"2023-10-30T08:43:52.361635Z","iopub.execute_input":"2023-10-30T08:43:52.362098Z","iopub.status.idle":"2023-10-30T08:43:53.828721Z","shell.execute_reply.started":"2023-10-30T08:43:52.362051Z","shell.execute_reply":"2023-10-30T08:43:53.827769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# separate training feature and target feature\ntarget = pd.DataFrame(edit_data['event'])\nfeature = edit_data.drop(columns=['series_id','step','timestamp','event'], inplace=True)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-30T08:50:24.618164Z","iopub.execute_input":"2023-10-30T08:50:24.618530Z","iopub.status.idle":"2023-10-30T08:50:25.236244Z","shell.execute_reply.started":"2023-10-30T08:50:24.618497Z","shell.execute_reply":"2023-10-30T08:50:25.234764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature = edit_data\nfeature","metadata":{"execution":{"iopub.status.busy":"2023-10-30T08:50:55.575925Z","iopub.execute_input":"2023-10-30T08:50:55.576280Z","iopub.status.idle":"2023-10-30T08:50:55.592726Z","shell.execute_reply.started":"2023-10-30T08:50:55.576255Z","shell.execute_reply":"2023-10-30T08:50:55.591573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# split training data and testing data\nX_train, X_test, y_train, y_test = train_test_split(feature,target, train_size= 0.3, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T08:51:38.333833Z","iopub.execute_input":"2023-10-30T08:51:38.334284Z","iopub.status.idle":"2023-10-30T08:51:38.382888Z","shell.execute_reply.started":"2023-10-30T08:51:38.334250Z","shell.execute_reply":"2023-10-30T08:51:38.382132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"svm_clf = svm.SVC(probability=True).fit(X_train,y_train)\nsvm_clf.score(X_test,y_test)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:09:52.640017Z","iopub.execute_input":"2023-10-30T09:09:52.640580Z","iopub.status.idle":"2023-10-30T09:31:40.497934Z","shell.execute_reply.started":"2023-10-30T09:09:52.640538Z","shell.execute_reply":"2023-10-30T09:31:40.496101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_test = test_series.drop(columns=['series_id','step','timestamp'],axis=1)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:36:20.162218Z","iopub.execute_input":"2023-10-30T09:36:20.162633Z","iopub.status.idle":"2023-10-30T09:36:20.170731Z","shell.execute_reply.started":"2023-10-30T09:36:20.162587Z","shell.execute_reply":"2023-10-30T09:36:20.169202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# do prediction\npred = svm_clf.predict(pred_test)\npred_df = pd.DataFrame(pred,columns=['event'])","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:37:54.398236Z","iopub.execute_input":"2023-10-30T09:37:54.398660Z","iopub.status.idle":"2023-10-30T09:37:55.096034Z","shell.execute_reply.started":"2023-10-30T09:37:54.398628Z","shell.execute_reply":"2023-10-30T09:37:55.094061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_df","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:38:02.602581Z","iopub.execute_input":"2023-10-30T09:38:02.603035Z","iopub.status.idle":"2023-10-30T09:38:02.616004Z","shell.execute_reply.started":"2023-10-30T09:38:02.603001Z","shell.execute_reply":"2023-10-30T09:38:02.614567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prob = svm_clf.predict_proba(pred_test)\nprob_df = pd.DataFrame(prob[:,1], columns=['score'])","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:42:38.690636Z","iopub.execute_input":"2023-10-30T09:42:38.691111Z","iopub.status.idle":"2023-10-30T09:42:39.386911Z","shell.execute_reply.started":"2023-10-30T09:42:38.691077Z","shell.execute_reply":"2023-10-30T09:42:39.385826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prob_df","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:42:49.887275Z","iopub.execute_input":"2023-10-30T09:42:49.888254Z","iopub.status.idle":"2023-10-30T09:42:49.901691Z","shell.execute_reply.started":"2023-10-30T09:42:49.888214Z","shell.execute_reply":"2023-10-30T09:42:49.899894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_series","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:43:56.713866Z","iopub.execute_input":"2023-10-30T09:43:56.714286Z","iopub.status.idle":"2023-10-30T09:43:56.733920Z","shell.execute_reply.started":"2023-10-30T09:43:56.714256Z","shell.execute_reply":"2023-10-30T09:43:56.732050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = test_series.drop(columns=['anglez','enmo','delta_angle','weighted_angle','angle_a'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:45:45.393475Z","iopub.execute_input":"2023-10-30T09:45:45.393877Z","iopub.status.idle":"2023-10-30T09:45:45.399825Z","shell.execute_reply.started":"2023-10-30T09:45:45.393847Z","shell.execute_reply":"2023-10-30T09:45:45.398966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.concat([submission, pred_df], axis=1)\nsubmission = pd.concat([submission, prob_df], axis=1)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:51:41.441185Z","iopub.execute_input":"2023-10-30T09:51:41.441646Z","iopub.status.idle":"2023-10-30T09:51:41.463806Z","shell.execute_reply.started":"2023-10-30T09:51:41.441613Z","shell.execute_reply":"2023-10-30T09:51:41.463030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = submission.reset_index(names='row_id')\n","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:59:04.442004Z","iopub.execute_input":"2023-10-30T09:59:04.442430Z","iopub.status.idle":"2023-10-30T09:59:04.476077Z","shell.execute_reply.started":"2023-10-30T09:59:04.442399Z","shell.execute_reply":"2023-10-30T09:59:04.473780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.set_index('row_id')\nsubmission","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:59:08.741314Z","iopub.execute_input":"2023-10-30T09:59:08.741713Z","iopub.status.idle":"2023-10-30T09:59:08.760502Z","shell.execute_reply.started":"2023-10-30T09:59:08.741683Z","shell.execute_reply":"2023-10-30T09:59:08.759171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# submit in csv file\nsubmission.to_csv('submission.csv', index=False)\nsubmission","metadata":{"execution":{"iopub.status.busy":"2023-10-30T10:01:46.702410Z","iopub.execute_input":"2023-10-30T10:01:46.703075Z","iopub.status.idle":"2023-10-30T10:01:46.725086Z","shell.execute_reply.started":"2023-10-30T10:01:46.703012Z","shell.execute_reply":"2023-10-30T10:01:46.724111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}