Skip to content

Instantly share code, notes, and snippets.

@ashishsahu1
Created September 18, 2023 21:41
Show Gist options
  • Save ashishsahu1/a798ceee8ba0c15a81cbca59cb41bc7d to your computer and use it in GitHub Desktop.
Save ashishsahu1/a798ceee8ba0c15a81cbca59cb41bc7d to your computer and use it in GitHub Desktop.
Alarm_Final_Dlvry.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyM9nuYDPk3nCETV4UShYAdG",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/ashishsahu1/a798ceee8ba0c15a81cbca59cb41bc7d/alarm_final_dlvry.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"## Data Creation"
],
"metadata": {
"id": "K-x0LYbcZxmr"
}
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "t-NEdUILY40N",
"outputId": "7e99136a-05f4-4ff6-bd03-8512fdcd2210"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"CSV file 'health_data.csv' generated with all possible combinations.\n",
"CSV file 'health_data_with_ews.csv' generated with EWS scores.\n",
"CSV file 'health_data_filtered.csv' generated with original columns and the 'alarm' column.\n"
]
},
{
"output_type": "stream",
"name": "stderr",
"text": [
"<ipython-input-2-f96d01f0bff4>:99: SettingWithCopyWarning: \n",
"A value is trying to be set on a copy of a slice from a DataFrame.\n",
"Try using .loc[row_indexer,col_indexer] = value instead\n",
"\n",
"See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
" filtered_data[column] = filtered_data[column].apply(lambda x: np.nan if np.random.rand() < missing_prob else x)\n"
]
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"CSV file 'health_data_Final.csv' generated with random missing values and removed rows.\n"
]
}
],
"source": [
"import csv\n",
"import pandas as pd\n",
"import numpy as np # Import numpy for random operations\n",
"\n",
"# Realistic ranges for each column\n",
"spo2_range = range(0, 101, 5) # SpO2 values from 0 to 90\n",
"temp_range = range(30, 46) # Temperature values from 30C to 45C\n",
"bps_range = range(70, 241, 17) # Systolic BP values from 70 to 240 mmHg\n",
"bpd_range = range(20, 141, 12) # Diastolic BP values from 20 to 140 mmHg\n",
"respiration_range = range(0, 51, 5) # Respiration rate values from 0 to 50 breaths per minute\n",
"heart_rate_range = range(0, 141, 14) # Heart rate values from 0 to 140 beats per minute\n",
"\n",
"# Generating all possible combinations\n",
"combinations = []\n",
"for spo2 in spo2_range:\n",
" for temp in temp_range:\n",
" for bps in bps_range:\n",
" for bpd in bpd_range:\n",
" for respiration_rate in respiration_range:\n",
" for heart_rate in heart_rate_range:\n",
" combinations.append([spo2, temp, bps, bpd, respiration_rate, heart_rate])\n",
"\n",
"# Write the combinations to a CSV file\n",
"with open('health_data.csv', 'w', newline='') as csvfile:\n",
" csvwriter = csv.writer(csvfile)\n",
" # Write the header\n",
" csvwriter.writerow([\"SpO2\", \"Temperature\", \"Systolic_BP\", \"Diastolic_BP\", \"Respiration_Rate\", \"Heart_Rate\"])\n",
" # Write the combinations\n",
" csvwriter.writerows(combinations)\n",
"\n",
"print(\"CSV file 'health_data.csv' generated with all possible combinations.\")\n",
"\n",
"# Load the generated CSV file into a DataFrame\n",
"data = pd.read_csv('health_data.csv')\n",
"\n",
"# Function to calculate EWS score for a given value and thresholds\n",
"def calculate_ews(value, thresholds):\n",
" if value >= thresholds[0][0] and value <= thresholds[0][1]:\n",
" return 0\n",
" elif value >= thresholds[1][0] and value <= thresholds[1][1]:\n",
" return 1\n",
" elif value >= thresholds[2][0] and value <= thresholds[2][1]:\n",
" return 2\n",
" else:\n",
" return 3\n",
"\n",
"# Define EWS thresholds for each column except Heart Rate\n",
"ews_thresholds = {\n",
" 'SpO2': [(92, 100), (90, 92), (85, 89)],\n",
" 'Temperature': [(40, 45), (34, 39), (30, 33)],\n",
" 'Systolic_BP': [(111, 240), (101, 110), (91, 100)],\n",
" 'Diastolic_BP': [(100, 140), (60, 99), (40, 59)],\n",
" 'Respiration_Rate': [(0, 20), (21, 30), (31, 45)]\n",
"}\n",
"\n",
"# Calculate EWS scores for each column and add as new columns\n",
"for column, thresholds in ews_thresholds.items():\n",
" data[f'{column}_EWS'] = data[column].apply(lambda x: calculate_ews(x, thresholds))\n",
"\n",
"# Calculate EWS score for Heart Rate based on provided thresholds\n",
"def calculate_heart_rate_ews(h_value):\n",
" if 50 <= h_value <= 99:\n",
" return 0 # Normal range\n",
" elif (100 <= h_value <= 109) or (40 <= h_value <= 49):\n",
" return 1 # Low risk\n",
" elif (110 <= h_value <= 129) or (30 <= h_value <= 39):\n",
" return 2 # Moderate risk\n",
" else:\n",
" return 3 # High risk\n",
"\n",
"data['Heart_Rate_EWS'] = data['Heart_Rate'].apply(calculate_heart_rate_ews)\n",
"\n",
"# Calculate mews_std_score based on max EWS scores\n",
"data['mews_std_score'] = data.apply(lambda row: max(row[f'{column}_EWS'] for column in ews_thresholds.keys()), axis=1)\n",
"data['mews_std_score'] = data.apply(lambda row: max(row['Heart_Rate_EWS'], row['mews_std_score']), axis=1)\n",
"\n",
"data['alarm'] = data['mews_std_score'].apply(lambda score: 0 if score > 0.5 else 1)\n",
"\n",
"# Save the updated DataFrame to a new CSV file\n",
"data.to_csv('health_data_with_ews.csv', index=False)\n",
"\n",
"print(\"CSV file 'health_data_with_ews.csv' generated with EWS scores.\")\n",
"\n",
"\n",
"\n",
"# Select only the original columns and the 'alarm' column\n",
"original_columns = [\"SpO2\", \"Temperature\", \"Systolic_BP\", \"Diastolic_BP\", \"Respiration_Rate\", \"Heart_Rate\", \"alarm\"]\n",
"filtered_data = data[original_columns]\n",
"\n",
"# Save the filtered DataFrame to a new CSV file\n",
"filtered_data.to_csv('health_data_filtered.csv', index=False)\n",
"\n",
"print(\"CSV file 'health_data_filtered.csv' generated with original columns and the 'alarm' column.\")\n",
"\n",
"# Add random missing values\n",
"missing_prob = 0.1 # Adjust this probability as needed\n",
"for column in filtered_data.columns:\n",
" if(column != \"alarm\"):\n",
" filtered_data[column] = filtered_data[column].apply(lambda x: np.nan if np.random.rand() < missing_prob else x)\n",
"\n",
"# Remove random rows\n",
"remove_prob = 0.1 # Adjust this probability as needed\n",
"filtered_data = filtered_data[filtered_data.apply(lambda row: np.random.rand() > remove_prob, axis=1)]\n",
"\n",
"# Save the updated DataFrame to a new CSV file\n",
"filtered_data.to_csv('health_data_Final.csv', index=False)\n",
"\n",
"print(\"CSV file 'health_data_Final.csv' generated with random missing values and removed rows.\")\n"
]
},
{
"cell_type": "markdown",
"source": [
"## Adding Filters and SMOTE"
],
"metadata": {
"id": "rKo4P0AAZ3Dq"
}
},
{
"cell_type": "code",
"source": [
"import pandas as pd\n",
"from imblearn.over_sampling import SMOTE\n",
"from sklearn.impute import SimpleImputer\n",
"\n",
"# Load the final dataset\n",
"final_dataset = pd.read_csv('health_data_Final.csv')\n",
"\n",
"# Impute missing values\n",
"imputer = SimpleImputer(strategy='mean') # You can change the strategy as needed\n",
"final_dataset_imputed = final_dataset.copy()\n",
"final_dataset_imputed.iloc[:, :-1] = imputer.fit_transform(final_dataset.iloc[:, :-1])\n",
"\n",
"# Split the dataset into features (X) and target (y)\n",
"X = final_dataset_imputed.drop('alarm', axis=1)\n",
"y = final_dataset_imputed['alarm']\n",
"\n",
"# Apply SMOTE\n",
"smote = SMOTE(sampling_strategy='auto', random_state=42)\n",
"X_resampled, y_resampled = smote.fit_resample(X, y)\n",
"\n",
"# Combine the resampled data into a new DataFrame\n",
"resampled_dataset = pd.concat([X_resampled, y_resampled], axis=1)\n",
"\n",
"# Save the resampled dataset to a new CSV file\n",
"resampled_dataset.to_csv('balanced_health_data.csv', index=False)\n",
"\n",
"print(\"Balanced dataset saved to 'balanced_health_data.csv'.\")\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Vz8aMrYjZQyZ",
"outputId": "e0206387-7061-476b-cf1e-b5e443783a95"
},
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Balanced dataset saved to 'balanced_health_data.csv'.\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"fin2 = pd.read_csv(\"balanced_health_data.csv\")\n",
"fin2.head(10)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 362
},
"id": "pEqo4BnPZizA",
"outputId": "1cf4df42-3823-48cf-bded-c7ed8f992b21"
},
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" SpO2 Temperature Systolic_BP Diastolic_BP Respiration_Rate \\\n",
"0 0.000000 30.000000 70.0 20.000000 0.0 \n",
"1 0.000000 30.000000 70.0 80.009088 0.0 \n",
"2 0.000000 30.000000 70.0 20.000000 0.0 \n",
"3 0.000000 30.000000 70.0 20.000000 0.0 \n",
"4 0.000000 37.498661 70.0 20.000000 0.0 \n",
"5 0.000000 30.000000 70.0 20.000000 0.0 \n",
"6 50.013709 30.000000 70.0 20.000000 0.0 \n",
"7 50.013709 30.000000 70.0 20.000000 0.0 \n",
"8 0.000000 30.000000 70.0 20.000000 0.0 \n",
"9 0.000000 30.000000 70.0 80.009088 0.0 \n",
"\n",
" Heart_Rate alarm \n",
"0 0.000000 0 \n",
"1 14.000000 0 \n",
"2 70.003722 0 \n",
"3 42.000000 0 \n",
"4 56.000000 0 \n",
"5 70.000000 0 \n",
"6 70.003722 0 \n",
"7 98.000000 0 \n",
"8 112.000000 0 \n",
"9 126.000000 0 "
],
"text/html": [
"\n",
" <div id=\"df-5fcbebf8-3f74-4c5c-8c6d-1aedf5474bfb\" class=\"colab-df-container\">\n",
" <div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>SpO2</th>\n",
" <th>Temperature</th>\n",
" <th>Systolic_BP</th>\n",
" <th>Diastolic_BP</th>\n",
" <th>Respiration_Rate</th>\n",
" <th>Heart_Rate</th>\n",
" <th>alarm</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0.000000</td>\n",
" <td>30.000000</td>\n",
" <td>70.0</td>\n",
" <td>20.000000</td>\n",
" <td>0.0</td>\n",
" <td>0.000000</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0.000000</td>\n",
" <td>30.000000</td>\n",
" <td>70.0</td>\n",
" <td>80.009088</td>\n",
" <td>0.0</td>\n",
" <td>14.000000</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>0.000000</td>\n",
" <td>30.000000</td>\n",
" <td>70.0</td>\n",
" <td>20.000000</td>\n",
" <td>0.0</td>\n",
" <td>70.003722</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>0.000000</td>\n",
" <td>30.000000</td>\n",
" <td>70.0</td>\n",
" <td>20.000000</td>\n",
" <td>0.0</td>\n",
" <td>42.000000</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>0.000000</td>\n",
" <td>37.498661</td>\n",
" <td>70.0</td>\n",
" <td>20.000000</td>\n",
" <td>0.0</td>\n",
" <td>56.000000</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>0.000000</td>\n",
" <td>30.000000</td>\n",
" <td>70.0</td>\n",
" <td>20.000000</td>\n",
" <td>0.0</td>\n",
" <td>70.000000</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>50.013709</td>\n",
" <td>30.000000</td>\n",
" <td>70.0</td>\n",
" <td>20.000000</td>\n",
" <td>0.0</td>\n",
" <td>70.003722</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>50.013709</td>\n",
" <td>30.000000</td>\n",
" <td>70.0</td>\n",
" <td>20.000000</td>\n",
" <td>0.0</td>\n",
" <td>98.000000</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>0.000000</td>\n",
" <td>30.000000</td>\n",
" <td>70.0</td>\n",
" <td>20.000000</td>\n",
" <td>0.0</td>\n",
" <td>112.000000</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>0.000000</td>\n",
" <td>30.000000</td>\n",
" <td>70.0</td>\n",
" <td>80.009088</td>\n",
" <td>0.0</td>\n",
" <td>126.000000</td>\n",
" <td>0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>\n",
" <div class=\"colab-df-buttons\">\n",
"\n",
" <div class=\"colab-df-container\">\n",
" <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-5fcbebf8-3f74-4c5c-8c6d-1aedf5474bfb')\"\n",
" title=\"Convert this dataframe to an interactive table.\"\n",
" style=\"display:none;\">\n",
"\n",
" <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
" <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
" </svg>\n",
" </button>\n",
"\n",
" <style>\n",
" .colab-df-container {\n",
" display:flex;\n",
" gap: 12px;\n",
" }\n",
"\n",
" .colab-df-convert {\n",
" background-color: #E8F0FE;\n",
" border: none;\n",
" border-radius: 50%;\n",
" cursor: pointer;\n",
" display: none;\n",
" fill: #1967D2;\n",
" height: 32px;\n",
" padding: 0 0 0 0;\n",
" width: 32px;\n",
" }\n",
"\n",
" .colab-df-convert:hover {\n",
" background-color: #E2EBFA;\n",
" box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
" fill: #174EA6;\n",
" }\n",
"\n",
" .colab-df-buttons div {\n",
" margin-bottom: 4px;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert {\n",
" background-color: #3B4455;\n",
" fill: #D2E3FC;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert:hover {\n",
" background-color: #434B5C;\n",
" box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
" filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
" fill: #FFFFFF;\n",
" }\n",
" </style>\n",
"\n",
" <script>\n",
" const buttonEl =\n",
" document.querySelector('#df-5fcbebf8-3f74-4c5c-8c6d-1aedf5474bfb button.colab-df-convert');\n",
" buttonEl.style.display =\n",
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
"\n",
" async function convertToInteractive(key) {\n",
" const element = document.querySelector('#df-5fcbebf8-3f74-4c5c-8c6d-1aedf5474bfb');\n",
" const dataTable =\n",
" await google.colab.kernel.invokeFunction('convertToInteractive',\n",
" [key], {});\n",
" if (!dataTable) return;\n",
"\n",
" const docLinkHtml = 'Like what you see? Visit the ' +\n",
" '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
" + ' to learn more about interactive tables.';\n",
" element.innerHTML = '';\n",
" dataTable['output_type'] = 'display_data';\n",
" await google.colab.output.renderOutput(dataTable, element);\n",
" const docLink = document.createElement('div');\n",
" docLink.innerHTML = docLinkHtml;\n",
" element.appendChild(docLink);\n",
" }\n",
" </script>\n",
" </div>\n",
"\n",
"\n",
"<div id=\"df-38f225d5-eb59-45ff-ae31-eee778274b77\">\n",
" <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-38f225d5-eb59-45ff-ae31-eee778274b77')\"\n",
" title=\"Suggest charts.\"\n",
" style=\"display:none;\">\n",
"\n",
"<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
" width=\"24px\">\n",
" <g>\n",
" <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
" </g>\n",
"</svg>\n",
" </button>\n",
"\n",
"<style>\n",
" .colab-df-quickchart {\n",
" --bg-color: #E8F0FE;\n",
" --fill-color: #1967D2;\n",
" --hover-bg-color: #E2EBFA;\n",
" --hover-fill-color: #174EA6;\n",
" --disabled-fill-color: #AAA;\n",
" --disabled-bg-color: #DDD;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-quickchart {\n",
" --bg-color: #3B4455;\n",
" --fill-color: #D2E3FC;\n",
" --hover-bg-color: #434B5C;\n",
" --hover-fill-color: #FFFFFF;\n",
" --disabled-bg-color: #3B4455;\n",
" --disabled-fill-color: #666;\n",
" }\n",
"\n",
" .colab-df-quickchart {\n",
" background-color: var(--bg-color);\n",
" border: none;\n",
" border-radius: 50%;\n",
" cursor: pointer;\n",
" display: none;\n",
" fill: var(--fill-color);\n",
" height: 32px;\n",
" padding: 0;\n",
" width: 32px;\n",
" }\n",
"\n",
" .colab-df-quickchart:hover {\n",
" background-color: var(--hover-bg-color);\n",
" box-shadow: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
" fill: var(--button-hover-fill-color);\n",
" }\n",
"\n",
" .colab-df-quickchart-complete:disabled,\n",
" .colab-df-quickchart-complete:disabled:hover {\n",
" background-color: var(--disabled-bg-color);\n",
" fill: var(--disabled-fill-color);\n",
" box-shadow: none;\n",
" }\n",
"\n",
" .colab-df-spinner {\n",
" border: 2px solid var(--fill-color);\n",
" border-color: transparent;\n",
" border-bottom-color: var(--fill-color);\n",
" animation:\n",
" spin 1s steps(1) infinite;\n",
" }\n",
"\n",
" @keyframes spin {\n",
" 0% {\n",
" border-color: transparent;\n",
" border-bottom-color: var(--fill-color);\n",
" border-left-color: var(--fill-color);\n",
" }\n",
" 20% {\n",
" border-color: transparent;\n",
" border-left-color: var(--fill-color);\n",
" border-top-color: var(--fill-color);\n",
" }\n",
" 30% {\n",
" border-color: transparent;\n",
" border-left-color: var(--fill-color);\n",
" border-top-color: var(--fill-color);\n",
" border-right-color: var(--fill-color);\n",
" }\n",
" 40% {\n",
" border-color: transparent;\n",
" border-right-color: var(--fill-color);\n",
" border-top-color: var(--fill-color);\n",
" }\n",
" 60% {\n",
" border-color: transparent;\n",
" border-right-color: var(--fill-color);\n",
" }\n",
" 80% {\n",
" border-color: transparent;\n",
" border-right-color: var(--fill-color);\n",
" border-bottom-color: var(--fill-color);\n",
" }\n",
" 90% {\n",
" border-color: transparent;\n",
" border-bottom-color: var(--fill-color);\n",
" }\n",
" }\n",
"</style>\n",
"\n",
" <script>\n",
" async function quickchart(key) {\n",
" const quickchartButtonEl =\n",
" document.querySelector('#' + key + ' button');\n",
" quickchartButtonEl.disabled = true; // To prevent multiple clicks.\n",
" quickchartButtonEl.classList.add('colab-df-spinner');\n",
" try {\n",
" const charts = await google.colab.kernel.invokeFunction(\n",
" 'suggestCharts', [key], {});\n",
" } catch (error) {\n",
" console.error('Error during call to suggestCharts:', error);\n",
" }\n",
" quickchartButtonEl.classList.remove('colab-df-spinner');\n",
" quickchartButtonEl.classList.add('colab-df-quickchart-complete');\n",
" }\n",
" (() => {\n",
" let quickchartButtonEl =\n",
" document.querySelector('#df-38f225d5-eb59-45ff-ae31-eee778274b77 button');\n",
" quickchartButtonEl.style.display =\n",
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
" })();\n",
" </script>\n",
"</div>\n",
" </div>\n",
" </div>\n"
]
},
"metadata": {},
"execution_count": 4
}
]
},
{
"cell_type": "code",
"source": [
"fin2.describe()"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 300
},
"id": "ilylMyjEZiww",
"outputId": "33f16bd5-fcf9-41b6-c90e-da28b58d2087"
},
"execution_count": 5,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" SpO2 Temperature Systolic_BP Diastolic_BP \\\n",
"count 8.839918e+06 8.839918e+06 8.839918e+06 8.839918e+06 \n",
"mean 7.141167e+01 3.978977e+01 1.662714e+02 9.913307e+01 \n",
"std 3.120020e+01 4.067187e+00 4.629664e+01 3.418658e+01 \n",
"min 0.000000e+00 3.000000e+01 7.000000e+01 2.000000e+01 \n",
"25% 5.001371e+01 3.749866e+01 1.380000e+02 8.000909e+01 \n",
"50% 9.000000e+01 4.100000e+01 1.550000e+02 1.040000e+02 \n",
"75% 9.711945e+01 4.300000e+01 2.060000e+02 1.280000e+02 \n",
"max 1.000000e+02 4.500000e+01 2.400000e+02 1.400000e+02 \n",
"\n",
" Respiration_Rate Heart_Rate alarm \n",
"count 8.839918e+06 8.839918e+06 8839918.0 \n",
"mean 1.827758e+01 7.319315e+01 0.5 \n",
"std 1.373257e+01 3.169613e+01 0.5 \n",
"min 0.000000e+00 0.000000e+00 0.0 \n",
"25% 5.811170e+00 5.600000e+01 0.0 \n",
"50% 1.500000e+01 7.000360e+01 0.5 \n",
"75% 2.500150e+01 9.800000e+01 1.0 \n",
"max 5.000000e+01 1.400000e+02 1.0 "
],
"text/html": [
"\n",
" <div id=\"df-8a2dac8a-689c-4871-90f1-f38e52f2c02d\" class=\"colab-df-container\">\n",
" <div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>SpO2</th>\n",
" <th>Temperature</th>\n",
" <th>Systolic_BP</th>\n",
" <th>Diastolic_BP</th>\n",
" <th>Respiration_Rate</th>\n",
" <th>Heart_Rate</th>\n",
" <th>alarm</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>count</th>\n",
" <td>8.839918e+06</td>\n",
" <td>8.839918e+06</td>\n",
" <td>8.839918e+06</td>\n",
" <td>8.839918e+06</td>\n",
" <td>8.839918e+06</td>\n",
" <td>8.839918e+06</td>\n",
" <td>8839918.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>mean</th>\n",
" <td>7.141167e+01</td>\n",
" <td>3.978977e+01</td>\n",
" <td>1.662714e+02</td>\n",
" <td>9.913307e+01</td>\n",
" <td>1.827758e+01</td>\n",
" <td>7.319315e+01</td>\n",
" <td>0.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>std</th>\n",
" <td>3.120020e+01</td>\n",
" <td>4.067187e+00</td>\n",
" <td>4.629664e+01</td>\n",
" <td>3.418658e+01</td>\n",
" <td>1.373257e+01</td>\n",
" <td>3.169613e+01</td>\n",
" <td>0.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>min</th>\n",
" <td>0.000000e+00</td>\n",
" <td>3.000000e+01</td>\n",
" <td>7.000000e+01</td>\n",
" <td>2.000000e+01</td>\n",
" <td>0.000000e+00</td>\n",
" <td>0.000000e+00</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>25%</th>\n",
" <td>5.001371e+01</td>\n",
" <td>3.749866e+01</td>\n",
" <td>1.380000e+02</td>\n",
" <td>8.000909e+01</td>\n",
" <td>5.811170e+00</td>\n",
" <td>5.600000e+01</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>50%</th>\n",
" <td>9.000000e+01</td>\n",
" <td>4.100000e+01</td>\n",
" <td>1.550000e+02</td>\n",
" <td>1.040000e+02</td>\n",
" <td>1.500000e+01</td>\n",
" <td>7.000360e+01</td>\n",
" <td>0.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>75%</th>\n",
" <td>9.711945e+01</td>\n",
" <td>4.300000e+01</td>\n",
" <td>2.060000e+02</td>\n",
" <td>1.280000e+02</td>\n",
" <td>2.500150e+01</td>\n",
" <td>9.800000e+01</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>max</th>\n",
" <td>1.000000e+02</td>\n",
" <td>4.500000e+01</td>\n",
" <td>2.400000e+02</td>\n",
" <td>1.400000e+02</td>\n",
" <td>5.000000e+01</td>\n",
" <td>1.400000e+02</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>\n",
" <div class=\"colab-df-buttons\">\n",
"\n",
" <div class=\"colab-df-container\">\n",
" <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-8a2dac8a-689c-4871-90f1-f38e52f2c02d')\"\n",
" title=\"Convert this dataframe to an interactive table.\"\n",
" style=\"display:none;\">\n",
"\n",
" <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
" <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
" </svg>\n",
" </button>\n",
"\n",
" <style>\n",
" .colab-df-container {\n",
" display:flex;\n",
" gap: 12px;\n",
" }\n",
"\n",
" .colab-df-convert {\n",
" background-color: #E8F0FE;\n",
" border: none;\n",
" border-radius: 50%;\n",
" cursor: pointer;\n",
" display: none;\n",
" fill: #1967D2;\n",
" height: 32px;\n",
" padding: 0 0 0 0;\n",
" width: 32px;\n",
" }\n",
"\n",
" .colab-df-convert:hover {\n",
" background-color: #E2EBFA;\n",
" box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
" fill: #174EA6;\n",
" }\n",
"\n",
" .colab-df-buttons div {\n",
" margin-bottom: 4px;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert {\n",
" background-color: #3B4455;\n",
" fill: #D2E3FC;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert:hover {\n",
" background-color: #434B5C;\n",
" box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
" filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
" fill: #FFFFFF;\n",
" }\n",
" </style>\n",
"\n",
" <script>\n",
" const buttonEl =\n",
" document.querySelector('#df-8a2dac8a-689c-4871-90f1-f38e52f2c02d button.colab-df-convert');\n",
" buttonEl.style.display =\n",
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
"\n",
" async function convertToInteractive(key) {\n",
" const element = document.querySelector('#df-8a2dac8a-689c-4871-90f1-f38e52f2c02d');\n",
" const dataTable =\n",
" await google.colab.kernel.invokeFunction('convertToInteractive',\n",
" [key], {});\n",
" if (!dataTable) return;\n",
"\n",
" const docLinkHtml = 'Like what you see? Visit the ' +\n",
" '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
" + ' to learn more about interactive tables.';\n",
" element.innerHTML = '';\n",
" dataTable['output_type'] = 'display_data';\n",
" await google.colab.output.renderOutput(dataTable, element);\n",
" const docLink = document.createElement('div');\n",
" docLink.innerHTML = docLinkHtml;\n",
" element.appendChild(docLink);\n",
" }\n",
" </script>\n",
" </div>\n",
"\n",
"\n",
"<div id=\"df-8f1d81de-07fe-4810-bfbf-21eaf06a9f58\">\n",
" <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-8f1d81de-07fe-4810-bfbf-21eaf06a9f58')\"\n",
" title=\"Suggest charts.\"\n",
" style=\"display:none;\">\n",
"\n",
"<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
" width=\"24px\">\n",
" <g>\n",
" <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
" </g>\n",
"</svg>\n",
" </button>\n",
"\n",
"<style>\n",
" .colab-df-quickchart {\n",
" --bg-color: #E8F0FE;\n",
" --fill-color: #1967D2;\n",
" --hover-bg-color: #E2EBFA;\n",
" --hover-fill-color: #174EA6;\n",
" --disabled-fill-color: #AAA;\n",
" --disabled-bg-color: #DDD;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-quickchart {\n",
" --bg-color: #3B4455;\n",
" --fill-color: #D2E3FC;\n",
" --hover-bg-color: #434B5C;\n",
" --hover-fill-color: #FFFFFF;\n",
" --disabled-bg-color: #3B4455;\n",
" --disabled-fill-color: #666;\n",
" }\n",
"\n",
" .colab-df-quickchart {\n",
" background-color: var(--bg-color);\n",
" border: none;\n",
" border-radius: 50%;\n",
" cursor: pointer;\n",
" display: none;\n",
" fill: var(--fill-color);\n",
" height: 32px;\n",
" padding: 0;\n",
" width: 32px;\n",
" }\n",
"\n",
" .colab-df-quickchart:hover {\n",
" background-color: var(--hover-bg-color);\n",
" box-shadow: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
" fill: var(--button-hover-fill-color);\n",
" }\n",
"\n",
" .colab-df-quickchart-complete:disabled,\n",
" .colab-df-quickchart-complete:disabled:hover {\n",
" background-color: var(--disabled-bg-color);\n",
" fill: var(--disabled-fill-color);\n",
" box-shadow: none;\n",
" }\n",
"\n",
" .colab-df-spinner {\n",
" border: 2px solid var(--fill-color);\n",
" border-color: transparent;\n",
" border-bottom-color: var(--fill-color);\n",
" animation:\n",
" spin 1s steps(1) infinite;\n",
" }\n",
"\n",
" @keyframes spin {\n",
" 0% {\n",
" border-color: transparent;\n",
" border-bottom-color: var(--fill-color);\n",
" border-left-color: var(--fill-color);\n",
" }\n",
" 20% {\n",
" border-color: transparent;\n",
" border-left-color: var(--fill-color);\n",
" border-top-color: var(--fill-color);\n",
" }\n",
" 30% {\n",
" border-color: transparent;\n",
" border-left-color: var(--fill-color);\n",
" border-top-color: var(--fill-color);\n",
" border-right-color: var(--fill-color);\n",
" }\n",
" 40% {\n",
" border-color: transparent;\n",
" border-right-color: var(--fill-color);\n",
" border-top-color: var(--fill-color);\n",
" }\n",
" 60% {\n",
" border-color: transparent;\n",
" border-right-color: var(--fill-color);\n",
" }\n",
" 80% {\n",
" border-color: transparent;\n",
" border-right-color: var(--fill-color);\n",
" border-bottom-color: var(--fill-color);\n",
" }\n",
" 90% {\n",
" border-color: transparent;\n",
" border-bottom-color: var(--fill-color);\n",
" }\n",
" }\n",
"</style>\n",
"\n",
" <script>\n",
" async function quickchart(key) {\n",
" const quickchartButtonEl =\n",
" document.querySelector('#' + key + ' button');\n",
" quickchartButtonEl.disabled = true; // To prevent multiple clicks.\n",
" quickchartButtonEl.classList.add('colab-df-spinner');\n",
" try {\n",
" const charts = await google.colab.kernel.invokeFunction(\n",
" 'suggestCharts', [key], {});\n",
" } catch (error) {\n",
" console.error('Error during call to suggestCharts:', error);\n",
" }\n",
" quickchartButtonEl.classList.remove('colab-df-spinner');\n",
" quickchartButtonEl.classList.add('colab-df-quickchart-complete');\n",
" }\n",
" (() => {\n",
" let quickchartButtonEl =\n",
" document.querySelector('#df-8f1d81de-07fe-4810-bfbf-21eaf06a9f58 button');\n",
" quickchartButtonEl.style.display =\n",
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
" })();\n",
" </script>\n",
"</div>\n",
" </div>\n",
" </div>\n"
]
},
"metadata": {},
"execution_count": 5
}
]
},
{
"cell_type": "code",
"source": [
"len(fin2[fin2['alarm']==1]),len(fin2[fin2['alarm']==0])"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "U9ArYktJZitx",
"outputId": "d682bc6b-9eff-4450-9597-35d6832f87d6"
},
"execution_count": 6,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(4419959, 4419959)"
]
},
"metadata": {},
"execution_count": 6
}
]
},
{
"cell_type": "markdown",
"source": [
"## Model Creation"
],
"metadata": {
"id": "Bfu4U-vVaDI5"
}
},
{
"cell_type": "code",
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import tensorflow as tf\n",
"from sklearn.model_selection import train_test_split\n",
"from tensorflow.keras import layers, models\n",
"\n",
"from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay\n",
"from sklearn.impute import SimpleImputer\n",
"\n",
"# Load the dataset\n",
"dataset = pd.read_csv('balanced_health_data.csv')\n",
"\n",
"# Handle NaN values (impute with mean, you can choose a different strategy if needed)\n",
"imputer = SimpleImputer(strategy='mean')\n",
"X = dataset.drop('alarm', axis=1).values\n",
"y = dataset['alarm'].values\n",
"\n",
"X = imputer.fit_transform(X)\n",
"\n",
"# Split into input features (X) and target values (y)\n",
"# X = dataset.drop('alarm', axis=1).values\n",
"# y = dataset['alarm'].values\n",
"\n",
"# Split into training and validation sets\n",
"X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)\n",
"\n",
"# Build the neural network model\n",
"model = models.Sequential([\n",
" layers.Input(shape=(6,)), # Input layer with 6 features\n",
" layers.Dense(128, activation='relu'),\n",
" layers.Dense(64, activation='relu'),\n",
" layers.Dense(1, activation='sigmoid') # Output layer with sigmoid activation for binary classification\n",
"])\n",
"\n",
"# Compile the model\n",
"model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n",
"\n",
"# Train the model\n",
"history = model.fit(X_train, y_train, epochs=10, batch_size=128, validation_data=(X_val, y_val))\n",
"\n",
"# Evaluate the model on validation data\n",
"loss, accuracy = model.evaluate(X_val, y_val)\n",
"print(f\"Validation Loss: {loss:.4f}, Validation Accuracy: {accuracy:.4f}\")\n",
"\n",
"# Predict alarm values\n",
"predictions = model.predict(X_val)\n",
"\n",
"# Round predictions to binary values\n",
"rounded_predictions = np.round(predictions)\n",
"\n",
"# Save rounded predictions to a CSV file\n",
"result_df = pd.DataFrame({'Actual_Alarm': y_val, 'Predicted_Alarm': rounded_predictions.flatten()})\n",
"result_df.to_csv('predicted_alarms.csv', index=False)\n",
"\n",
"print(\"Predictions saved to predicted_alarms.csv\")\n",
"\n",
"# Evaluate the model on validation data\n",
"loss, accuracy = model.evaluate(X_val, y_val)\n",
"print(f\"Validation Loss: {loss:.4f}, Validation Accuracy: {accuracy:.4f}\")\n",
"\n",
"# Predict alarm values\n",
"predictions = model.predict(X_val)\n",
"\n",
"# Round predictions to binary values\n",
"rounded_predictions = np.round(predictions)\n",
"\n",
"# Save rounded predictions to a CSV file\n",
"result_df = pd.DataFrame({'Actual_Alarm': y_val, 'Predicted_Alarm': rounded_predictions.flatten()})\n",
"result_df.to_csv('predicted_alarms.csv', index=False)\n",
"\n",
"print(\"Predictions saved to predicted_alarms.csv\")"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "YUNkhNm2ZirJ",
"outputId": "84b564b6-24ff-4c2d-b8d6-9449154ed52d"
},
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Epoch 1/10\n",
"55250/55250 [==============================] - 144s 3ms/step - loss: 0.0500 - accuracy: 0.9841 - val_loss: 0.0447 - val_accuracy: 0.9870\n",
"Epoch 2/10\n",
"55250/55250 [==============================] - 142s 3ms/step - loss: 0.0331 - accuracy: 0.9900 - val_loss: 0.0325 - val_accuracy: 0.9912\n",
"Epoch 3/10\n",
"55250/55250 [==============================] - 137s 2ms/step - loss: 0.0295 - accuracy: 0.9913 - val_loss: 0.0254 - val_accuracy: 0.9922\n",
"Epoch 4/10\n",
"55250/55250 [==============================] - 135s 2ms/step - loss: 0.0269 - accuracy: 0.9923 - val_loss: 0.0248 - val_accuracy: 0.9925\n",
"Epoch 5/10\n",
"55250/55250 [==============================] - 138s 2ms/step - loss: 0.0256 - accuracy: 0.9929 - val_loss: 0.0235 - val_accuracy: 0.9934\n",
"Epoch 6/10\n",
"55250/55250 [==============================] - 141s 3ms/step - loss: 0.0248 - accuracy: 0.9932 - val_loss: 0.0232 - val_accuracy: 0.9937\n",
"Epoch 7/10\n",
"55250/55250 [==============================] - 135s 2ms/step - loss: 0.0242 - accuracy: 0.9934 - val_loss: 0.0236 - val_accuracy: 0.9940\n",
"Epoch 8/10\n",
"55250/55250 [==============================] - 137s 2ms/step - loss: 0.0239 - accuracy: 0.9936 - val_loss: 0.0265 - val_accuracy: 0.9925\n",
"Epoch 9/10\n",
"55250/55250 [==============================] - 137s 2ms/step - loss: 0.0236 - accuracy: 0.9937 - val_loss: 0.0225 - val_accuracy: 0.9937\n",
"Epoch 10/10\n",
"55250/55250 [==============================] - 135s 2ms/step - loss: 0.0232 - accuracy: 0.9938 - val_loss: 0.0231 - val_accuracy: 0.9939\n",
"55250/55250 [==============================] - 87s 2ms/step - loss: 0.0231 - accuracy: 0.9939\n",
"Validation Loss: 0.0231, Validation Accuracy: 0.9939\n",
"55250/55250 [==============================] - 79s 1ms/step\n",
"Predictions saved to predicted_alarms.csv\n",
"55250/55250 [==============================] - 89s 2ms/step - loss: 0.0231 - accuracy: 0.9939\n",
"Validation Loss: 0.0231, Validation Accuracy: 0.9939\n",
"55250/55250 [==============================] - 78s 1ms/step\n",
"Predictions saved to predicted_alarms.csv\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"## Ploting Confusion Matrix"
],
"metadata": {
"id": "7y2cmxUwaItE"
}
},
{
"cell_type": "code",
"source": [
"# Round predictions to binary values\n",
"rounded_predictions = np.round(predictions)\n",
"\n",
"# Replace NaN values in y_val with 0 (or any other value)\n",
"y_val = np.nan_to_num(y_val, nan=0)\n",
"\n",
"# Compute confusion matrix\n",
"confusion = confusion_matrix(y_val, rounded_predictions)\n",
"print(\"Confusion Matrix:\")\n",
"print(confusion)\n",
"\n",
"# Display confusion matrix as a heatmap\n",
"ConfusionMatrixDisplay(confusion).plot()\n",
"\n",
"# Show the plot\n",
"import matplotlib.pyplot as plt\n",
"plt.show()"
],
"metadata": {
"id": "IQfmPs6TZint"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "hLZp1OPFZiis"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "CaYlkopUZiVd"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment