-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
6 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,12 @@ | ||
import numpy as np | ||
from sklearn.datasets import load_iris | ||
from sklearn.model_selection import train_test_split | ||
import numpy as np | ||
from sklearn.metrics import confusion_matrix, classification_report | ||
from collections import Counter | ||
|
||
|
||
# Load iris dataset | ||
iris = load_iris() | ||
X, y = iris.data, iris.target | ||
class_names = iris.target_names | ||
|
||
# Split dataset into training set and test set | ||
X, y, names = *load_iris(return_X_y=True), load_iris().target_names | ||
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1) | ||
predict = lambda X: np.array([Counter(y_train[np.argsort([np.linalg.norm(x - x_train) for x_train in X_train])[:3]]).most_common(1)[0][0] for x in X]) | ||
y_pred = predict(X_test) | ||
|
||
class KNN: | ||
def __init__(self, k=3): | ||
self.k = k | ||
|
||
def fit(self, X, y): | ||
self.X_train = X | ||
self.y_train = y | ||
|
||
def predict(self, X): | ||
y_pred = [self._predict(x) for x in X] | ||
return np.array(y_pred) | ||
|
||
def _predict(self, x): | ||
# Compute distances between x and all examples in the training set | ||
distances = [] | ||
for x_train in self.X_train: | ||
distances.append(np.linalg.norm(x - x_train)) | ||
# Sort by distance and return indices of the first k neighbors | ||
k_indices = np.argsort(distances)[:self.k] | ||
# Extract the labels of the k nearest neighbor training samples | ||
k_nearest_labels = [self.y_train[i] for i in k_indices] | ||
# return the most common class label | ||
most_common = Counter(k_nearest_labels).most_common(1) | ||
#print(most_common) | ||
return most_common[0][0] | ||
|
||
# Create a k-NN classifier with 3 neighbors | ||
knn = KNN(k=3) | ||
|
||
# Train the model using the training sets | ||
knn.fit(X_train, y_train) | ||
|
||
# Predict the response for test dataset | ||
y_pred = knn.predict(X_test) | ||
print('Accuracy: %.4f' % np.mean(y_pred == y_test)) | ||
print("Predictions:", class_names[y_pred]) | ||
|
||
|
||
# Optional confusion matrix | ||
from sklearn.metrics import classification_report, confusion_matrix | ||
# Print confusion matrix | ||
print("\nConfusion Matrix:") | ||
print(confusion_matrix(y_test, y_pred)) | ||
|
||
# Print classification report | ||
print("\nClassification Report:") | ||
print(classification_report(y_test, y_pred)) | ||
print(f'Accuracy: {np.mean(y_pred == y_test):.4f}', "\nPredictions:", names[y_pred], "\nConfusion Matrix:\n", confusion_matrix(y_test, y_pred), "\nClassification Report:\n", classification_report(y_test, y_pred, target_names=names)) |