This python code implements the K-nearest neighbor algorithm. It is asked to convert this code to weighted K-nearest neighbor algorithm. Needed information and formulas about WKNN algorithm is given in the second photo. 
import numpy as np import pandas as pd train = pd. read_csv('HW6Train.csv') test = pd. read_csv('HW6Test.csv') def euclidean_distance (rowl, row2): return np.sqrt(np. sum((row1 [:-1] - row2 [:-1])**2)) def get_neighbors (train, test_instance, num_neighbors): distances = [] train_copy=train.copy() for j in range (len (train)): dist = euclidean_distance (test_instance, train.iloc [j, :]) distances.append(dist) train_copy ['distance']=distances return train_copy.nsmallest (num_neighbors, ['distance']) def predict_classes (train, test, num_neighbors): prediction=[] for j in range (len (test)): neighbors = get_neighbors (train, test.iloc [j, :], num_neighbors) predicted_class = neighbors.variety.value_counts().index [0] prediction.append (predicted_class) return prediction def compute_accuracy (prediction, test_y): return np.mean(prediction==test_y)
Your task: We have learned how to implement K-nearest neighbors algorithm in class. Now, you are asked to write weighted k-nearest neighbors algorithm. Note that, in KNN, a point x is assigned to the class most present among the K points in the training set nearest to x. On the contrary, with the Weighted KNN every neighbor has an associated weight; in the final decision, each neighbor counts with its own weight and the class with the highest weighted sum is chosen. For instance, in the example below (see figure), x is assigned to circle with KNN (K-3), since the most occurring class around x is circle, however, with weighted KNN (K-3), we sum the weights of the nearest K neighboring points for each class and since the sum of weights of squares (0.9) is larger than the sum of weights of circles (0.3+0.5-0.8), x is assigned to square. ?X 0.9 class(x) ...> class(x) ...> (a) (b) The weights of every neighboring K points are chosen as the 1/Euclidean distances of the training points to the test point x. Wnid(x,n?) where n, is one of the neighboring points and d(x, n) is the Euclidean distance between these two points. In other words, the farther the point, the less weight it has. Function Parameters: train: This is the training data set used to train your model. Read it as a pandas dataframe. Note that you should not further split the training set as the training set and test set are given separately. test: This is the test data set used to make predictions and test the performance of your model. Read it as a pandas dataframe. Please note that test data set should not be used during the training of the model and only be used to make predictions and compute the accuracy. number_neighbors: Number of neighbors (k) Function Outputs: Predictions: Your function should return class predictions for the test set and type of predictions should be a numpy array. Accuracy: You should also report overall accuracy of your model in the test set. The type of the reported accuracy should be a numpy float. 0.5 O