[go: up one dir, main page]

0% found this document useful (0 votes)
7 views2 pages

IML Program 3

The document outlines the implementation of K-Nearest Neighbors (KNN) for both classification and regression tasks using the Iris and Diabetes datasets, respectively. The KNN classifier achieved a perfect accuracy of 1.0 on the Iris dataset, while the KNN regressor resulted in a mean squared error of approximately 3222.12 and an R² score of about 0.40 on the Diabetes dataset. The code includes data loading, model training, predictions, and evaluation metrics for both tasks.

Uploaded by

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

IML Program 3

The document outlines the implementation of K-Nearest Neighbors (KNN) for both classification and regression tasks using the Iris and Diabetes datasets, respectively. The KNN classifier achieved a perfect accuracy of 1.0 on the Iris dataset, while the KNN regressor resulted in a mean squared error of approximately 3222.12 and an R² score of about 0.40 on the Diabetes dataset. The code includes data loading, model training, predictions, and evaluation metrics for both tasks.

Uploaded by

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

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

You might also like