from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score

# 1. LOAD DATASET

iris = load_iris()

# 2. FEATURES AND TARGET

X = iris.data
y = iris.target

print("Features:", iris.feature_names)
print("Species:", iris.target_names)

# 3. 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
)

# 4. STANDARDIZATION

scaler = StandardScaler()

X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

# 5. TRAIN KNN FOR DIFFERENT K VALUES

best_k = 0
best_accuracy = 0

for k in [3, 5, 7]:

    model = KNeighborsClassifier(n_neighbors=k)
    model.fit(X_train, y_train)

    y_pred = model.predict(X_test)

    accuracy = accuracy_score(y_test, y_pred)

    print("K =", k, "Accuracy =", accuracy)

    # 6. SELECT BEST K

    if accuracy > best_accuracy:
        best_accuracy = accuracy
        best_k = k

print("\nBest K:", best_k)
print("Best Accuracy:", best_accuracy)


# ENHANCED: Training standardized sepal-length histogram
import matplotlib.pyplot as plt
plt.hist(X_train[:, 0], bins=15)
plt.xlabel("Standardized sepal length")
plt.title("Training Sepal-Length Distribution")
plt.show()
