PROGRAM:
KNN for Classification:
# Import necessary libraries
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, classification_report
# Load the Iris dataset
iris = load_iris()
X = iris.data
y = iris.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 and fit the KNN Classifier
knn_classifier = KNeighborsClassifier(n_neighbors=5)
# Use 5 nearest neighbors
knn_classifier.fit(X_train, y_train)
# Make predictions
y_pred = knn_classifier.predict(X_test)
# Evaluate the model
print("Classification Accuracy:", accuracy_score(y_test, y_pred))
print("\nClassification Report:\n", classification_report(y_test, y_pred))
OUTPUT:
Classification Accuracy: 1.0
Classification Report:
precision recall f1-score support
0 1.00 1.00 1.00 19
1 1.00 1.00 1.00 13
2 1.00 1.00 1.00 13
accuracy 1.00 45
macro avg 1.00 1.00 1.00 45
weighted avg 1.00 1.00 1.00 45
KNN for Regression:
# Import necessary libraries
from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsRegressor
from sklearn.metrics import mean_squared_error, r2_score
# Load the Diabetes dataset
diabetes = load_diabetes()
X = diabetes.data
y = diabetes.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 and fit the KNN Regressor
knn_regressor = KNeighborsRegressor(n_neighbors=5)
# Use 5 nearest neighbors
knn_regressor.fit(X_train, y_train)
# Make predictions
y_pred = knn_regressor.predict(X_test)
# Evaluate the model
print("Mean Squared Error:", mean_squared_error(y_test, y_pred))
print("R² Score:", r2_score(y_test, y_pred))
OUTPUT:
Mean Squared Error: 3222.117894736842
R² Score: 0.4031244536507893