0% found this document useful (0 votes)
24 views7 pages

KNN Classifier Implementation Guide

This document describes implementing a KNN classifier on a CSV dataset containing medical data. The KNN algorithm works by finding the distances between query data and examples in the dataset, selecting the K closest examples, and predicting the class or average of labels. The code loads the CSV file, preprocesses the data by filling missing values and normalizing features, splits the data into training and test sets, trains a KNN model with 5 neighbors on the training set, makes predictions on the test set, and evaluates accuracy.

Uploaded by

Yellow Moustache
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)
24 views7 pages

KNN Classifier Implementation Guide

This document describes implementing a KNN classifier on a CSV dataset containing medical data. The KNN algorithm works by finding the distances between query data and examples in the dataset, selecting the K closest examples, and predicting the class or average of labels. The code loads the CSV file, preprocesses the data by filling missing values and normalizing features, splits the data into training and test sets, trains a KNN model with 5 neighbors on the training set, makes predictions on the test set, and evaluates accuracy.

Uploaded by

Yellow Moustache
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

Machine Learning Lab Assessment 3

18BCE2301
Devangshu Mazumder

Aim:
Design and implement a KNN Classifier using a csv file

Csv file: [Link]

Abstract:
The abbreviation KNN stands for “K-Nearest Neighbour”. It is a supervised machine learning
algorithm. The algorithm can be used to solve both classification and regression problem
statements. The number of nearest neighbours to a new unknown variable that has to be predicted
or classified is denoted by the symbol 'K'.

KNN works by finding the distances between a query and all the examples in the data, selecting the
specified number examples (K) closest to the query, then votes for the most frequent label (in the
case of classification) or averages the labels (in the case of regression).

Sample Code:
import numpy as np

import pandas as pd

import [Link] as plt

from [Link] import KNeighborsClassifier

from sklearn import datasets

from sklearn.model_selection import train_test_split

from [Link] import confusion_matrix, accuracy_score

dataset = pd.read_csv("C:\\Users\\skull\\BCE2301\\vir\\env\\Machine Learning Lab


CSE4020/[Link]",
names=['age','sex','cp','trestbps','chol','fbs','restecg','thalach','exang','oldpeak','slope','ca','thal','output'])

dataset_mean= dataset

print("**Before Filling missing values***")


print(dataset_mean.loc[287])

dataset1=dataset_mean

df1=[Link](dataset1)

print("**Mean of Coloumn 11**")

print(df1['ca'].mean())

[Link]([Link](), inplace=True)

print("**After Filling missing values**")

print([Link][[166,192,287,302]])

print("**Mean of Coloumn 12**")

print(df1['thal'].mean())

[Link]([Link](), inplace=True)

print("**After Filling missing values**")

print([Link][[87,266]])

feature_cols = list([Link][0:13])

print("Feature coloumns: \n{}".format(feature_cols))

#Separate the data into feature data and target data

X= dataset[feature_cols]

y= dataset['output'].values

print("\nFeature values:")

[Link]

#split the dataset into training and testing data

X_train,X_test , y_train, y_test = train_test_split(X,y, test_size=0.30, random_state=5)

print(X_train)

#Normalization

from [Link] import StandardScaler


scaler = StandardScaler()

[Link](X_train)

X_train = [Link](X_train)

print("**After Z-score normalization on X_train***")

print(X_train)

[Link](X_test)

X_test = [Link](X_test)

print("**After Z-score Normalization on X_test***")

print(X_test)

print("KNN CLASSIFER")

clf2 = KNeighborsClassifier(n_neighbors=5)

[Link](X_train,y_train)

y_predictions = [Link](X_test)

cm1 = confusion_matrix(y_test, y_predictions)

print("Accuracy=",accuracy_score(y_test, y_predictions))
OUTPUT:

Common questions

Powered by AI

The 'K' in K-Nearest Neighbour denotes the number of nearest neighbors considered when making predictions. In classification tasks, the algorithm selects the 'K' closest training examples in the feature space and assigns the majority label among those neighbors to the query point. The value of 'K' is crucial because it impacts the model's bias-variance tradeoff. A smaller 'K' can lead to high variance and model overfitting because the decision boundaries become sensitive to noise in the dataset. Conversely, a larger 'K' smooths out the decision boundaries, reducing variance but potentially introducing bias as important local patterns may be ignored. It's essential to tune 'K' based on the validation data or through cross-validation to achieve the best performance .

You might also like