{"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"},{"sourceId":6507308,"sourceType":"datasetVersion","datasetId":3706667}],"dockerImageVersionId":30587,"isInternetEnabled":false,"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)\nfrom tqdm import tqdm\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-27T08:49:20.332132Z","iopub.execute_input":"2023-11-27T08:49:20.332650Z","iopub.status.idle":"2023-11-27T08:49:20.344614Z","shell.execute_reply.started":"2023-11-27T08:49:20.332610Z","shell.execute_reply":"2023-11-27T08:49:20.343314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_parquet(\"/kaggle/input/zzzs-lightweight-training-dataset-target/Zzzs_train.parquet\")\n\ndef make_features(df):\n    periods = 5\n    df[\"anglez\"] = abs(df[\"anglez\"])\n    df[\"anglez_diff\"] = df.groupby('series_id')['anglez'].diff(periods=periods).bfill().astype('float16')\n    df[\"enmo_diff\"] = df.groupby('series_id')['enmo'].diff(periods=periods).bfill().astype('float16')\n    \n    # Add progress bar\n    for col in tqdm([\"anglez\", \"enmo\", \"anglez_diff\", \"enmo_diff\"]):\n        df[f\"{col}_rolling_mean\"] = df[col].rolling(periods,center=True).mean().ffill().bfill().astype('float16')\n        df[f\"{col}_rolling_max\"] = df[col].rolling(periods,center=True).max().ffill().bfill().astype('float16')\n        df[f\"{col}_rolling_std\"] = df[col].rolling(periods,center=True).std().ffill().bfill().astype('float16')\n    \n#     df = df.drop(columns=['step', 'timestamp'])\n    return df\n\nfeatures = [\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           ]\n\ndf_ms = make_features(train)\ndf_ms.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:49:20.347151Z","iopub.execute_input":"2023-11-27T08:49:20.347850Z","iopub.status.idle":"2023-11-27T08:49:41.289366Z","shell.execute_reply.started":"2023-11-27T08:49:20.347805Z","shell.execute_reply":"2023-11-27T08:49:41.288243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.compose import ColumnTransformer\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.model_selection import KFold\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.metrics import roc_curve, auc, confusion_matrix\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n# 1. การเตรียมข้อมูล\ndf = df_ms.dropna(axis=0).reset_index(drop=True)\nprint('Reading Data done!')\nprint(df.head())\n\n# 2. Standardization\n# features = ['start_step', 'end_step', 'sleep_hr']\nX = df[features]\nscaler = StandardScaler()\nX_standardized = scaler.fit_transform(X)\n\n# สุ่มเลือก 100% ของข้อมูล\n# X_train, _, y_train, _ = train_test_split(X_standardized, df['awake'], train_size=1, stratify=df['awake'], random_state=42)\n\n# 3. สร้าง k-fold cross-validation\nk = 5  # ตัวอย่าง: ใช้ 5-fold cross-validation\nkf = KFold(n_splits=k, shuffle=True, random_state=42)\n\n# 4. ทำ RandomForestClassifier และประเมินผลในแต่ละ fold\nfpr_values = []\ntpr_values = []\nroc_auc_values = []\nconf_matrices = []\n\naccuracy_values = []\nprecision_values = []\nrecall_values = []\nspecificity_values = []\nf1_score_values = []\n\n# Initialize a list to store the models\nmodels = []\n\n# Initialize a list to store the performance metrics\nperformance_metrics = []\n\nfor train_idx, test_idx in tqdm(kf.split(X_standardized), total=k, desc=\"Training and Evaluating Folds\"):\n    X_train_fold, X_test_fold = X_standardized[train_idx], X_standardized[test_idx]\n    y_train_fold, y_test_fold = df['awake'][train_idx], df['awake'][test_idx]\n    \n    preprocessor = ColumnTransformer(\n        transformers=[('num', StandardScaler(), list(range(14)))],\n        remainder='passthrough'\n    )\n\n    RandomForest_model = Pipeline([\n        ('preprocessor', preprocessor),\n        ('classifier', RandomForestClassifier(n_estimators=2))\n    ])\n    \n    RandomForest_model.fit(X_train_fold, y_train_fold)\n    \n    # Append the trained model to the list\n    models.append(RandomForest_model)\n    \n    probabilities = RandomForest_model.predict_proba(X_test_fold)[:, 1]\n\n    fpr, tpr, _ = roc_curve(y_test_fold, probabilities)\n    fpr_values.append(fpr)\n    tpr_values.append(tpr)\n\n    roc_auc = auc(fpr, tpr)\n    roc_auc_values.append(roc_auc)\n\n    # สร้าง Confusion Matrix\n    y_pred = RandomForest_model.predict(X_test_fold)\n    conf_matrix = confusion_matrix(y_test_fold, y_pred)\n    conf_matrices.append(conf_matrix)\n    \n    # Calculate additional metrics\n    accuracy = (conf_matrix[0, 0] + conf_matrix[1, 1]) / np.sum(conf_matrix)\n    precision = conf_matrix[1, 1] / (conf_matrix[1, 1] + conf_matrix[0, 1])\n    recall = conf_matrix[1, 1] / (conf_matrix[1, 1] + conf_matrix[1, 0])\n    specificity = conf_matrix[0, 0] / (conf_matrix[0, 0] + conf_matrix[0, 1])\n    f1_score = 2 * (precision * recall) / (precision + recall)\n    \n    performance_metrics.append(accuracy)\n\n    # Append metrics to lists\n    accuracy_values.append(accuracy)\n    precision_values.append(precision)\n    recall_values.append(recall)\n    specificity_values.append(specificity)\n    f1_score_values.append(f1_score)\n\n\n# 5. พล็อต ROC curve และแสดง AUC ใน legend\nplt.figure(figsize=(8, 8))\nfor i in range(k):\n    plt.plot(fpr_values[i], tpr_values[i], label=f'Fold {i+1} (AUC = {roc_auc_values[i]:.5f}')\n\nplt.plot([0, 1], [0, 1], linestyle='--', color='black')  # กราฟแทนการทายแบบสุ่ม (Random Guess)\nplt.xlabel('False Positive Rate')\nplt.ylabel('True Positive Rate')\nplt.title('ROC Curve with AUC')\nplt.legend()\nplt.show()\n\n# 6. พล็อต Confusion Matrix สำหรับแต่ละ fold\nplt.figure(figsize=(12, 8))\nfor i, conf_matrix in enumerate(conf_matrices, 1):\n    plt.subplot(2, 3, i)\n    sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues', cbar=False,\n                xticklabels=['Predicted 0', 'Predicted 1'],\n                yticklabels=['Actual 0', 'Actual 1'])\n    plt.title(f'Confusion Matrix - Fold {i}')\n\nplt.tight_layout()\nplt.show()\n\n# Print average metrics\nprint(f'Average Accuracy: {np.mean(accuracy_values):.5f}')\nprint(f'Average Precision: {np.mean(precision_values):.5f}')\nprint(f'Average Recall (Sensitivity): {np.mean(recall_values):.5f}')\nprint(f'Average Specificity: {np.mean(specificity_values):.5f}')\nprint(f'Average F1 Score: {np.mean(f1_score_values):.5f}')\n\nbest_model_idx = performance_metrics.index(max(performance_metrics))\nbest_model = models[best_model_idx]","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:49:41.290963Z","iopub.execute_input":"2023-11-27T08:49:41.291290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\n\ntest  = pd.read_parquet(\"/kaggle/input/child-mind-institute-detect-sleep-states/test_series.parquet\")\ntest  = make_features(test)\nX_test = test[features]\n\n# 2. Random Forest Model\nX_test_standardized = scaler.transform(X_test)  # ตรวจสอบให้แน่ใจว่าทำการปรับมาตราส่วนข้อมูลทดสอบ\ntest[\"not_awake_rf\"] = best_model.predict_proba(X_test_standardized)[:, 0]\ntest[\"awake_rf\"] = best_model.predict_proba(X_test_standardized)[:, 1]\n\ntest.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nfrom itertools import groupby\n\n# Assuming 'test' DataFrame already exists and contains necessary columns\n\n# 1. Smoothing the Predictions\nsmoothing_length = 100\ntest[\"score\"] = test[\"awake_rf\"].rolling(smoothing_length, center=True).mean().ffill().bfill()\ntest[\"smooth\"] = test[\"not_awake_rf\"].rolling(smoothing_length, center=True).mean().ffill().bfill()\n\n# 2. Re-binarization\ntest[\"smooth\"] = test[\"smooth\"].round()\n\n# 3. Marking the Start and End of Events\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\n# 4. Column \"event\"\ntest[\"event\"] = get_event(test)\n\ntest.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 5. Create sample_submission with scores for all models\nsample_submission = test.loc[test[\"event\"] != 0][[\"series_id\", \"step\", \"event\", \"score\"]].copy().reset_index(drop=True).reset_index(drop=True, names=\"row_id\")\nsample_submission.to_csv('submission.csv', index=False)\nsample_submission.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}