{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "50ddcbd1", "metadata": {}, "outputs": [], "source": [ "import pickle" ] }, { "cell_type": "code", "execution_count": 2, "id": "9a90403c", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\Users\\deekshi\\AppData\\Local\\Temp\\ipykernel_31352\\4176903524.py:2: UserWarning: [16:57:16] WARNING: C:\\actions-runner\\_work\\xgboost\\xgboost\\src\\data\\../common/error_msg.h:82: If you are loading a serialized model (like pickle in Python, RDS in R) or\n", "configuration generated by an older version of XGBoost, please export the model by calling\n", "`Booster.save_model` from that version first, then load it back in current version. See:\n", "\n", " https://xgboost.readthedocs.io/en/stable/tutorials/saving_model.html\n", "\n", "for more details about differences between saving model and serializing.\n", "\n", " model = pickle.load(f)\n" ] } ], "source": [ "with open(\"xgboost_sleep_model.pkl\",'rb') as f:\n", " model = pickle.load(f)" ] }, { "cell_type": "code", "execution_count": 3, "id": "eaabe4bf", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: pandas in d:\\anaconda\\lib\\site-packages (2.2.2)\n", "Requirement already satisfied: scikit-learn in d:\\anaconda\\lib\\site-packages (1.4.2)\n", "Requirement already satisfied: xgboost in d:\\anaconda\\lib\\site-packages (3.0.0)\n", "Requirement already satisfied: openpyxl in d:\\anaconda\\lib\\site-packages (3.1.2)\n", "Requirement already satisfied: numpy>=1.26.0 in d:\\anaconda\\lib\\site-packages (from pandas) (1.26.4)\n", "Requirement already satisfied: python-dateutil>=2.8.2 in d:\\anaconda\\lib\\site-packages (from pandas) (2.9.0.post0)\n", "Requirement already satisfied: pytz>=2020.1 in d:\\anaconda\\lib\\site-packages (from pandas) (2024.1)\n", "Requirement already satisfied: tzdata>=2022.7 in d:\\anaconda\\lib\\site-packages (from pandas) (2023.3)\n", "Requirement already satisfied: scipy>=1.6.0 in d:\\anaconda\\lib\\site-packages (from scikit-learn) (1.13.1)\n", "Requirement already satisfied: joblib>=1.2.0 in d:\\anaconda\\lib\\site-packages (from scikit-learn) (1.4.2)\n", "Requirement already satisfied: threadpoolctl>=2.0.0 in d:\\anaconda\\lib\\site-packages (from scikit-learn) (2.2.0)\n", "Requirement already satisfied: et-xmlfile in d:\\anaconda\\lib\\site-packages (from openpyxl) (1.1.0)\n", "Requirement already satisfied: six>=1.5 in d:\\anaconda\\lib\\site-packages (from python-dateutil>=2.8.2->pandas) (1.16.0)\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "pip install pandas scikit-learn xgboost openpyxl\n" ] }, { "cell_type": "code", "execution_count": 15, "id": "ca8672ba", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "✅ Accuracy: 0.5952008346374543\n", "🎉 Model saved successfully as 'new_sleep_model.pkl'\n" ] } ], "source": [ "import pandas as pd\n", "import xgboost as xgb\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.metrics import accuracy_score\n", "import pickle\n", "\n", "# ✅ Step 1: Load the cleaned CSV file\n", "df = pd.read_csv(r\"D:\\Child_sleep_detect\\cleaned_sleep_data.csv\")\n", "\n", "# ✅ Step 2: Drop missing values\n", "df.dropna(inplace=True)\n", "\n", "# ✅ Step 3: Define features and target\n", "X = df[['anglez', 'enmo']]\n", "\n", "# ✅ Step 4: Manually encode 'sleep' column ('onset' = 0, 'wakeup' = 1)\n", "y = df['sleep'].map({'onset': 0, 'wakeup': 1})\n", "\n", "# ✅ Step 5: Train-test split\n", "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n", "\n", "# ✅ Step 6: Train XGBoost classifier\n", "model = xgb.XGBClassifier()\n", "model.fit(X_train, y_train)\n", "\n", "# ✅ Step 7: Evaluate the model\n", "y_pred = model.predict(X_test)\n", "acc = accuracy_score(y_test, y_pred)\n", "print(\"✅ Accuracy:\", acc)\n", "\n", "# ✅ Step 8: Save the model\n", "with open(\"new_sleep_model.pkl\", \"wb\") as f:\n", " pickle.dump(model, f)\n", "\n", "print(\"🎉 Model saved successfully as 'new_sleep_model.pkl'\")\n", "\n" ] }, { "cell_type": "code", "execution_count": 16, "id": "7c079ac3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 0\n", "1 1\n", "2 0\n", "3 1\n", "4 0\n", " ..\n", "9580 1\n", "9581 0\n", "9582 1\n", "9583 0\n", "9584 1\n", "Name: sleep, Length: 9585, dtype: int64" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y" ] }, { "cell_type": "code", "execution_count": null, "id": "18a6341f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "badf06a5", "metadata": {}, "outputs": [], "source": [ "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "a4376d7c", "metadata": {}, "outputs": [], "source": [ "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "1752fabb", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "77d9b049", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "7104aff0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "91b3c488", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "11a7572f", "metadata": {}, "outputs": [], "source": [ "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "dfe336dd", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "b680af21", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.4" } }, "nbformat": 4, "nbformat_minor": 5 }