{"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)\nfrom itertools import groupby\nimport gc\nimport xgboost as xgb\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-21T19:49:40.769823Z","iopub.execute_input":"2023-09-21T19:49:40.770824Z","iopub.status.idle":"2023-09-21T19:49:41.925940Z","shell.execute_reply.started":"2023-09-21T19:49:40.770786Z","shell.execute_reply":"2023-09-21T19:49:41.924910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_events = pd.read_csv(\"/kaggle/input/child-mind-institute-detect-sleep-states/train_events.csv\")\ntrain_events.dropna(inplace = True)","metadata":{"execution":{"iopub.status.busy":"2023-09-21T19:49:42.518809Z","iopub.execute_input":"2023-09-21T19:49:42.519557Z","iopub.status.idle":"2023-09-21T19:49:42.569458Z","shell.execute_reply.started":"2023-09-21T19:49:42.519525Z","shell.execute_reply":"2023-09-21T19:49:42.568685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_series = pd.read_parquet(\"/kaggle/input/child-mind-institute-detect-sleep-states/train_series.parquet\")\ntrain_series.dropna(inplace = True)","metadata":{"execution":{"iopub.status.busy":"2023-09-21T19:49:42.955024Z","iopub.execute_input":"2023-09-21T19:49:42.955714Z","iopub.status.idle":"2023-09-21T19:51:03.810818Z","shell.execute_reply.started":"2023-09-21T19:49:42.955640Z","shell.execute_reply":"2023-09-21T19:51:03.809772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"series_sample = train_events['series_id'].sample(n=35, random_state=None)","metadata":{"execution":{"iopub.status.busy":"2023-09-22T01:25:25.712218Z","iopub.execute_input":"2023-09-22T01:25:25.712626Z","iopub.status.idle":"2023-09-22T01:25:25.719163Z","shell.execute_reply.started":"2023-09-22T01:25:25.712594Z","shell.execute_reply":"2023-09-22T01:25:25.717843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"series_sample","metadata":{"execution":{"iopub.status.busy":"2023-09-22T01:25:26.126569Z","iopub.execute_input":"2023-09-22T01:25:26.126946Z","iopub.status.idle":"2023-09-22T01:25:26.135464Z","shell.execute_reply.started":"2023-09-22T01:25:26.126914Z","shell.execute_reply":"2023-09-22T01:25:26.134217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_train_series(series):\n    train_series_ = train_series.copy()[train_series.copy()['series_id'] == series]\n    train_events_ = train_events.copy()[train_events.copy()['series_id'] == series]\n    \n    \n    train_events_[\"step\"]  = train_events_[\"step\"].astype(\"int\")\n    train_events_[\"awake\"] = train_events_[\"event\"].replace({\"onset\":1,\"wakeup\":0})\n\n    train = pd.merge(train_series_, train_events_[['step','awake']], on='step', how='left')\n    train[\"awake\"] = train[\"awake\"].bfill(axis ='rows')\n    # final section:\n    # train_events.groupby('series_id').tail(1)[\"event\"].unique()\n    # Result: the last event is always a \"wakeup\"\n    train['awake'] = train['awake'].fillna(1) # awake\n    train[\"awake\"] = train[\"awake\"].astype(\"int\")\n    return(train)","metadata":{"execution":{"iopub.status.busy":"2023-09-22T01:25:28.458927Z","iopub.execute_input":"2023-09-22T01:25:28.459356Z","iopub.status.idle":"2023-09-22T01:25:28.468454Z","shell.execute_reply.started":"2023-09-22T01:25:28.459324Z","shell.execute_reply":"2023-09-22T01:25:28.467294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clean_train_data = []\n\nfor series_id in series_sample:\n    train = get_train_series(series_id)\n    clean_train_data.append(train)","metadata":{"execution":{"iopub.status.busy":"2023-09-22T01:25:29.753959Z","iopub.execute_input":"2023-09-22T01:25:29.754335Z","iopub.status.idle":"2023-09-22T01:46:20.092274Z","shell.execute_reply.started":"2023-09-22T01:25:29.754309Z","shell.execute_reply":"2023-09-22T01:46:20.091301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clean_train_data[6].awake.unique()","metadata":{"execution":{"iopub.status.busy":"2023-09-22T01:46:20.094301Z","iopub.execute_input":"2023-09-22T01:46:20.094604Z","iopub.status.idle":"2023-09-22T01:46:20.103010Z","shell.execute_reply.started":"2023-09-22T01:46:20.094577Z","shell.execute_reply":"2023-09-22T01:46:20.102055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.concat(clean_train_data).reset_index(drop=True)\n\n#del clean_train_data\n#gc.collect()\n","metadata":{"execution":{"iopub.status.busy":"2023-09-22T01:46:20.104294Z","iopub.execute_input":"2023-09-22T01:46:20.104603Z","iopub.status.idle":"2023-09-22T01:46:24.218440Z","shell.execute_reply.started":"2023-09-22T01:46:20.104576Z","shell.execute_reply":"2023-09-22T01:46:24.217513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['awake'].unique()","metadata":{"execution":{"iopub.status.busy":"2023-09-22T01:46:24.220828Z","iopub.execute_input":"2023-09-22T01:46:24.221184Z","iopub.status.idle":"2023-09-22T01:46:24.323044Z","shell.execute_reply.started":"2023-09-22T01:46:24.221154Z","shell.execute_reply":"2023-09-22T01:46:24.321902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test  = pd.read_parquet(\"/kaggle/input/child-mind-institute-detect-sleep-states/test_series.parquet\")","metadata":{"execution":{"iopub.status.busy":"2023-09-22T01:46:24.324369Z","iopub.execute_input":"2023-09-22T01:46:24.324690Z","iopub.status.idle":"2023-09-22T01:46:24.346956Z","shell.execute_reply.started":"2023-09-22T01:46:24.324664Z","shell.execute_reply":"2023-09-22T01:46:24.346031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.series_id.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-09-22T02:31:55.124817Z","iopub.execute_input":"2023-09-22T02:31:55.125251Z","iopub.status.idle":"2023-09-22T02:31:55.135051Z","shell.execute_reply.started":"2023-09-22T02:31:55.125206Z","shell.execute_reply":"2023-09-22T02:31:55.134351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_features(df):\n    # parse the timestamp and create an \"hour\" feature\n    df['timestamp'] = pd.to_datetime(df['timestamp']).apply(lambda t: t.tz_localize(None))\n    df[\"hour\"] = df[\"timestamp\"].dt.hour\n    \n    periods = 20\n    df[\"anglez\"] = abs(df[\"anglez\"])\n    df[\"anglez_diff\"] = df.groupby('series_id')['anglez'].diff(periods=periods).fillna(method=\"bfill\").astype('float16')\n    df[\"enmo_diff\"] = df.groupby('series_id')['enmo'].diff(periods=periods).fillna(method=\"bfill\").astype('float16')\n    df[\"anglez_rolling_mean\"] = df[\"anglez\"].rolling(periods,center=True).mean().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\n    df[\"enmo_rolling_mean\"] = df[\"enmo\"].rolling(periods,center=True).mean().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\n    df[\"anglez_rolling_max\"] = df[\"anglez\"].rolling(periods,center=True).max().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\n    df[\"enmo_rolling_max\"] = df[\"enmo\"].rolling(periods,center=True).max().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\n    df[\"anglez_rolling_std\"] = df[\"anglez\"].rolling(periods,center=True).std().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\n    df[\"enmo_rolling_std\"] = df[\"enmo\"].rolling(periods,center=True).std().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\n    df[\"anglez_diff_rolling_mean\"] = df[\"anglez_diff\"].rolling(periods,center=True).mean().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\n    df[\"enmo_diff_rolling_mean\"] = df[\"enmo_diff\"].rolling(periods,center=True).mean().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\n    df[\"anglez_diff_rolling_max\"] = df[\"anglez_diff\"].rolling(periods,center=True).max().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\n    df[\"enmo_diff_rolling_max\"] = df[\"enmo_diff\"].rolling(periods,center=True).max().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\n    \n    return df\n\nfeatures = [\"hour\",\n            \"anglez\",\n            \"anglez_rolling_mean\",\n            \"anglez_rolling_max\",\n            \"anglez_rolling_std\",\n            \"anglez_diff\",\n            \"anglez_diff_rolling_mean\",\n            \"anglez_diff_rolling_max\",\n            \"enmo\",\n            \"enmo_rolling_mean\",\n            \"enmo_rolling_max\",\n            \"enmo_rolling_std\",\n            \"enmo_diff\",\n            \"enmo_diff_rolling_mean\",\n            \"enmo_diff_rolling_max\",\n           ]","metadata":{"execution":{"iopub.status.busy":"2023-09-22T01:46:24.348301Z","iopub.execute_input":"2023-09-22T01:46:24.348634Z","iopub.status.idle":"2023-09-22T01:46:24.367271Z","shell.execute_reply.started":"2023-09-22T01:46:24.348608Z","shell.execute_reply":"2023-09-22T01:46:24.366070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train   = make_features(train)\n\nX_train = train[features]\ny_train = train[\"awake\"]\n\n# save some memory\n#del train\n#gc.collect();","metadata":{"execution":{"iopub.status.busy":"2023-09-22T01:46:24.368751Z","iopub.execute_input":"2023-09-22T01:46:24.369304Z","iopub.status.idle":"2023-09-22T01:53:14.787807Z","shell.execute_reply.started":"2023-09-22T01:46:24.369274Z","shell.execute_reply":"2023-09-22T01:53:14.786853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-22T01:53:14.789286Z","iopub.execute_input":"2023-09-22T01:53:14.789682Z","iopub.status.idle":"2023-09-22T01:53:14.812810Z","shell.execute_reply.started":"2023-09-22T01:53:14.789647Z","shell.execute_reply":"2023-09-22T01:53:14.811671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-09-22T01:53:14.813848Z","iopub.execute_input":"2023-09-22T01:53:14.814732Z","iopub.status.idle":"2023-09-22T01:53:14.985477Z","shell.execute_reply.started":"2023-09-22T01:53:14.814694Z","shell.execute_reply":"2023-09-22T01:53:14.984424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\nfrom sklearn.linear_model import LogisticRegression\n\n# Create a logistic regression model with tolerance specified\nclf = LogisticRegression(\n    penalty='l1',\n    class_weight='balanced',\n    random_state=42,\n    solver='saga',\n    tol=1e-4  # Specify the tolerance value here (default is 1e-4)\n)\n\n# Fit the logistic regression model to your data\nclf.fit(X_train, y_train)\n\n# save some memory\n#del X_train, y_train\n#gc.collect();","metadata":{"execution":{"iopub.status.busy":"2023-09-22T01:53:14.990163Z","iopub.execute_input":"2023-09-22T01:53:14.990798Z","iopub.status.idle":"2023-09-22T02:25:05.991810Z","shell.execute_reply.started":"2023-09-22T01:53:14.990769Z","shell.execute_reply":"2023-09-22T02:25:05.990735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle\n\n# Save the model to the specified file using pickle in the Kaggle working directory\nmodel_file_path = '/kaggle/working/model_4.pkl'\n\nwith open(model_file_path, 'wb') as model_file:\n    pickle.dump(clf, model_file)","metadata":{"execution":{"iopub.status.busy":"2023-09-22T00:01:24.701838Z","iopub.execute_input":"2023-09-22T00:01:24.702274Z","iopub.status.idle":"2023-09-22T00:01:24.707833Z","shell.execute_reply.started":"2023-09-22T00:01:24.702242Z","shell.execute_reply":"2023-09-22T00:01:24.706861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test  = pd.read_parquet(\"/kaggle/input/child-mind-institute-detect-sleep-states/test_series.parquet\")\n\ntest  = make_features(test)\n\nX_test = test[features]\n\ntest[\"not_awake\"] = clf_1.predict_proba(X_test)[:,0]\ntest[\"awake\"]     = clf_1.predict_proba(X_test)[:,1]","metadata":{"execution":{"iopub.status.busy":"2023-09-22T02:32:57.003030Z","iopub.execute_input":"2023-09-22T02:32:57.003416Z","iopub.status.idle":"2023-09-22T02:32:57.052864Z","shell.execute_reply.started":"2023-09-22T02:32:57.003390Z","shell.execute_reply":"2023-09-22T02:32:57.051917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf_1.predict_proba(X_test)[:,1]","metadata":{"execution":{"iopub.status.busy":"2023-09-22T02:32:58.899467Z","iopub.execute_input":"2023-09-22T02:32:58.900452Z","iopub.status.idle":"2023-09-22T02:32:58.915968Z","shell.execute_reply.started":"2023-09-22T02:32:58.900418Z","shell.execute_reply":"2023-09-22T02:32:58.914646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# smoothing the predictions\nsmoothing_length = 50\ntest[\"score\"]  = test[\"awake\"].rolling(smoothing_length,center=True).mean().fillna(method=\"bfill\").fillna(method=\"ffill\")\ntest[\"smooth\"] = test[\"not_awake\"].rolling(smoothing_length,center=True).mean().fillna(method=\"bfill\").fillna(method=\"ffill\")\n# re-binarize\ntest[\"smooth\"] = test[\"smooth\"].round()\n\n# https://stackoverflow.com/questions/73777727/how-to-mark-start-end-of-a-series-of-non-null-and-non-0-values-in-a-column-of-a\ndef get_event(df):\n    lstCV = zip(df.series_id, df.smooth)\n    lstPOI = []\n    for (c, v), g in groupby(lstCV, lambda cv: \n                            (cv[0], cv[1]!=0 and not pd.isnull(cv[1]))):\n        llg = sum(1 for item in g)\n        if v is False: \n            lstPOI.extend([0]*llg)\n        else: \n            lstPOI.extend(['onset']+(llg-2)*[0]+['wakeup'] if llg > 1 else [0])\n    return lstPOI\n\ntest[\"event\"] = get_event(test)","metadata":{"execution":{"iopub.status.busy":"2023-09-22T02:33:00.342691Z","iopub.execute_input":"2023-09-22T02:33:00.343236Z","iopub.status.idle":"2023-09-22T02:33:00.357318Z","shell.execute_reply.started":"2023-09-22T02:33:00.343201Z","shell.execute_reply":"2023-09-22T02:33:00.356295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = test.loc[test[\"event\"] != 0][[\"series_id\",\"step\",\"event\",\"score\"]].copy().reset_index(drop=True).reset_index(names=\"row_id\")\nsample_submission\n\n","metadata":{"execution":{"iopub.status.busy":"2023-09-22T02:33:01.977029Z","iopub.execute_input":"2023-09-22T02:33:01.977443Z","iopub.status.idle":"2023-09-22T02:33:01.994261Z","shell.execute_reply.started":"2023-09-22T02:33:01.977407Z","shell.execute_reply":"2023-09-22T02:33:01.993044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"pd.read_csv('/kaggle/input/child-mind-institute-detect-sleep-states/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-09-22T02:32:28.323490Z","iopub.execute_input":"2023-09-22T02:32:28.323896Z","iopub.status.idle":"2023-09-22T02:32:28.747657Z","shell.execute_reply.started":"2023-09-22T02:32:28.323868Z","shell.execute_reply":"2023-09-22T02:32:28.745962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open('/kaggle/working/model_3.pkl', 'rb') as file:\n    clf_1 = pickle.load(file)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-22T02:32:43.123886Z","iopub.execute_input":"2023-09-22T02:32:43.124630Z","iopub.status.idle":"2023-09-22T02:32:43.129368Z","shell.execute_reply.started":"2023-09-22T02:32:43.124598Z","shell.execute_reply":"2023-09-22T02:32:43.128571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}