Spaces:
Sleeping
Sleeping
binary knn added
Browse files- KNN.py +126 -0
- __pycache__/KNN.cpython-313.pyc +0 -0
- app.py +5 -5
- knn_final_model.pkl +3 -0
KNN.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from collections import Counter
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class CustomKNN:
|
| 6 |
+
def __init__(self, k=5, weights="uniform"):
|
| 7 |
+
"""
|
| 8 |
+
k: Number of nearest neighbors to use.
|
| 9 |
+
weights: "uniform" for majority vote or "distance" for inverse-distance voting.
|
| 10 |
+
"""
|
| 11 |
+
if not isinstance(k, int) or k <= 0:
|
| 12 |
+
raise ValueError("k must be a positive integer.")
|
| 13 |
+
if weights not in {"uniform", "distance"}:
|
| 14 |
+
raise ValueError('weights must be either "uniform" or "distance".')
|
| 15 |
+
|
| 16 |
+
self.k = k
|
| 17 |
+
self.weights = weights
|
| 18 |
+
self.X_train = None
|
| 19 |
+
self.y_train = None
|
| 20 |
+
|
| 21 |
+
def fit(self, X, y):
|
| 22 |
+
"""
|
| 23 |
+
KNN is a lazy learner; training memorizes the feature matrix and labels.
|
| 24 |
+
"""
|
| 25 |
+
X = np.asarray(X, dtype=float)
|
| 26 |
+
y = np.asarray(y)
|
| 27 |
+
|
| 28 |
+
if X.ndim != 2:
|
| 29 |
+
raise ValueError("X must be a 2D array of shape (n_samples, n_features).")
|
| 30 |
+
if y.ndim != 1:
|
| 31 |
+
raise ValueError("y must be a 1D array.")
|
| 32 |
+
if len(X) != len(y):
|
| 33 |
+
raise ValueError("X and y must contain the same number of samples.")
|
| 34 |
+
if self.k > len(X):
|
| 35 |
+
raise ValueError("k cannot be larger than the number of training samples.")
|
| 36 |
+
|
| 37 |
+
self.X_train = X
|
| 38 |
+
self.y_train = y
|
| 39 |
+
return self
|
| 40 |
+
|
| 41 |
+
def predict(self, X):
|
| 42 |
+
"""
|
| 43 |
+
Predict labels for a 2D array of samples.
|
| 44 |
+
"""
|
| 45 |
+
if self.X_train is None or self.y_train is None:
|
| 46 |
+
raise ValueError("KNN has not been trained. Call fit() first.")
|
| 47 |
+
|
| 48 |
+
X = np.asarray(X, dtype=float)
|
| 49 |
+
if X.ndim == 1:
|
| 50 |
+
X = X.reshape(1, -1)
|
| 51 |
+
if X.ndim != 2:
|
| 52 |
+
raise ValueError("X must be a 1D sample or 2D array of samples.")
|
| 53 |
+
if X.shape[1] != self.X_train.shape[1]:
|
| 54 |
+
raise ValueError(
|
| 55 |
+
f"Expected {self.X_train.shape[1]} features, got {X.shape[1]}."
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
return np.array([self._predict_single(x) for x in X])
|
| 59 |
+
|
| 60 |
+
def _predict_single(self, x):
|
| 61 |
+
distances = np.linalg.norm(self.X_train - x, axis=1)
|
| 62 |
+
k_indices = np.argpartition(distances, self.k - 1)[: self.k]
|
| 63 |
+
k_labels = self.y_train[k_indices]
|
| 64 |
+
|
| 65 |
+
if self.weights == "distance":
|
| 66 |
+
k_distances = distances[k_indices]
|
| 67 |
+
zero_distance_mask = k_distances == 0
|
| 68 |
+
if np.any(zero_distance_mask):
|
| 69 |
+
return Counter(k_labels[zero_distance_mask]).most_common(1)[0][0]
|
| 70 |
+
|
| 71 |
+
vote_totals = {}
|
| 72 |
+
for label, distance in zip(k_labels, k_distances):
|
| 73 |
+
vote_totals[label] = vote_totals.get(label, 0.0) + (1.0 / distance)
|
| 74 |
+
return max(vote_totals, key=vote_totals.get)
|
| 75 |
+
|
| 76 |
+
return Counter(k_labels).most_common(1)[0][0]
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def find_optimal_k(X_train, y_train, X_val, y_val, max_k=15, weights="distance"):
|
| 80 |
+
"""
|
| 81 |
+
Evaluate odd k values from 1 to max_k and return the k with the lowest
|
| 82 |
+
validation error.
|
| 83 |
+
"""
|
| 84 |
+
if max_k < 1:
|
| 85 |
+
raise ValueError("max_k must be at least 1.")
|
| 86 |
+
|
| 87 |
+
max_k = min(max_k, len(X_train))
|
| 88 |
+
k_values = list(range(1, max_k + 1, 2))
|
| 89 |
+
errors = []
|
| 90 |
+
|
| 91 |
+
print(f"{'K Value':<10} | {'Validation Error':<20} | {'Accuracy':<10}")
|
| 92 |
+
print("-" * 45)
|
| 93 |
+
|
| 94 |
+
for k in k_values:
|
| 95 |
+
knn = CustomKNN(k=k, weights=weights).fit(X_train, y_train)
|
| 96 |
+
y_pred = knn.predict(X_val)
|
| 97 |
+
accuracy = np.mean(y_pred == y_val)
|
| 98 |
+
error = 1.0 - accuracy
|
| 99 |
+
errors.append(error)
|
| 100 |
+
|
| 101 |
+
print(f"{k:<10} | {error:<20.4f} | {accuracy:<10.4f}")
|
| 102 |
+
|
| 103 |
+
best_idx = int(np.argmin(errors))
|
| 104 |
+
best_k = k_values[best_idx]
|
| 105 |
+
|
| 106 |
+
print("-" * 45)
|
| 107 |
+
print(f"Optimal K found at K = {best_k} with Error = {errors[best_idx]:.4f}")
|
| 108 |
+
|
| 109 |
+
return best_k
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
def make_knn_trainer(k, weights="distance"):
|
| 113 |
+
"""
|
| 114 |
+
Build a train_fn compatible with run_kfold.
|
| 115 |
+
"""
|
| 116 |
+
def train_knn(X, y):
|
| 117 |
+
return CustomKNN(k=k, weights=weights).fit(X, y)
|
| 118 |
+
|
| 119 |
+
return train_knn
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
def predict_knn(model, X):
|
| 123 |
+
"""
|
| 124 |
+
predict_fn compatible with run_kfold.
|
| 125 |
+
"""
|
| 126 |
+
return model.predict(X)
|
__pycache__/KNN.cpython-313.pyc
ADDED
|
Binary file (6.59 kB). View file
|
|
|
app.py
CHANGED
|
@@ -18,7 +18,7 @@ def load_pickle_file(file_name):
|
|
| 18 |
except Exception:
|
| 19 |
return None
|
| 20 |
|
| 21 |
-
|
| 22 |
gnb_package = load_pickle_file("gnb_digit6_package.pkl")
|
| 23 |
pca_multi = load_pickle_file("pcaMulti.pkl")
|
| 24 |
pca_multi = pca_multi['pca']
|
|
@@ -33,14 +33,14 @@ else:
|
|
| 33 |
pca = None
|
| 34 |
best_weight = None
|
| 35 |
hog_params = None
|
| 36 |
-
|
| 37 |
dt_package = load_pickle_file("dt.pkl")
|
| 38 |
|
| 39 |
if dt_package is not None:
|
| 40 |
dt_binary_model = dt_package["model"]
|
| 41 |
else:
|
| 42 |
dt_binary_model = None
|
| 43 |
-
|
| 44 |
logreg_binary_model = load_pickle_file("custom_logreg_hog.pkl")
|
| 45 |
|
| 46 |
svm_package = load_pickle_file("svm2.pkl")
|
|
@@ -58,7 +58,7 @@ svm_scaler = load_pickle_file("svmScaler.pkl")
|
|
| 58 |
# =========================
|
| 59 |
|
| 60 |
# Binary KNN model is not inserted yet.
|
| 61 |
-
knn_binary_model =
|
| 62 |
|
| 63 |
# Multimode models are not inserted yet.
|
| 64 |
gnb_multimode_model = load_pickle_file('gnbMulti.pkl')
|
|
@@ -83,7 +83,7 @@ def preprocess_image(image, model_choice, mode_choice):
|
|
| 83 |
)
|
| 84 |
|
| 85 |
if mode_choice == "Binary":
|
| 86 |
-
if model_choice == "GNB":
|
| 87 |
image_hog = image_hog.reshape(1, -1)
|
| 88 |
image_pca = pca.transform(image_hog)
|
| 89 |
return image_pca
|
|
|
|
| 18 |
except Exception:
|
| 19 |
return None
|
| 20 |
|
| 21 |
+
print("Loading GNB model... (this might take a few seconds)")
|
| 22 |
gnb_package = load_pickle_file("gnb_digit6_package.pkl")
|
| 23 |
pca_multi = load_pickle_file("pcaMulti.pkl")
|
| 24 |
pca_multi = pca_multi['pca']
|
|
|
|
| 33 |
pca = None
|
| 34 |
best_weight = None
|
| 35 |
hog_params = None
|
| 36 |
+
print("Loading Decision Tree...")
|
| 37 |
dt_package = load_pickle_file("dt.pkl")
|
| 38 |
|
| 39 |
if dt_package is not None:
|
| 40 |
dt_binary_model = dt_package["model"]
|
| 41 |
else:
|
| 42 |
dt_binary_model = None
|
| 43 |
+
print("Loading Logistic Regression...")
|
| 44 |
logreg_binary_model = load_pickle_file("custom_logreg_hog.pkl")
|
| 45 |
|
| 46 |
svm_package = load_pickle_file("svm2.pkl")
|
|
|
|
| 58 |
# =========================
|
| 59 |
|
| 60 |
# Binary KNN model is not inserted yet.
|
| 61 |
+
knn_binary_model = load_pickle_file('knn_final_model.pkl')
|
| 62 |
|
| 63 |
# Multimode models are not inserted yet.
|
| 64 |
gnb_multimode_model = load_pickle_file('gnbMulti.pkl')
|
|
|
|
| 83 |
)
|
| 84 |
|
| 85 |
if mode_choice == "Binary":
|
| 86 |
+
if model_choice == "GNB" or model_choice == "KNN":
|
| 87 |
image_hog = image_hog.reshape(1, -1)
|
| 88 |
image_pca = pca.transform(image_hog)
|
| 89 |
return image_pca
|
knn_final_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6fd5ab59b64bc895d5014509fdefb53f115faad8177f8633cc3f2e5603d43c6f
|
| 3 |
+
size 24480315
|