KNN Algorithm Edt
KNN Algorithm Edt
K-NN ALGORITHM
Aim:
To write a python code to implement K-Nearest Neighbour algorithms for the given data set.
Algorithm:
Step 1: Start the process
Step 2: Load the necessary libraries such a pandas, numpy, sklearn.
Step 3: Load the abalone dataset into the Google colab.
Step 4: Fetch the necessary feature from the dataset.
Step 5: Split the dataset into training set and test set
Step 6: Perform the k-nn algorithm for the abalone dataset.
Step 7: Calculate the accuracy for different number of neighbors.
Step 8: Display the result.
Step 9: Stop the process.
Coding:
#Import the necessary libraries
import numpy as np import
pandas as pd import csv
import seaborn as sns import
matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix from
sklearn.datasets import make_blobs from
sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
#Load the dataset into the colab from the google
drive df =
pd.read_csv("/content/drive/MyDrive/ml_lab/abalon
e.csv") df
X = df.drop(['Sex'],axis=1)
X
y = df['Sex'] y
#Split the dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state = 42)
knn5.fit(X_train, y_train) knn3.fit(X_train, y_train) knn1.fit(X_train,
y_train) knn4.fit(X_train, y_train)