{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "92f9466f-a6f2-4699-b058-10c477f3751e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Initial Data Information:\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 545 entries, 0 to 544\n",
      "Data columns (total 13 columns):\n",
      " #   Column            Non-Null Count  Dtype \n",
      "---  ------            --------------  ----- \n",
      " 0   price             545 non-null    int64 \n",
      " 1   area              545 non-null    int64 \n",
      " 2   bedrooms          545 non-null    int64 \n",
      " 3   bathrooms         545 non-null    int64 \n",
      " 4   stories           545 non-null    int64 \n",
      " 5   mainroad          545 non-null    object\n",
      " 6   guestroom         545 non-null    object\n",
      " 7   basement          545 non-null    object\n",
      " 8   hotwaterheating   545 non-null    object\n",
      " 9   airconditioning   545 non-null    object\n",
      " 10  parking           545 non-null    int64 \n",
      " 11  prefarea          545 non-null    object\n",
      " 12  furnishingstatus  545 non-null    object\n",
      "dtypes: int64(6), object(7)\n",
      "memory usage: 55.5+ KB\n",
      "\n",
      "First 5 rows of the initial dataset:\n",
      "      price  area  bedrooms  bathrooms  stories mainroad guestroom basement  \\\n",
      "0  13300000  7420         4          2        3      yes        no       no   \n",
      "1  12250000  8960         4          4        4      yes        no       no   \n",
      "2  12250000  9960         3          2        2      yes        no      yes   \n",
      "3  12215000  7500         4          2        2      yes        no      yes   \n",
      "4  11410000  7420         4          1        2      yes       yes      yes   \n",
      "\n",
      "  hotwaterheating airconditioning  parking prefarea furnishingstatus  \n",
      "0              no             yes        2      yes        furnished  \n",
      "1              no             yes        3       no        furnished  \n",
      "2              no              no        2      yes   semi-furnished  \n",
      "3              no             yes        3      yes        furnished  \n",
      "4              no             yes        2       no        furnished  \n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "from sklearn.impute import KNNImputer\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "\n",
    "# Load the housing dataset\n",
    "df = pd.read_csv('Housing.csv')\n",
    "\n",
    "# Display initial information about the dataset\n",
    "print(\"Initial Data Information:\")\n",
    "df.info()\n",
    "print(\"\\nFirst 5 rows of the initial dataset:\")\n",
    "print(df.head())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f0fa6419-c123-45a0-beac-197b4424ab66",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      price  area  bedrooms  bathrooms  stories  mainroad  guestroom  \\\n",
      "0  13300000  7420         4          2        3         1          0   \n",
      "1  12250000  8960         4          4        4         1          0   \n",
      "2  12250000  9960         3          2        2         1          0   \n",
      "3  12215000  7500         4          2        2         1          0   \n",
      "4  11410000  7420         4          1        2         1          1   \n",
      "\n",
      "   basement  hotwaterheating  airconditioning  parking  prefarea  \\\n",
      "0         0                0                1        2         1   \n",
      "1         0                0                1        3         0   \n",
      "2         1                0                0        2         1   \n",
      "3         1                0                1        3         1   \n",
      "4         1                0                1        2         0   \n",
      "\n",
      "   furnishingstatus  \n",
      "0                 0  \n",
      "1                 0  \n",
      "2                 1  \n",
      "3                 0  \n",
      "4                 0  \n"
     ]
    }
   ],
   "source": [
    "\n",
    "# Step 1: Encode categorical columns\n",
    "label_encoder = LabelEncoder()\n",
    "for column in df.select_dtypes(include=['object']).columns:\n",
    "    df[column] = label_encoder.fit_transform(df[column])\n",
    "\n",
    "# Calculate the percentage of missing values in each column\n",
    "missing_percentage = df.isnull().mean() * 100\n",
    "\n",
    "# Define a threshold (30%) for dropping columns\n",
    "threshold = 30\n",
    "columns_to_drop = missing_percentage[missing_percentage > threshold].index\n",
    "\n",
    "# Drop columns with more than the threshold percentage of missing values\n",
    "df_cleaned = df.drop(columns=columns_to_drop)\n",
    "\n",
    "# Use KNN Imputer for numerical columns with missing values\n",
    "imputer = KNNImputer(n_neighbors=2)\n",
    "df_cleaned.iloc[:, :] = imputer.fit_transform(df_cleaned)\n",
    "\n",
    "print(df_cleaned.head())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c4026fc9-fd4a-493c-8f24-215c8a25eb5c",
   "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.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
