Question: Assignment 1 : K - nearest neighbors classifier ( KNN ) Mandatory. You are required to submit Python code with extension . py

Assignment 1: K-nearest neighbors classifier (KNN)
Mandatory. You are required to submit Python code with extension ".py" that includes an implementation of a KNN classifier and a test on it. You can use the attached template knn.py (including a minimum test) and fill the unimplemented parts there.
The submitted code must include comments that explain what the code does for every functional block (you don't have to write comments line-by-line).
Hint. It may be useful to know that a vector X (of numpy.ndarray) can be sorted according to values in a vector d (of numpy. ndarray) by: X = X[d.argsort()]
where d.argsort() returns a vector n such that n[i ] is an index at which the value d[i] should be in sorting d; see the documentation of numpy. argsort for detail. You may use this tip for sorting K training data points X according to the distances d from them to a given input.
This is the template code that I want you to fill out and the goal of it is to get an accuracy of over 70%.
class KNearestNeighborsClassifier:
# K: the number of training data points joining the voting
def __init__(self, K):
self.K = K
# fit: train this model on training inputs X and outputs Y
# X: training inputs -- np.ndarray
# (shape: [# of data points, # of features])
# Y: training outputs -- np.ndarray
# (shape: [# of data points])
def fit(self, X, Y):
# TODO: IMPLEMENT ME
pass
# predict: classify given data points
# X: inputs to the classifier -- np.ndarray
# (shape: [# of data points, # of features])
def predict(self, X):
# TODO: IMPLEMENT ME
# Hint: Euclid distances between the training inputs X_ and
# prediction inputs X with shape [# of data points, # of features] are
# calculuated by ``numpy.sqrt(((X_- X)**2.).sum(axis=1))``
pass
# check this is a main file
if __name__=='__main__':
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
iris_dataset = load_iris()
X_train, X_test, Y_train, Y_test = train_test_split(iris_dataset.data,
iris_dataset.target,
random_state=0)
knn = KNearestNeighborsClassifier(3)
knn.fit(X_train, Y_train)
X_test_predict = knn.predict(X_test)
accuracy = np.sum(Y_test == X_test_predict)/ Y_test.shape[0]
print('acc:', accuracy)
Assignment 1 : K - nearest neighbors classifier (

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!