{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "946e852d",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "0952f099",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 185260 characters]",
      "text/plain": [
       "<Figure size 1200x1200 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 28012 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Classification Report:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "        Poor       0.87      0.87      0.87      4524\n",
      "        Rich       0.62      0.62      0.62      1509\n",
      "\n",
      "    accuracy                           0.81      6033\n",
      "   macro avg       0.74      0.75      0.74      6033\n",
      "weighted avg       0.81      0.81      0.81      6033\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.tree import DecisionTreeClassifier, plot_tree\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "from sklearn.metrics import mean_squared_error, mean_absolute_error , r2_score\n",
    "from sklearn.metrics import classification_report, confusion_matrix, ConfusionMatrixDisplay\n",
    "\n",
    "# Load dataset\n",
    "df = pd.read_csv('./Datasets/adult.csv', comment = '#')\n",
    "\n",
    "# First, create a copy of the dataframe to avoid modifying the original\n",
    "df_encoded = df.copy()\n",
    "df_encoded.drop(['fnlwgt', 'education'], axis=1, inplace=True)\n",
    "\n",
    "# drop all rows that contain '?'\n",
    "for column in df_encoded.columns:\n",
    "    df_encoded = df_encoded[df_encoded[column] != '?']\n",
    "\n",
    "# Apply label encoding to categorical columns\n",
    "label_encoder = LabelEncoder()\n",
    "categorical_columns = ['workclass', 'marital.status', 'occupation', \n",
    "                      'relationship', 'race', 'sex', 'native.country', 'income']\n",
    "\n",
    "for column in categorical_columns:\n",
    "    df_encoded[column] = label_encoder.fit_transform(df_encoded[column])\n",
    "\n",
    "# Now properly separate features and target\n",
    "X = df_encoded.drop(columns=['income'])\n",
    "y = df_encoded['income']\n",
    "\n",
    "# Split the data\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
    "X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.25, random_state=42)\n",
    "\n",
    "\n",
    "# Build pipeline\n",
    "model = Pipeline([\n",
    "    ('full_dt_classifier', DecisionTreeClassifier(random_state=42))  # Train Decision Tree Regressor\n",
    "])\n",
    "\n",
    "# Train the model\n",
    "model.fit(X_train, y_train)\n",
    "y_pred = model.predict(X_val)\n",
    "\n",
    "# Visualize the decision tree\n",
    "plt.figure(figsize=(12, 12))\n",
    "plot_tree(\n",
    "    model.named_steps['full_dt_classifier'],\n",
    "    feature_names=X.columns,\n",
    "    class_names=[\"Poor\", \"Rich\"],\n",
    "    filled=True,\n",
    "    rounded=True,\n",
    "    max_depth=5,       # Keep tree shallow for readability\n",
    "    fontsize=3,\n",
    "    precision=2        # Limit decimals\n",
    ")\n",
    "\n",
    "plt.savefig('decision_tree.pdf', format='pdf', dpi=300)\n",
    "plt.show()\n",
    "\n",
    "#mse = mean_squared_error(y_val, y_pred)\n",
    "#print(f'Mean Squared Error: {mse}')\n",
    "\n",
    "CM = confusion_matrix(y_val, y_pred)\n",
    "disp = ConfusionMatrixDisplay(confusion_matrix=CM, display_labels=[\"Poor\", \"Rich\"])\n",
    "disp.plot(cmap=\"Blues\")\n",
    "plt.title(\"Confusion Matrix\")\n",
    "plt.show()  \n",
    "\n",
    "print(\"Classification Report:\")\n",
    "print(classification_report(y_val, y_pred, target_names=[\"Poor\", \"Rich\"]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e567e4e9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "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.13.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
