0% found this document useful (0 votes)
14 views11 pages

Practical 5

The document outlines a practical exercise demonstrating the k-NN classification method using the Iris dataset. It includes steps for data loading, preprocessing, visualization, splitting the dataset, training the k-NN model, and evaluating its accuracy. The final accuracy of the k-NN classifier is reported as 100% after training with k=3.

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)
14 views11 pages

Practical 5

The document outlines a practical exercise demonstrating the k-NN classification method using the Iris dataset. It includes steps for data loading, preprocessing, visualization, splitting the dataset, training the k-NN model, and evaluating its accuracy. The final accuracy of the k-NN classifier is reported as 100% after training with k=3.

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/ 11

Practical 5

Aim: Demonstrate results of k-NN classification method.

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()
GCETIT
Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm

count 150.000000 150.000000 150.000000 150.000000


150.000000
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()

0
Species

Iris-setosa 50
Iris-versicolor 50
Iris-virginica 50

GCETIT
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()

GCETIT
/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()
GCETIT
Machine Learning 42302880501053

<Figure size 640x480 with 0 Axes>

GCETIT
Machine Learning 42302880501053

# 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)

GCETIT
Machine Learning 42302880501053

# Initialize the KNN classifier with a chosen value of K (e.g.,


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

▾ KNeighborsClassifier i ?

KNeighborsClassifier(n_neighbors=50)

# Train the model


knn.fit(X_train, y_train)

# 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: 95.56%
knn = KNeighborsClassifier(n_neighbors=3)

# Train the model


knn.fit(X_train, y_train)
▾ KNeighborsClassifier i ?

KNeighborsClassifier(n_neighbors=3)

GCETIT
Machine Learning 42302880501053

# 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.

from google.colab import drive


drive.mount('/content/drive')

import numpy as np
import pandas as pd
# Importing the dataset
dataset = pd.read_csv("/content/drive/MyDrive/Iris.csv")
dataset.shape
dataset.describe()

# 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()
feature_columns = ['SepalLengthCm', 'SepalWidthCm',
'PetalLengthCm','PetalWidthCm']

GCETIT
Machine Learning 42302880501053

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()
plt.figure()
dataset.drop("Id", axis=1).boxplot(by="Species", figsize=(15,
10))
plt.show()
# 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

GCETIT
Machine Learning 42302880501053

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)
# 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}%")
knn = KNeighborsClassifier(n_neighbors=3)

GCETIT
Machine Learning 42302880501053

# Train the model


knn.fit(X_train, y_train)
# 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}%")

GCETIT

You might also like