# Q32. Compare Decision Tree, KNN and Naive Bayes

# Step 1: Import Libraries
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import (
    accuracy_score, precision_score, recall_score,
    f1_score, confusion_matrix
)

# Step 2: Load Dataset
df = pd.read_csv("Iris.csv")
print("First Five Records:")
print(df.head())

# Step 3: Handle Missing Values and Select Features
if "Id" in df.columns:
    df = df.drop("Id", axis=1)

X = df.drop("Species", axis=1).copy()
y = df["Species"].copy()

# Fill Missing Numerical Values
X = X.fillna(X.median())

# Step 4: Common Stratified Train-Test Split
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42, stratify=y
)

# Step 5: Scale Features for KNN
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

# Step 6: Train Three Classifiers
models = {
    "Decision Tree": DecisionTreeClassifier(random_state=42),
    "KNN": KNeighborsClassifier(n_neighbors=5),
    "Naive Bayes": GaussianNB()
}

results = []
predictions = {}

for name, model in models.items():

    # KNN uses scaled features
    if name == "KNN":
        model.fit(X_train_scaled, y_train)
        y_pred = model.predict(X_test_scaled)
    else:
        model.fit(X_train, y_train)
        y_pred = model.predict(X_test)

    predictions[name] = y_pred

    results.append({
        "Model": name,
        "Accuracy": accuracy_score(y_test, y_pred),
        "Precision": precision_score(
            y_test, y_pred, average="weighted", zero_division=0
        ),
        "Recall": recall_score(
            y_test, y_pred, average="weighted", zero_division=0
        ),
        "F1-Score": f1_score(
            y_test, y_pred, average="weighted", zero_division=0
        )
    })

# Step 7: Display Comparison Table
comparison = pd.DataFrame(results)
print("\nModel Comparison:")
print(comparison.round(3).to_string(index=False))

# Step 8: Display Confusion Matrices
fig, axes = plt.subplots(1, 3, figsize=(15, 4))

for ax, name in zip(axes, models.keys()):
    cm = confusion_matrix(y_test, predictions[name], labels=models[name].classes_)

    sns.heatmap(
        cm, annot=True, fmt="d", cmap="Blues",
        xticklabels=models[name].classes_,
        yticklabels=models[name].classes_,
        ax=ax
    )

    ax.set_title(name)
    ax.set_xlabel("Predicted")
    ax.set_ylabel("Actual")

plt.tight_layout()
plt.show()

# Step 9: Recommend Model Based on F1-Score
best_model = comparison.loc[comparison["F1-Score"].idxmax()]

print("\nRecommended Model:", best_model["Model"])
print("Accuracy:", round(best_model["Accuracy"], 3))
print("F1-Score:", round(best_model["F1-Score"], 3))

# Step 10: Conclusion
print("\nComparison of all three classifiers completed.")




















