-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkthnearest.py
More file actions
68 lines (53 loc) · 2.17 KB
/
kthnearest.py
File metadata and controls
68 lines (53 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, classification_report
from sklearn.datasets import load_iris
# Load the Iris dataset
iris = load_iris()
X = iris.data # Features
y = iris.target # Labels
# Split the data into training and test sets (80% train, 20% test)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Create and train the k-NN model
k = 5 # Number of neighbors
knn = KNeighborsClassifier(n_neighbors=k)
knn.fit(X_train, y_train)
# Make predictions
y_pred = knn.predict(X_test)
# Evaluate accuracy
accuracy = accuracy_score(y_test, y_pred)
print("Model accuracy %.4f" % accuracy)
# Print classification report
print("\nClassification Report:\n", classification_report(y_test, y_pred))
# Print correct and incorrect predictions
print("\nPredictions Analysis:")
correct_predictions = []
incorrect_predictions = []
for i in range(len(y_test)):
if y_test[i] == y_pred[i]:
correct_predictions.append((X_test[i], y_test[i], y_pred[i]))
else:
incorrect_predictions.append((X_test[i], y_test[i], y_pred[i]))
# Print correct predictions
# Print all predictions with correctness
print("\nCorrect Predictions:")
for sample, true_label, pred_label in correct_predictions:
print("Sample:", sample)
print("True Label:", iris.target_names[true_label])
print("Predicted Label:", iris.target_names[pred_label])
print()
# Print incorrect predictions
if incorrect_predictions:
print("\nIncorrect Predictions:")
for sample, true_label, pred_label in incorrect_predictions:
print(f"Sample: {sample}, True Label: {iris.target_names[true_label]}, Predicted Label: {iris.target_names[pred_label]}")
else:
print("No incorrect predictions!")
# for sample, true_label, pred_label in zip(X_test, y_test, y_pred):
# label_true = iris.target_names[true_label]
# label_pred = iris.target_names[pred_label]
# status = "Correct" if true_label == pred_label else "Incorrect"
# print(f"{status}: {label_true} → {label_pred}")
# print("No incorrect predictions")