Question: Implement a KNN (k nearest neighbors) classifier from scratch without using the scikitlearn library. This means, do not use the following import line in your

Implement a KNN (k nearest neighbors) classifier from scratch without using the scikitlearn library. This means, do not use the following import line in your submitted solution: from sklearn.neighbors import KNeighborsClassifier

Implement a function called knn(), which accepts three input parameters: a single observation, a reference dataset containing more than one observations, and k parameter defining how many nearest neighbors should vote on the classification: knn(newObservation, referenceData, k=3)

The reference observation data should be a pandas dataframe, with rows as observations and columns as input variables. The last column of this dataframe should be the output/class variable. The parameter k should default to 3 (voting is based on 3 nearest neighbors). Your knn() function should not assume any dimensionality of the data and should be able to perform classification on any dimensionality of the data as long as the newObservation and the referenceData are of the same dimensions. The newObservation variable refers to a new observation for which we want to predict a class label.

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 Databases Questions!