0% found this document useful (0 votes)
41 views4 pages

KNN ALGORITHM - Ipynb - Colab

The document outlines a Jupyter Notebook that implements the K-Nearest Neighbors (KNN) algorithm using the Iris dataset. It includes data loading, preprocessing, and visualization steps, as well as model training and evaluation, achieving an accuracy of 100% with K=3. The notebook demonstrates the use of libraries such as pandas, seaborn, and scikit-learn for data analysis and machine learning tasks.

Uploaded by

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

KNN ALGORITHM - Ipynb - Colab

The document outlines a Jupyter Notebook that implements the K-Nearest Neighbors (KNN) algorithm using the Iris dataset. It includes data loading, preprocessing, and visualization steps, as well as model training and evaluation, achieving an accuracy of 100% with K=3. The notebook demonstrates the use of libraries such as pandas, seaborn, and scikit-learn for data analysis and machine learning tasks.

Uploaded by

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

1/29/25, 9:49 KNN ALGORITHM.

ipynb -
AM Colab

Start coding or ge nerate with AI.

from google.colab import drive


drive.mount('/content/drive')

Mounted at /content/drive

import numpy as
np import pandas
as pd

# Importing the dataset


dataset = pd.read_csv("/content/drive/MyDrive/Iris.csv")

dataset.shape

(150, 6)

dataset.describe()

Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm

150.000000 150.000000 150.000000 150.000000 150.000000


count
mean 75.500000 5.843333 3.054000 3.758667 1.198667

std 43.445368 0.828066 0.433594 1.764420 0.763161

min 1.000000 4.300000 2.000000 1.000000 0.100000

25% 38.250000 5.100000 2.800000 1.600000 0.300000

50% 75.500000 5.800000 3.000000 4.350000 1.300000

75% 112.750000 6.400000 3.300000 5.100000 1.800000

max 150.000000 7.900000 4.400000 6.900000 2.500000

# Let’s now take a look at the number of instances (rows) that belong to each class. We can view this as an absolute count.
dataset.groupby('Species').size()

Species

Iris-setosa 50

Iris-versicolor 50

Iris-virginica 50

dtype: int64

feature_columns = ['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm','PetalWidthCm']


X =
dataset[feature_columns].values
y = dataset['Species'].values

X = dataset.iloc[:,
1:5].values y =
dataset.iloc[:, 5].values

from sklearn.preprocessing import


LabelEncoder le = LabelEncoder()
y = le.fit_transform(y)

import matplotlib.pyplot as plt


import seaborn as sns
%matplotlib inline

plt.figure()
sns.pairplot(dataset.drop("Id", axis=1), hue = "Species", size=3, markers=["o", "s", "D"])
plt.show()

https://colab.research.google.com/drive/ 1/
1JqVsKd0IjWMC0yvZBCs7HcDrzaBVSqFx#scrollTo=TzFVc2ocSDuj&printMode=true 4
1/29/25, 9:49 KNN ALGORITHM.ipynb -
AM Colab
/usr/local/lib/python3.11/dist-packages/seaborn/axisgrid.py:2100: UserWarning: The `size` parameter has been renamed to
`height`; warnings.warn(msg, UserWarning)
<Figure size 640x480 with 0 Axes>

plt.figure()
dataset.drop("Id", axis=1).boxplot(by="Species", figsize=(15,
10)) plt.show()

https://colab.research.google.com/drive/ 2/
1JqVsKd0IjWMC0yvZBCs7HcDrzaBVSqFx#scrollTo=TzFVc2ocSDuj&printMode=true 4
1/29/25, 9:49 KNN ALGORITHM.ipynb -
AM Colab
<Figure size 640x480 with 0 Axes>

# Importing necessary libraries


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

# Importing the dataset


dataset = pd.read_csv("/content/drive/MyDrive/Iris.csv")

feature_columns = ['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm','PetalWidthCm']


X =
dataset[feature_columns].values
y = dataset['Species'].values

# 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 KNN classifier with a chosen value of K (e.g.,


K=3) knn = KNeighborsClassifier(n_neighbors=50)

# Train the model


knn.fit(X_train, y_train)

▾ i ?

KNeighborsClassifier(n_neighbors=
50)
https://colab.research.google.com/drive/ 3/
1JqVsKd0IjWMC0yvZBCs7HcDrzaBVSqFx#scrollTo=TzFVc2ocSDuj&printMode=true 4
1/29/25, 9:49 KNN ALGORITHM.ipynb -
AM # Make predictions on the test Colab
set y_pred = knn.predict(X_test)

# Evaluate the model's accuracy


accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy of KNN classifier: {accuracy *

100:.2f}%") Accuracy of KNN classifier: 95.56%

knn = KNeighborsClassifier(n_neighbors=3)

# Train the model


knn.fit(X_train, y_train)

▾ KNeighborsClassifier i ?

KNeighborsClassifier(n_neighbors=
3)

# Make predictions on the test


set y_pred = knn.predict(X_test)

# Evaluate the model's accuracy


accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy of KNN classifier: {accuracy *

100:.2f}%") Accuracy of KNN classifier: 100.00%

Start coding or ge nerate with AI.

https://colab.research.google.com/drive/ 4/
1JqVsKd0IjWMC0yvZBCs7HcDrzaBVSqFx#scrollTo=TzFVc2ocSDuj&printMode=true 4

You might also like