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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started