{"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":"markdown","source":"# First Base Random Forrest","metadata":{}},{"cell_type":"code","source":"# Get list of series\nimport os\nimport sys\nimport joblib\nimport pandas as pd\nimport numpy as np\nimport seaborn as sns\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.ensemble import RandomForestClassifier\n\nfrom sklearn.metrics import (\n    mean_squared_error,\n    # make_scorer,\n    r2_score,\n    classification_report,\n    confusion_matrix,\n    # mean_squared_log_error,\n    # precision_recall_curve,\n    roc_auc_score,\n    average_precision_score,\n    ConfusionMatrixDisplay,\n)\n\n# import os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\npd.set_option(\"mode.chained_assignment\", None)","metadata":{"execution":{"iopub.status.busy":"2023-10-01T07:53:56.052765Z","iopub.execute_input":"2023-10-01T07:53:56.053273Z","iopub.status.idle":"2023-10-01T07:53:58.459392Z","shell.execute_reply.started":"2023-10-01T07:53:56.053242Z","shell.execute_reply":"2023-10-01T07:53:58.457760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def binary_eval(y_true, y_pred=None, model=None, predictor=None, thrs=0.5):\n    \"\"\"\n    [TODO] To output evaluation of binary model\n\n    Args:\n        predictor (pandas):\n        model (string) : column name for target value\n        predictor (array of string) : name of features to be calculated\n\n    Returns:\n        auc_pr, auc_roc\n\n    \"\"\"\n\n    y_val = y_true.copy()\n\n    if model is not None:\n        if predictor is not None:\n            y_val_pred = model.predict(predictor)\n        else:\n            print(\"Insert Data for Model\")\n            return\n    elif y_pred is not None:\n        y_val_pred = y_pred.copy()\n    else:\n        print(\"Insert Model or Target Prediction\")\n        return\n\n    y_val_pred2 = y_val_pred.reshape(-1)\n    y_val_label = (y_val_pred2 > thrs).astype(int)\n\n    print(classification_report(y_val, y_val_label))\n\n    cmatrix = confusion_matrix(y_val, y_val_label)\n    # print(cmatrix)\n    cmatrix = (cmatrix.transpose() / cmatrix.sum(axis=1)).transpose()\n    # sns.heatmap((cmatrix.transpose() / cmatrix.sum(axis=1)).transpose(), annot=True)\n    display = ConfusionMatrixDisplay(cmatrix)\n    display.plot()\n\n    auc_pr_val = round(average_precision_score(y_val, y_val_pred2), 4)\n    auc_roc_val = round(roc_auc_score(y_val, y_val_pred2), 4)\n\n    print(\"aucpr : \", auc_pr_val)\n    print(\"aucroc : \", auc_roc_val)\n\n    return auc_pr_val, auc_roc_val","metadata":{"execution":{"iopub.status.busy":"2023-10-01T07:53:58.461720Z","iopub.execute_input":"2023-10-01T07:53:58.462205Z","iopub.status.idle":"2023-10-01T07:53:58.470741Z","shell.execute_reply.started":"2023-10-01T07:53:58.462175Z","shell.execute_reply":"2023-10-01T07:53:58.469349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p25 = lambda x : np.percentile(x,25)\np75 = lambda x : np.percentile(x,75)\nmeasure_fun = ['mean','median','max','min',p25,p75]\nmeasure_names = ['mean','median','max','min','p25','p75']\n\ndef feat_eng_dc1(df, is_test=False):\n    new_cols = ['series_id','date','hour'] + ['anglez_' + i for i in measure_names] + ['enmo_' + i for i in measure_names]\n    df['timestamp'] = pd.to_datetime(df['timestamp'].apply(lambda x: x[:-5]))\n\n    df.loc[:,'hour'] = df.loc[:,'timestamp'].dt.hour\n    \n    df['date'] = df['timestamp'].dt.date\n    if is_test:\n        df_agg = df.groupby(['series_id','date','hour']).agg({'anglez':measure_fun, \n                                                    'enmo':measure_fun,\n                                                    'series_id':np.size}).reset_index()\n        new_cols = new_cols+ ['total_steps']\n    else:\n\n        df_agg = df.groupby(['series_id','date','hour']).agg({'anglez':measure_fun, \n                                                    'enmo':measure_fun,\n                                                    'awake':['mean']}).reset_index()\n        new_cols = new_cols+ ['awake_ratio1']\n\n\n    df_agg.columns = new_cols\n    df_agg = df_agg.reset_index()\n    df_agg.loc[:,'steps'] = df_agg['index']\n    del df_agg['index']\n\n    col_name = df_agg.columns\n    col_name1 = [i for i in col_name if 'anglez' in i or 'enmo' in i]\n    col_name2 = [i for i in col_name if  i not in col_name1]\n    col_name_sorted = col_name2[:3] + col_name1 + col_name2[3:]\n    df_agg = df_agg[col_name_sorted]\n    return df_agg","metadata":{"execution":{"iopub.status.busy":"2023-10-01T07:53:58.472553Z","iopub.execute_input":"2023-10-01T07:53:58.472858Z","iopub.status.idle":"2023-10-01T07:53:58.496711Z","shell.execute_reply.started":"2023-10-01T07:53:58.472831Z","shell.execute_reply":"2023-10-01T07:53:58.495105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nPATH_BASE = '/kaggle/input/v0-hb-rf-dreamcatcher/'\nPATH_BASE2 = '/kaggle/input/child-mind-institute-detect-sleep-states/'\ntrain_series = pd.read_csv(PATH_BASE + 'train_series.csv')['0'].values\nuri_data = PATH_BASE + \"base_train_set-v0.parquet\"\nuri_test = PATH_BASE2 +\"test_series.parquet\"","metadata":{"execution":{"iopub.status.busy":"2023-10-01T07:53:58.498516Z","iopub.execute_input":"2023-10-01T07:53:58.498808Z","iopub.status.idle":"2023-10-01T07:53:58.536222Z","shell.execute_reply.started":"2023-10-01T07:53:58.498784Z","shell.execute_reply":"2023-10-01T07:53:58.535117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Prep","metadata":{}},{"cell_type":"code","source":"df_all = pd.read_parquet(uri_data)\ndf_all = df_all.dropna()","metadata":{"execution":{"iopub.status.busy":"2023-10-01T07:53:58.537511Z","iopub.execute_input":"2023-10-01T07:53:58.537798Z","iopub.status.idle":"2023-10-01T07:53:59.050181Z","shell.execute_reply.started":"2023-10-01T07:53:58.537771Z","shell.execute_reply":"2023-10-01T07:53:59.048854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = df_all[df_all['series_id'].isin(train_series)]\ndf_test = df_all[~(df_all['series_id'].isin(train_series))]\n\ncol_used = ['hour', 'anglez_mean', 'anglez_median',\n       'anglez_max', 'anglez_min', 'anglez_p25', 'anglez_p75', 'enmo_mean',\n       'enmo_median', 'enmo_max', 'enmo_min', 'enmo_p25', 'enmo_p75'\n       ]\ncol_target = ['awake']","metadata":{"execution":{"iopub.status.busy":"2023-10-01T07:53:59.051411Z","iopub.execute_input":"2023-10-01T07:53:59.051831Z","iopub.status.idle":"2023-10-01T07:53:59.101797Z","shell.execute_reply.started":"2023-10-01T07:53:59.051801Z","shell.execute_reply":"2023-10-01T07:53:59.100545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-01T07:53:59.103076Z","iopub.execute_input":"2023-10-01T07:53:59.103436Z","iopub.status.idle":"2023-10-01T07:53:59.110765Z","shell.execute_reply.started":"2023-10-01T07:53:59.103405Z","shell.execute_reply":"2023-10-01T07:53:59.109357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-01T07:53:59.112390Z","iopub.execute_input":"2023-10-01T07:53:59.112687Z","iopub.status.idle":"2023-10-01T07:53:59.128766Z","shell.execute_reply.started":"2023-10-01T07:53:59.112662Z","shell.execute_reply":"2023-10-01T07:53:59.127418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Xtr = df_train[col_used]\nytr = df_train[col_target]\n\nXte = df_test[col_used]\nyte = df_test[col_target]","metadata":{"execution":{"iopub.status.busy":"2023-10-01T07:53:59.130773Z","iopub.execute_input":"2023-10-01T07:53:59.131480Z","iopub.status.idle":"2023-10-01T07:53:59.160765Z","shell.execute_reply.started":"2023-10-01T07:53:59.131437Z","shell.execute_reply":"2023-10-01T07:53:59.159227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ss_1 = StandardScaler()\nss_1.fit(Xtr)","metadata":{"execution":{"iopub.status.busy":"2023-10-01T07:53:59.163935Z","iopub.execute_input":"2023-10-01T07:53:59.164228Z","iopub.status.idle":"2023-10-01T07:53:59.187169Z","shell.execute_reply.started":"2023-10-01T07:53:59.164201Z","shell.execute_reply":"2023-10-01T07:53:59.186386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Xtr1 = ss_1.transform(Xtr)\nXte1 = ss_1.transform(Xte)","metadata":{"execution":{"iopub.status.busy":"2023-10-01T07:53:59.187998Z","iopub.execute_input":"2023-10-01T07:53:59.188714Z","iopub.status.idle":"2023-10-01T07:53:59.204401Z","shell.execute_reply.started":"2023-10-01T07:53:59.188690Z","shell.execute_reply":"2023-10-01T07:53:59.202842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Training","metadata":{}},{"cell_type":"code","source":"rf_model = RandomForestClassifier(n_estimators=150, max_depth=8, min_samples_split=10)","metadata":{"execution":{"iopub.status.busy":"2023-10-01T07:53:59.206116Z","iopub.execute_input":"2023-10-01T07:53:59.206425Z","iopub.status.idle":"2023-10-01T07:53:59.210730Z","shell.execute_reply.started":"2023-10-01T07:53:59.206399Z","shell.execute_reply":"2023-10-01T07:53:59.209674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rf_model.fit(Xtr1,ytr)","metadata":{"execution":{"iopub.status.busy":"2023-10-01T07:53:59.212074Z","iopub.execute_input":"2023-10-01T07:53:59.212415Z","iopub.status.idle":"2023-10-01T07:54:24.366942Z","shell.execute_reply.started":"2023-10-01T07:53:59.212388Z","shell.execute_reply":"2023-10-01T07:54:24.365890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.loc[:,'y_pred'] = rf_model.predict_proba(Xtr1)[:,1]\ndf_test.loc[:,'y_pred'] = rf_model.predict_proba(Xte1)[:,1]","metadata":{"execution":{"iopub.status.busy":"2023-10-01T07:54:24.367968Z","iopub.execute_input":"2023-10-01T07:54:24.368208Z","iopub.status.idle":"2023-10-01T07:54:26.021547Z","shell.execute_reply.started":"2023-10-01T07:54:24.368187Z","shell.execute_reply":"2023-10-01T07:54:26.020226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Eval","metadata":{}},{"cell_type":"code","source":"binary_eval(df_train['awake'].values,df_train['y_pred'].values)","metadata":{"execution":{"iopub.status.busy":"2023-10-01T07:54:26.022521Z","iopub.execute_input":"2023-10-01T07:54:26.022765Z","iopub.status.idle":"2023-10-01T07:54:26.354182Z","shell.execute_reply.started":"2023-10-01T07:54:26.022744Z","shell.execute_reply":"2023-10-01T07:54:26.353374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"binary_eval(df_test['awake'].values,df_test['y_pred'].values)","metadata":{"execution":{"iopub.status.busy":"2023-10-01T07:54:26.355343Z","iopub.execute_input":"2023-10-01T07:54:26.356092Z","iopub.status.idle":"2023-10-01T07:54:26.620060Z","shell.execute_reply.started":"2023-10-01T07:54:26.356060Z","shell.execute_reply":"2023-10-01T07:54:26.619292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test['awake'].mean()","metadata":{"execution":{"iopub.status.busy":"2023-10-01T07:54:26.621112Z","iopub.execute_input":"2023-10-01T07:54:26.621866Z","iopub.status.idle":"2023-10-01T07:54:26.628099Z","shell.execute_reply.started":"2023-10-01T07:54:26.621837Z","shell.execute_reply":"2023-10-01T07:54:26.627319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Xtr1.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-01T07:54:26.629297Z","iopub.execute_input":"2023-10-01T07:54:26.630069Z","iopub.status.idle":"2023-10-01T07:54:26.644136Z","shell.execute_reply.started":"2023-10-01T07:54:26.630042Z","shell.execute_reply":"2023-10-01T07:54:26.643324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Features\n# sns.barplot(x=col_used, \n#        y=rf_model.feature_importances_,\n#        title='Random forest feature importances'\n#       )\npd.Series(rf_model.feature_importances_,index=col_used).sort_values().plot(kind='barh')","metadata":{"execution":{"iopub.status.busy":"2023-10-01T07:54:26.645389Z","iopub.execute_input":"2023-10-01T07:54:26.645884Z","iopub.status.idle":"2023-10-01T07:54:26.851093Z","shell.execute_reply.started":"2023-10-01T07:54:26.645855Z","shell.execute_reply":"2023-10-01T07:54:26.850322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Run for the test","metadata":{}},{"cell_type":"code","source":"df_test_raw = pd.read_parquet(uri_test)\n# df_test_submit = pd.read_parquet(PATH_BASE + 'base-feat_eng-v0_test.parquet')\nall_df_test = []\nunique_ser_test = df_test_raw.series_id.unique()\nfor ser in unique_ser_test:\n    df_temp = df_test_raw[df_test_raw.series_id==ser]\n    df_temp_agg = feat_eng_dc1(df_temp, is_test=True)\n    all_df_test.append(df_temp_agg)\n\ndf_test_submit = pd.concat(all_df_test,ignore_index=True)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-10-01T07:54:26.852236Z","iopub.execute_input":"2023-10-01T07:54:26.852704Z","iopub.status.idle":"2023-10-01T07:54:26.927936Z","shell.execute_reply.started":"2023-10-01T07:54:26.852678Z","shell.execute_reply":"2023-10-01T07:54:26.926573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test_submit.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-01T07:54:26.928950Z","iopub.execute_input":"2023-10-01T07:54:26.930072Z","iopub.status.idle":"2023-10-01T07:54:26.951870Z","shell.execute_reply.started":"2023-10-01T07:54:26.930030Z","shell.execute_reply":"2023-10-01T07:54:26.950442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Xs = df_test_submit[col_used]\nXs = ss_1.transform(Xs)","metadata":{"execution":{"iopub.status.busy":"2023-10-01T07:54:26.953845Z","iopub.execute_input":"2023-10-01T07:54:26.954147Z","iopub.status.idle":"2023-10-01T07:54:26.961333Z","shell.execute_reply.started":"2023-10-01T07:54:26.954121Z","shell.execute_reply":"2023-10-01T07:54:26.960346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test_submit['pred'] = rf_model.predict_proba(Xs)[:,1]\n# score_ser = dict(zip(unique_ser_test,pred))\n# score_ser\ndf_test_submit2 = df_test_submit[['series_id','date','hour','total_steps','pred']]","metadata":{"execution":{"iopub.status.busy":"2023-10-01T07:54:26.962220Z","iopub.execute_input":"2023-10-01T07:54:26.963330Z","iopub.status.idle":"2023-10-01T07:54:26.992429Z","shell.execute_reply.started":"2023-10-01T07:54:26.963272Z","shell.execute_reply":"2023-10-01T07:54:26.991070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test_submit2","metadata":{"execution":{"iopub.status.busy":"2023-10-01T07:54:26.993817Z","iopub.execute_input":"2023-10-01T07:54:26.994179Z","iopub.status.idle":"2023-10-01T07:54:27.009659Z","shell.execute_reply.started":"2023-10-01T07:54:26.994147Z","shell.execute_reply":"2023-10-01T07:54:27.008653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"0.4*0.7","metadata":{"execution":{"iopub.status.busy":"2023-10-01T07:54:27.010440Z","iopub.execute_input":"2023-10-01T07:54:27.010687Z","iopub.status.idle":"2023-10-01T07:54:27.026046Z","shell.execute_reply.started":"2023-10-01T07:54:27.010664Z","shell.execute_reply":"2023-10-01T07:54:27.025315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_test_event = []\nfor ser in unique_ser_test :\n    df_temp = df_test_submit2[df_test_submit2.series_id == ser]\n    df_temp['awake_steps'] = (df_temp['total_steps'] * df_temp['pred']).astype(int)\n    df_temp['onset_steps'] = df_temp['total_steps'] - df_temp['awake_steps']\n    \n    awake_step = df_temp['awake_steps'].sum()\n    onset_step = df_temp['onset_steps'].sum()\n    \n    awake_score = df_temp['pred'].product()\n#     onset_score = (1-df_temp['pred']).product()\n    \n    wake_event = [ser,awake_step,'wakeup', awake_score]\n    onset_event = [ser,onset_step,'onset', awake_score]\n    all_test_event += [wake_event, onset_event]\n\n    ","metadata":{"execution":{"iopub.status.busy":"2023-10-01T07:56:04.975738Z","iopub.execute_input":"2023-10-01T07:56:04.976061Z","iopub.status.idle":"2023-10-01T07:56:04.990137Z","shell.execute_reply.started":"2023-10-01T07:56:04.976036Z","shell.execute_reply":"2023-10-01T07:56:04.988811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_cols = ['row_id','series_id','step','event','score']\ndf_submission = pd.DataFrame(all_test_event, columns=submission_cols[1:])\ndf_submission['row_id'] = df_submission.index\ndf_submission[submission_cols].to_csv('submission.csv', index=False)\n# df_submission\n","metadata":{"execution":{"iopub.status.busy":"2023-10-01T07:56:10.239685Z","iopub.execute_input":"2023-10-01T07:56:10.240005Z","iopub.status.idle":"2023-10-01T07:56:10.250700Z","shell.execute_reply.started":"2023-10-01T07:56:10.239977Z","shell.execute_reply":"2023-10-01T07:56:10.249520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}}]}