Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

from sklearn.

model_selection import train_test_split


from sklearn.neighbors import KNeighborsClassifier
from sklearn.datasets import load_iris
from sklearn import metrics

# Load Iris dataset


iris_data = load_iris()

# Separate features (X) and target variable (y)


X = iris_data.data
y = iris_data.target

# Split the dataset into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,
random_state=42)

# Initialize the k-Nearest Neighbors classifier


knn_classifier = KNeighborsClassifier(n_neighbors=3)

# Train the classifier


knn_classifier.fit(X_train, y_train)

# Make predictions
predictions = knn_classifier.predict(X_test)

# Evaluate performance
accuracy=metrics.accuracy_score(y_test,predictions)

# Print accuracy
print(f'Accuracy: {accuracy}')
print("classification report")
# Print classification report
print(metrics.classification_report(y_test, predictions))

# Print confusion matrix:")


print(metrics.confusion_matrix(y_test, predictions))

You might also like