{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e4d6a756-7540-4741-8e3f-2b4da348783b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from matplotlib.pyplot import subplots\n",
    "\n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.preprocessing import PolynomialFeatures\n",
    "from sklearn.metrics import r2_score, mean_squared_error\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.linear_model import LinearRegression"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9b9b6c7f-2584-4d16-ad2c-8b95a6fea9a4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<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>Age</th>\n",
       "      <th>Blood Pressure</th>\n",
       "      <th>Blood Glucose Random</th>\n",
       "      <th>White Blood Cell Count</th>\n",
       "      <th>Red Blood Cell Count</th>\n",
       "      <th>Class</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>48</td>\n",
       "      <td>70</td>\n",
       "      <td>117</td>\n",
       "      <td>6700</td>\n",
       "      <td>3.9</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>53</td>\n",
       "      <td>90</td>\n",
       "      <td>70</td>\n",
       "      <td>12100</td>\n",
       "      <td>3.7</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>63</td>\n",
       "      <td>70</td>\n",
       "      <td>380</td>\n",
       "      <td>4500</td>\n",
       "      <td>3.8</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>68</td>\n",
       "      <td>80</td>\n",
       "      <td>157</td>\n",
       "      <td>11000</td>\n",
       "      <td>2.6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>61</td>\n",
       "      <td>80</td>\n",
       "      <td>173</td>\n",
       "      <td>9200</td>\n",
       "      <td>3.2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>153</th>\n",
       "      <td>55</td>\n",
       "      <td>80</td>\n",
       "      <td>140</td>\n",
       "      <td>6700</td>\n",
       "      <td>4.9</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>154</th>\n",
       "      <td>42</td>\n",
       "      <td>70</td>\n",
       "      <td>75</td>\n",
       "      <td>7800</td>\n",
       "      <td>6.2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>155</th>\n",
       "      <td>12</td>\n",
       "      <td>80</td>\n",
       "      <td>100</td>\n",
       "      <td>6600</td>\n",
       "      <td>5.4</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>156</th>\n",
       "      <td>17</td>\n",
       "      <td>60</td>\n",
       "      <td>114</td>\n",
       "      <td>7200</td>\n",
       "      <td>5.9</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>157</th>\n",
       "      <td>58</td>\n",
       "      <td>80</td>\n",
       "      <td>131</td>\n",
       "      <td>6800</td>\n",
       "      <td>6.1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>158 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     Age  Blood Pressure  Blood Glucose Random  White Blood Cell Count  \\\n",
       "0     48              70                   117                    6700   \n",
       "1     53              90                    70                   12100   \n",
       "2     63              70                   380                    4500   \n",
       "3     68              80                   157                   11000   \n",
       "4     61              80                   173                    9200   \n",
       "..   ...             ...                   ...                     ...   \n",
       "153   55              80                   140                    6700   \n",
       "154   42              70                    75                    7800   \n",
       "155   12              80                   100                    6600   \n",
       "156   17              60                   114                    7200   \n",
       "157   58              80                   131                    6800   \n",
       "\n",
       "     Red Blood Cell Count  Class  \n",
       "0                     3.9      1  \n",
       "1                     3.7      1  \n",
       "2                     3.8      1  \n",
       "3                     2.6      1  \n",
       "4                     3.2      1  \n",
       "..                    ...    ...  \n",
       "153                   4.9      0  \n",
       "154                   6.2      0  \n",
       "155                   5.4      0  \n",
       "156                   5.9      0  \n",
       "157                   6.1      0  \n",
       "\n",
       "[158 rows x 6 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ckd = pd.read_csv('ckd.csv')\n",
    "ckd = ckd[['Age', 'Blood Pressure', 'Blood Glucose Random', 'White Blood Cell Count', 'Red Blood Cell Count', 'Class']]\n",
    "ckd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "979258e3-0fd1-4d79-a700-17c792b7275a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Evaluation of Logistic Regression classification\n",
      "Accuracy: 0.9583333333333334\n",
      "\n",
      "Confusion Matrix:\n",
      "[[34  1]\n",
      " [ 1 12]]\n",
      "\n",
      "Classification Report:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.97      0.97      0.97        35\n",
      "           1       0.92      0.92      0.92        13\n",
      "\n",
      "    accuracy                           0.96        48\n",
      "   macro avg       0.95      0.95      0.95        48\n",
      "weighted avg       0.96      0.96      0.96        48\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Logistic Regression\n",
    "\n",
    "X = ckd[['Age', 'Blood Pressure', 'Blood Glucose Random', 'White Blood Cell Count', 'Red Blood Cell Count']]\n",
    "y = ckd['Class']\n",
    "\n",
    "# Create a train:test split of the data \n",
    "X_train, X_test, y_train, y_test = train_test_split(\n",
    "    X, y, test_size=0.30, random_state=42, stratify=y\n",
    ")\n",
    "\n",
    "# We shall use the StandardScaler to scale the training dataset\n",
    "scaler = StandardScaler()\n",
    "X_train_scaled = scaler.fit_transform(X_train)\n",
    "# The transform method is then used to scale the test data using parameters\n",
    "# obtained from the training data.\n",
    "X_test_scaled = scaler.transform(X_test)\n",
    "\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "logreg = LogisticRegression(max_iter=1000)\n",
    "\n",
    "logreg.fit(X_train, y_train)\n",
    "y_pred = logreg.predict(X_test)\n",
    "\n",
    "from sklearn.metrics import accuracy_score, confusion_matrix, classification_report\n",
    "# Evaluate\n",
    "print('Evaluation of Logistic Regression classification')\n",
    "print(\"Accuracy:\", accuracy_score(y_test, y_pred))\n",
    "print(\"\\nConfusion Matrix:\")\n",
    "print(confusion_matrix(y_test, y_pred))\n",
    "print(\"\\nClassification Report:\")\n",
    "print(classification_report(y_test, y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "33be67d0-3e6c-4eb4-be24-9c558612bd9f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Evaluation of KNN classification\n",
      "Accuracy: 0.8541666666666666\n",
      "\n",
      "Confusion Matrix:\n",
      "[[35  0]\n",
      " [ 7  6]]\n",
      "\n",
      "Classification Report:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.83      1.00      0.91        35\n",
      "           1       1.00      0.46      0.63        13\n",
      "\n",
      "    accuracy                           0.85        48\n",
      "   macro avg       0.92      0.73      0.77        48\n",
      "weighted avg       0.88      0.85      0.83        48\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "knn = KNeighborsClassifier(n_neighbors=9)\n",
    "knn.fit(X_train, y_train)\n",
    "y_pred = knn.predict(X_test)\n",
    "\n",
    "# Evaluate\n",
    "print('Evaluation of KNN classification')\n",
    "print(\"Accuracy:\", accuracy_score(y_test, y_pred))\n",
    "print(\"\\nConfusion Matrix:\")\n",
    "print(confusion_matrix(y_test, y_pred))\n",
    "print(\"\\nClassification Report:\")\n",
    "print(classification_report(y_test, y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c6e2c252-32aa-40ff-aac3-d2a051d876cf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 0.9791666666666666\n",
      "\n",
      "Confusion Matrix:\n",
      "[[35  0]\n",
      " [ 1 12]]\n",
      "\n",
      "Classification Report:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.97      1.00      0.99        35\n",
      "           1       1.00      0.92      0.96        13\n",
      "\n",
      "    accuracy                           0.98        48\n",
      "   macro avg       0.99      0.96      0.97        48\n",
      "weighted avg       0.98      0.98      0.98        48\n",
      "\n"
     ]
    }
   ],
   "source": [
    "X = ckd[['Age', 'Blood Pressure', 'Blood Glucose Random', 'White Blood Cell Count', 'Red Blood Cell Count']]\n",
    "y = ckd['Class']\n",
    "\n",
    "# Create a train:test split of the data \n",
    "X_train, X_test, y_train, y_test = train_test_split(\n",
    "    X, y, test_size=0.30, random_state=42, stratify=y\n",
    ")\n",
    "\n",
    "from sklearn.naive_bayes import GaussianNB\n",
    "from sklearn.metrics import accuracy_score, confusion_matrix, classification_report\n",
    "\n",
    "# Create and train the classifier\n",
    "gnb_model = GaussianNB()\n",
    "gnb_model.fit(X_train, y_train)\n",
    "\n",
    "# Make predictions\n",
    "y_pred = gnb_model.predict(X_test)\n",
    "\n",
    "# Evaluate\n",
    "print(\"Accuracy:\", accuracy_score(y_test, y_pred))\n",
    "print(\"\\nConfusion Matrix:\")\n",
    "print(confusion_matrix(y_test, y_pred))\n",
    "print(\"\\nClassification Report:\")\n",
    "print(classification_report(y_test, y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b4565f0-c297-4b40-8897-03237941ff78",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
