0% found this document useful (0 votes)
4 views1 page

PRGM 8

Uploaded by

ansaarkhan498
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

PRGM 8

Uploaded by

ansaarkhan498
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import pandas as pd

import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score

# Load the Iris dataset


iris = load_iris()
X = iris.data
y = iris.target

# Split the data into training and testing sets (50% test size to reduce training
accuracy)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5,
random_state=0)

# Initialize the k-NN classifier with k=10 to increase misclassifications


knn = KNeighborsClassifier(n_neighbors=10)

# Train the classifier


knn.fit(X_train, y_train)

# Make predictions on the test set


y_pred = knn.predict(X_test)

# Calculate the accuracy


accuracy = accuracy_score(y_test, y_pred)
print(f'Accuracy: {accuracy:.2f}')

# Print correct and wrong predictions


correct_predictions = []
wrong_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:
wrong_predictions.append((X_test[i], y_test[i], y_pred[i]))

print("\nCorrect Predictions:")
for sample, true_label, predicted_label in correct_predictions:
print(f'Sample: {sample}, True Label: {true_label}, Predicted Label:
{predicted_label}')

print("\nWrong Predictions:")
for sample, true_label, predicted_label in wrong_predictions:
print(f'Sample: {sample}, True Label: {true_label}, Predicted Label:
{predicted_label}')

You might also like