{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "310c258e",
   "metadata": {},
   "source": [
    "# Machine Learning Activity: Feature Selection and Model Comparison\n",
    "\n",
    "In this notebook, you will train a **Random Forest classifier** using the **Diabetes** or **Titanic** dataset.\n",
    "You will analyze the effect of **feature selection** and compare **Random Forest, Logistic Regression, and Naïve Bayes classifiers**.\n",
    "\n",
    "## Objectives\n",
    "1. Train a **Random Forest classifier**.\n",
    "2. Investigate feature selection using:\n",
    "   - **Manual feature importance analysis**.\n",
    "   - **Principal Component Analysis (PCA)**.\n",
    "3. Train **Logistic Regression** and **Naïve Bayes** for comparison.\n",
    "4. Evaluate models using **accuracy, precision, recall, and F1-score**.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "f77f9100",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 891 entries, 0 to 890\n",
      "Data columns (total 12 columns):\n",
      " #   Column       Non-Null Count  Dtype  \n",
      "---  ------       --------------  -----  \n",
      " 0   PassengerId  891 non-null    int64  \n",
      " 1   Survived     891 non-null    int64  \n",
      " 2   Pclass       891 non-null    int64  \n",
      " 3   Name         891 non-null    object \n",
      " 4   Sex          891 non-null    object \n",
      " 5   Age          714 non-null    float64\n",
      " 6   SibSp        891 non-null    int64  \n",
      " 7   Parch        891 non-null    int64  \n",
      " 8   Ticket       891 non-null    object \n",
      " 9   Fare         891 non-null    float64\n",
      " 10  Cabin        204 non-null    object \n",
      " 11  Embarked     889 non-null    object \n",
      "dtypes: float64(2), int64(5), object(5)\n",
      "memory usage: 83.7+ KB\n",
      "None\n"
     ]
    },
    {
     "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>PassengerId</th>\n",
       "      <th>Survived</th>\n",
       "      <th>Pclass</th>\n",
       "      <th>Name</th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Ticket</th>\n",
       "      <th>Fare</th>\n",
       "      <th>Cabin</th>\n",
       "      <th>Embarked</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Braund\\t Mr. Owen Harris</td>\n",
       "      <td>male</td>\n",
       "      <td>22.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>A/5 21171</td>\n",
       "      <td>7.2500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Cumings\\t Mrs. John Bradley (Florence Briggs T...</td>\n",
       "      <td>female</td>\n",
       "      <td>38.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>PC 17599</td>\n",
       "      <td>71.2833</td>\n",
       "      <td>C85</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>Heikkinen\\t Miss Laina</td>\n",
       "      <td>female</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>STON/O2. 3101282</td>\n",
       "      <td>7.9250</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Futrelle\\t Mrs. Jacques Heath (Lily May Peel)</td>\n",
       "      <td>female</td>\n",
       "      <td>35.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>113803</td>\n",
       "      <td>53.1000</td>\n",
       "      <td>C123</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Allen\\t Mr. William Henry</td>\n",
       "      <td>male</td>\n",
       "      <td>35.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>373450</td>\n",
       "      <td>8.0500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   PassengerId  Survived  Pclass  \\\n",
       "0            1         0       3   \n",
       "1            2         1       1   \n",
       "2            3         1       3   \n",
       "3            4         1       1   \n",
       "4            5         0       3   \n",
       "\n",
       "                                                Name     Sex   Age  SibSp  \\\n",
       "0                           Braund\\t Mr. Owen Harris    male  22.0      1   \n",
       "1  Cumings\\t Mrs. John Bradley (Florence Briggs T...  female  38.0      1   \n",
       "2                             Heikkinen\\t Miss Laina  female  26.0      0   \n",
       "3      Futrelle\\t Mrs. Jacques Heath (Lily May Peel)  female  35.0      1   \n",
       "4                          Allen\\t Mr. William Henry    male  35.0      0   \n",
       "\n",
       "   Parch            Ticket     Fare Cabin Embarked  \n",
       "0      0         A/5 21171   7.2500   NaN        S  \n",
       "1      0          PC 17599  71.2833   C85        C  \n",
       "2      0  STON/O2. 3101282   7.9250   NaN        S  \n",
       "3      0            113803  53.1000  C123        S  \n",
       "4      0            373450   8.0500   NaN        S  "
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.preprocessing import StandardScaler, LabelEncoder\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.naive_bayes import GaussianNB\n",
    "from sklearn.decomposition import PCA\n",
    "from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score\n",
    "\n",
    "# Load dataset (Uncomment the one you want to use)\n",
    "#df = pd.read_csv('diabetes.csv')  # Diabetes dataset\n",
    "df_titanic= pd.read_csv('titanic.csv')  # Titanic dataset\n",
    "\n",
    "# Display basic info\n",
    "print(df_titanic.info())\n",
    "\n",
    "df_titanic.head()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ef428d8d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# # # Handle missing values (if any)\n",
    "# df.dropna(inplace=True)\n",
    "\n",
    "# # Encode categorical features (if using Titanic dataset)\n",
    "# if 'Sex' in df.columns:\n",
    "#     df['Sex'] = LabelEncoder().fit_transform(df['Sex'])\n",
    "\n",
    "# # Define features and target\n",
    "# X = df.drop(columns=['Outcome']) if 'Outcome' in df.columns else df.drop(columns=['Survived'])\n",
    "# y = df['Outcome'] if 'Outcome' in df.columns else df['Survived']\n",
    "\n",
    "# # Split data\n",
    "# X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "f90a8350-7a25-4b22-824f-915de56cb775",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Before ============ ::\n",
      "PassengerId      0\n",
      "Survived         0\n",
      "Pclass           0\n",
      "Name             0\n",
      "Sex              0\n",
      "Age            177\n",
      "SibSp            0\n",
      "Parch            0\n",
      "Ticket           0\n",
      "Fare             0\n",
      "Cabin          687\n",
      "Embarked         2\n",
      "dtype: int64\n",
      "\n",
      "After ============ :\n",
      "PassengerId    0\n",
      "Survived       0\n",
      "Pclass         0\n",
      "Name           0\n",
      "Sex            0\n",
      "Age            0\n",
      "SibSp          0\n",
      "Parch          0\n",
      "Ticket         0\n",
      "Fare           0\n",
      "Embarked       0\n",
      "dtype: int64\n",
      "===============================\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Index: 712 entries, 0 to 890\n",
      "Data columns (total 11 columns):\n",
      " #   Column       Non-Null Count  Dtype  \n",
      "---  ------       --------------  -----  \n",
      " 0   PassengerId  712 non-null    int64  \n",
      " 1   Survived     712 non-null    int64  \n",
      " 2   Pclass       712 non-null    int64  \n",
      " 3   Name         712 non-null    object \n",
      " 4   Sex          712 non-null    int64  \n",
      " 5   Age          712 non-null    float64\n",
      " 6   SibSp        712 non-null    int64  \n",
      " 7   Parch        712 non-null    int64  \n",
      " 8   Ticket       712 non-null    object \n",
      " 9   Fare         712 non-null    float64\n",
      " 10  Embarked     712 non-null    float64\n",
      "dtypes: float64(3), int64(6), object(2)\n",
      "memory usage: 66.8+ KB\n",
      "None\n",
      "===============================\n",
      "Index(['PassengerId', 'Survived', 'Pclass', 'Name', 'Sex', 'Age', 'SibSp',\n",
      "       'Parch', 'Ticket', 'Fare', 'Embarked'],\n",
      "      dtype='object')\n"
     ]
    }
   ],
   "source": [
    "## Check for missing \n",
    "print(\"Before ============ ::\")\n",
    "print(df_titanic.isna().sum())\n",
    "\n",
    "# Delete the column \"Cabin\", which has 687 out of 890 missing.\n",
    "df_titanic.drop(\"Cabin\", axis=1, inplace=True)\n",
    "\n",
    "## Delete the rows with missing values for \"Embarked\"\n",
    "df_titanic.dropna(subset=[\"Embarked\"], axis=0, how=\"all\", inplace=True)\n",
    "\n",
    "\n",
    "## Delete the rows with missing values for \"Age\"\n",
    "df_titanic.dropna(subset=[\"Age\"], axis=0, how=\"all\", inplace=True)\n",
    "\n",
    "\n",
    "print(\"\\nAfter ============ :\")\n",
    "print(df_titanic.isna().sum())\n",
    "\n",
    "print(\"===============================\")\n",
    "\n",
    "print(df_titanic.info())\n",
    "\n",
    "print(\"===============================\")\n",
    "print(df_titanic.columns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "d5c02e35-efdc-48fb-b731-2d8964dce815",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2. 0. 1.]\n"
     ]
    },
    {
     "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>PassengerId</th>\n",
       "      <th>Survived</th>\n",
       "      <th>Pclass</th>\n",
       "      <th>Name</th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Ticket</th>\n",
       "      <th>Fare</th>\n",
       "      <th>Embarked</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Braund\\t Mr. Owen Harris</td>\n",
       "      <td>0</td>\n",
       "      <td>22.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>A/5 21171</td>\n",
       "      <td>7.2500</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Cumings\\t Mrs. John Bradley (Florence Briggs T...</td>\n",
       "      <td>1</td>\n",
       "      <td>38.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>PC 17599</td>\n",
       "      <td>71.2833</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>Heikkinen\\t Miss Laina</td>\n",
       "      <td>1</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>STON/O2. 3101282</td>\n",
       "      <td>7.9250</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Futrelle\\t Mrs. Jacques Heath (Lily May Peel)</td>\n",
       "      <td>1</td>\n",
       "      <td>35.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>113803</td>\n",
       "      <td>53.1000</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Allen\\t Mr. William Henry</td>\n",
       "      <td>0</td>\n",
       "      <td>35.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>373450</td>\n",
       "      <td>8.0500</td>\n",
       "      <td>2.0</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",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>885</th>\n",
       "      <td>886</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Rice\\t Mrs. William (Margaret Norton)</td>\n",
       "      <td>1</td>\n",
       "      <td>39.0</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>382652</td>\n",
       "      <td>29.1250</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>886</th>\n",
       "      <td>887</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>Montvila\\t Rev. Juozas</td>\n",
       "      <td>0</td>\n",
       "      <td>27.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>211536</td>\n",
       "      <td>13.0000</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>887</th>\n",
       "      <td>888</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Graham\\t Miss Margaret Edith</td>\n",
       "      <td>1</td>\n",
       "      <td>19.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>112053</td>\n",
       "      <td>30.0000</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>889</th>\n",
       "      <td>890</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Behr\\t Mr. Karl Howell</td>\n",
       "      <td>0</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>111369</td>\n",
       "      <td>30.0000</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>890</th>\n",
       "      <td>891</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Dooley\\t Mr. Patrick</td>\n",
       "      <td>0</td>\n",
       "      <td>32.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>370376</td>\n",
       "      <td>7.7500</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>712 rows × 11 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     PassengerId  Survived  Pclass  \\\n",
       "0              1         0       3   \n",
       "1              2         1       1   \n",
       "2              3         1       3   \n",
       "3              4         1       1   \n",
       "4              5         0       3   \n",
       "..           ...       ...     ...   \n",
       "885          886         0       3   \n",
       "886          887         0       2   \n",
       "887          888         1       1   \n",
       "889          890         1       1   \n",
       "890          891         0       3   \n",
       "\n",
       "                                                  Name  Sex   Age  SibSp  \\\n",
       "0                             Braund\\t Mr. Owen Harris    0  22.0      1   \n",
       "1    Cumings\\t Mrs. John Bradley (Florence Briggs T...    1  38.0      1   \n",
       "2                               Heikkinen\\t Miss Laina    1  26.0      0   \n",
       "3        Futrelle\\t Mrs. Jacques Heath (Lily May Peel)    1  35.0      1   \n",
       "4                            Allen\\t Mr. William Henry    0  35.0      0   \n",
       "..                                                 ...  ...   ...    ...   \n",
       "885              Rice\\t Mrs. William (Margaret Norton)    1  39.0      0   \n",
       "886                             Montvila\\t Rev. Juozas    0  27.0      0   \n",
       "887                       Graham\\t Miss Margaret Edith    1  19.0      0   \n",
       "889                             Behr\\t Mr. Karl Howell    0  26.0      0   \n",
       "890                               Dooley\\t Mr. Patrick    0  32.0      0   \n",
       "\n",
       "     Parch            Ticket     Fare  Embarked  \n",
       "0        0         A/5 21171   7.2500       2.0  \n",
       "1        0          PC 17599  71.2833       0.0  \n",
       "2        0  STON/O2. 3101282   7.9250       2.0  \n",
       "3        0            113803  53.1000       2.0  \n",
       "4        0            373450   8.0500       2.0  \n",
       "..     ...               ...      ...       ...  \n",
       "885      5            382652  29.1250       1.0  \n",
       "886      0            211536  13.0000       2.0  \n",
       "887      0            112053  30.0000       2.0  \n",
       "889      0            111369  30.0000       0.0  \n",
       "890      0            370376   7.7500       1.0  \n",
       "\n",
       "[712 rows x 11 columns]"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Replace 'male' with 0 and 'female' with 1\n",
    "df_titanic['Sex'] = df_titanic['Sex'].replace({'male': 0, 'female': 1})\n",
    "\n",
    "# Get all the unique values in the Embark column.\n",
    "print(df_titanic['Embarked'].unique())\n",
    "\n",
    "# Replace the values in Embark with numeric ones\n",
    "df_titanic['Embarked'] = df_titanic['Embarked'].replace({'C': 0, 'Q': 1, 'S': 2})\n",
    "df_titanic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "803835ae-24cb-4cde-b2fa-4110eff3c7fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# split the data into train and test using the SciKitLearn package\n",
    "\n",
    "# specify the target variables (dependent/predicted): \"y\"\n",
    "y = df_titanic['Survived']\n",
    "\n",
    "# Drop the 'y' column and unwanted columns i.e. the predictors (a.k.a. independent) variables : \"x\"\n",
    "X = df_titanic.drop(['Survived','PassengerId', 'Name', 'Ticket'],axis=1)\n",
    "\n",
    "\n",
    "# split the data into train and test using the SciKitLearn package \"train_test_split\"\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y , test_size=0.25, random_state=104) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "36a74543-e4a8-452f-8def-2c15aa81d8ee",
   "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>Pclass</th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Fare</th>\n",
       "      <th>Embarked</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>421</th>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>21.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>7.7333</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>78</th>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0.83</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>29.0000</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>860</th>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>41.00</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>14.1083</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>607</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>27.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>30.5000</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>804</th>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>27.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>6.9750</td>\n",
       "      <td>2</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",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>209</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>40.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>31.0000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>819</th>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>10.00</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>27.9000</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>315</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>26.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>7.8542</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>883</th>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>28.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>10.5000</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>91</th>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>20.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>7.8542</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>534 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     Pclass  Sex    Age  SibSp  Parch     Fare  Embarked\n",
       "421       3    0  21.00      0      0   7.7333         1\n",
       "78        2    0   0.83      0      2  29.0000         2\n",
       "860       3    0  41.00      2      0  14.1083         2\n",
       "607       1    0  27.00      0      0  30.5000         2\n",
       "804       3    0  27.00      0      0   6.9750         2\n",
       "..      ...  ...    ...    ...    ...      ...       ...\n",
       "209       1    0  40.00      0      0  31.0000         0\n",
       "819       3    0  10.00      3      2  27.9000         2\n",
       "315       3    1  26.00      0      0   7.8542         2\n",
       "883       2    0  28.00      0      0  10.5000         2\n",
       "91        3    0  20.00      0      0   7.8542         2\n",
       "\n",
       "[534 rows x 7 columns]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "fba7f053-b147-4edb-a642-f14258d609b7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "421    0\n",
       "78     1\n",
       "860    0\n",
       "607    1\n",
       "804    1\n",
       "      ..\n",
       "209    1\n",
       "819    0\n",
       "315    1\n",
       "883    0\n",
       "91     0\n",
       "Name: Survived, Length: 534, dtype: int64"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "282e91c1-9453-426b-a89d-aa0b5a00c388",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "The accuracy score for RandomForest:  0.8089887640449438\n",
      "\n",
      "============== Metric ===============================\n",
      "\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.88      0.82      0.85       114\n",
      "           1       0.71      0.80      0.75        64\n",
      "\n",
      "    accuracy                           0.81       178\n",
      "   macro avg       0.79      0.81      0.80       178\n",
      "weighted avg       0.82      0.81      0.81       178\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import classification_report, accuracy_score, confusion_matrix\n",
    "\n",
    "\n",
    "# make a copy of the algorithm\n",
    "RF_clf = RandomForestClassifier(random_state=42)\n",
    "\n",
    "# fits a model using training data\n",
    "RF_clf.fit(X_train, y_train)\n",
    "\n",
    "\n",
    "# check with test data\n",
    "y_pred = RF_clf.predict(X_test)\n",
    "\n",
    "# Print the accuracy score\n",
    "print('\\nThe accuracy score for RandomForest: ', accuracy_score(y_test, y_pred))\n",
    "\n",
    "print(\"\\n============== Metric ===============================\\n\")\n",
    "# Print the evaluation metrics\n",
    "print(classification_report(y_test, y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "0b428f41-6e0f-4829-bfa6-af20d34ecf78",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.10698919, 0.22239287, 0.28488012, 0.05249737, 0.04421786,\n",
       "       0.26067288, 0.02834971])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "RF_clf.feature_importances_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "14bd53fe-a3e8-473c-a1e4-270ab71a0aee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked'], dtype='object')"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "f432df54-595b-4dca-adda-0667e2de7602",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Reduce unimportant features\n",
    "# split the data into train and test using the SciKitLearn package\n",
    "\n",
    "# specify the target variables (dependent/predicted): \"y\"\n",
    "y = df_titanic['Survived']\n",
    "\n",
    "# Drop the 'y' column and unwanted columns i.e. the predictors (a.k.a. independent) variables : \"x\"\n",
    "X = df_titanic.drop(['Survived','PassengerId', 'Name', 'Ticket', 'Embarked', 'Parch'],axis=1)\n",
    "\n",
    "\n",
    "# split the data into train and test using the SciKitLearn package \"train_test_split\"\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y , test_size=0.25, random_state=104) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5ff2ce9d-1f1d-4189-ab66-e164bd040cda",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import LabelEncoder\n",
    "le = LabelEncoder()\n",
    "X_train['your_column'] = le.fit_transform(X_train['your_column'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "e27b100a-c6fa-4b69-a7df-8b6a5794d5dd",
   "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>Pclass</th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Fare</th>\n",
       "      <th>Cabin</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>888</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>23.4500</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>394</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>24.0</td>\n",
       "      <td>0</td>\n",
       "      <td>16.7000</td>\n",
       "      <td>G6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>84</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>17.0</td>\n",
       "      <td>0</td>\n",
       "      <td>10.5000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>689</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>15.0</td>\n",
       "      <td>0</td>\n",
       "      <td>211.3375</td>\n",
       "      <td>B5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>862</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>48.0</td>\n",
       "      <td>0</td>\n",
       "      <td>25.9292</td>\n",
       "      <td>D17</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>654</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>18.0</td>\n",
       "      <td>0</td>\n",
       "      <td>6.7500</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>251</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>29.0</td>\n",
       "      <td>1</td>\n",
       "      <td>10.4625</td>\n",
       "      <td>G6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>729</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>25.0</td>\n",
       "      <td>1</td>\n",
       "      <td>7.9250</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>705</th>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>39.0</td>\n",
       "      <td>0</td>\n",
       "      <td>26.0000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>69</th>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>26.0</td>\n",
       "      <td>2</td>\n",
       "      <td>8.6625</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>668 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     Pclass  Sex   Age  SibSp      Fare Cabin\n",
       "888       3    1   NaN      1   23.4500   NaN\n",
       "394       3    1  24.0      0   16.7000    G6\n",
       "84        2    1  17.0      0   10.5000   NaN\n",
       "689       1    1  15.0      0  211.3375    B5\n",
       "862       1    1  48.0      0   25.9292   D17\n",
       "..      ...  ...   ...    ...       ...   ...\n",
       "654       3    1  18.0      0    6.7500   NaN\n",
       "251       3    1  29.0      1   10.4625    G6\n",
       "729       3    1  25.0      1    7.9250   NaN\n",
       "705       2    0  39.0      0   26.0000   NaN\n",
       "69        3    0  26.0      2    8.6625   NaN\n",
       "\n",
       "[668 rows x 6 columns]"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "0ed0f9ae-2177-454d-aeee-1e7048b4fb0d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "The accuracy score for RandomForest:  0.7937219730941704\n",
      "\n",
      "============== Metric ===============================\n",
      "\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.83      0.86      0.84       145\n",
      "           1       0.72      0.67      0.69        78\n",
      "\n",
      "    accuracy                           0.79       223\n",
      "   macro avg       0.78      0.76      0.77       223\n",
      "weighted avg       0.79      0.79      0.79       223\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import classification_report, accuracy_score, confusion_matrix\n",
    "\n",
    "\n",
    "# make a copy of the algorithm\n",
    "RF_clf = RandomForestClassifier(random_state=42)\n",
    "# fits a model using training data\n",
    "RF_clf.fit(X_train, y_train)\n",
    "\n",
    "X_train, X_test = X_train.align(X_test, join='left', axis=1, fill_value=0)\n",
    "# check with test data\n",
    "y_pred = RF_clf.predict(X_test)\n",
    "\n",
    "# Print the accuracy score\n",
    "print('\\nThe accuracy score for RandomForest: ', accuracy_score(y_test, y_pred))\n",
    "\n",
    "print(\"\\n============== Metric ===============================\\n\")\n",
    "# Print the evaluation metrics\n",
    "print(classification_report(y_test, y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "27e04ac0-7e8d-476c-b849-7122292228b5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "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.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
