K-Nearest Neighbors Classification for Cat and Dog Images by osama zamanK-Nearest Neighbors Classification for Cat and Dog Images by osama zaman

K-Nearest Neighbors Classification for Cat and Dog Images

osama zaman

osama zaman

knn_classifation

knn_classifation

Runtime

Input Data

DATASETS

Language

Python
import os os.environ["OMP_NUM_THREADS"] = "1" os.environ["OPENBLAS_NUM_THREADS"] = "1" os.environ["MKL_NUM_THREADS"] = "1" os.environ["NUMEXPR_NUM_THREADS"] = "1" # ========================= # 1) Imports # ========================= import cv2 import numpy as np import matplotlib.pyplot as plt from sklearn.neighbors import KNeighborsClassifier from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
BASE_PATH = "/kaggle/input/cat-and-dog" TRAIN_CATS = os.path.join(BASE_PATH, "training_set", "training_set", "cats") TRAIN_DOGS = os.path.join(BASE_PATH, "training_set", "training_set", "dogs") TEST_CATS = os.path.join(BASE_PATH, "test_set", "test_set", "cats") TEST_DOGS = os.path.join(BASE_PATH, "test_set", "test_set", "dogs")
def load_images(folder_path, label, img_size=64, max_images=None): data = [] labels = [] files = os.listdir(folder_path) if max_images is not None: files = files[:max_images] for img_name in files: img_path = os.path.join(folder_path, img_name) img = cv2.imread(img_path) if img is None: continue img = cv2.resize(img, (img_size, img_size)) img = img.flatten() data.append(img) labels.append(label) return np.array(data), np.array(labels)
IMG_SIZE = 64 X_train_cat, y_train_cat = load_images(TRAIN_CATS, label=0, img_size=IMG_SIZE) X_train_dog, y_train_dog = load_images(TRAIN_DOGS, label=1, img_size=IMG_SIZE) X_train = np.vstack([X_train_cat, X_train_dog]) y_train = np.hstack([y_train_cat, y_train_dog]) X_test_cat, y_test_cat = load_images(TEST_CATS, label=0, img_size=IMG_SIZE) X_test_dog, y_test_dog = load_images(TEST_DOGS, label=1, img_size=IMG_SIZE) X_test = np.vstack([X_test_cat, X_test_dog]) y_test = np.hstack([y_test_cat, y_test_dog])
X_train = X_train.astype(np.float32) / 255.0 X_test = X_test.astype(np.float32) / 255.0
k_values = range(1, 15) error_rates = [] for k in k_values: knn_tmp = KNeighborsClassifier(n_neighbors=k, metric="euclidean") knn_tmp.fit(X_train, y_train) pred_k = knn_tmp.predict(X_test) error = np.mean(pred_k != y_test) error_rates.append(error) best_k = int(np.argmin(error_rates) + 1)
knn = KNeighborsClassifier(n_neighbors=best_k, metric="euclidean", weights="distance") knn.fit(X_train, y_train)
y_pred = knn.predict(X_test) print("\nAccuracy:", accuracy_score(y_test, y_pred)) print("\nClassification Report:\n", classification_report(y_test, y_pred)) print("\nConfusion Matrix:\n", confusion_matrix(y_test, y_pred))
import random label_map = {0: "Cat", 1: "Dog"} true_label = random.choice([0, 1]) folder = TEST_CATS if true_label == 0 else TEST_DOGS img_file = random.choice(os.listdir(folder)) img_path = os.path.join(folder, img_file) img_bgr = cv2.imread(img_path) img_resized = cv2.resize(img_bgr, (IMG_SIZE, IMG_SIZE)) x = img_resized.astype(np.float32).reshape(1, -1) / 255.0 pred = knn.predict(x)[0] proba = knn.predict_proba(x)[0] img_rgb = cv2.cvtColor(img_resized, cv2.COLOR_BGR2RGB) plt.figure() plt.imshow(img_rgb) plt.axis("off") plt.title( f"True: {label_map[true_label]} | Pred: {label_map[pred]} | " f"P(Cat)={proba[0]:.2f}, P(Dog)={proba[1]:.2f}" ) plt.show() print("Image Path:", img_path) print("True Label:", label_map[true_label]) print("Predicted :", label_map[pred]) print("Probabilities [Cat, Dog]:", proba)
Like this project

Posted May 11, 2026

K-Nearest Neighbors classification on cat and dog images using Python.