Prepin
Log in

Execute a k-nearest neighbors algorithm.

Approach / Explanation

This question requires you to write code that implements the k-nearest neighbors (k-NN) algorithm. The k-NN algorithm is a form of instance-based or lazy learning, where the function is approximated locally and all computations are postponed until classification or regression occurs. It is commonly applied in pattern recognition and computer vision tasks. For this question, you will need to understand and explain the fundamentals of the k-NN algorithm, how it operates, and produce functional code. Additionally, you might need to discuss the selection of the value of k and the distance metric used for measurement. To effectively address this question, you should concentrate on: A comprehensive explanation of the k-NN algorithm, including its prediction methods. The process of calculating distance in the k-NN algorithm and the significance of feature scaling. The strategy for determining the optimal k value. A functional code example that implements the k-NN algorithm.

Suggested Answer

The k-Nearest Neighbors (k-NN) algorithm is one of the most straightforward machine learning algorithms. It falls under the category of instance-based learning, where the function is approximated locally, and all calculations are postponed until the prediction phase. In the k-NN algorithm, the result is either class membership for classification tasks or an average of values for regression tasks. Classification is determined by a majority vote of the neighboring instances, with the object being assigned to the class that appears most frequently among its k nearest neighbors. Now, let’s look at a basic implementation in Python using the Euclidean distance as the distance measure. from collections import Counter def euclidean_distance ( point1 , point2 ) : return sum ( ( a - b ) ** 2 for a , b in zip ( point1 , point2 ) ) ** 0.5 def k_nearest_neighbors ( data , query , k , distance_fn ) : neighbor_distances_and_indices = [ ] for index , example in enumerate ( data ) : distance = distance_fn ( example [ : - 1 ] , query ) neighbor_distances_and_indices . append ( ( distance , index ) ) sorted_neighbor_distances_and_indices = sorted ( neighbor_distances_and_indices ) k_nearest_distances_and_indices = sorted_neighbor_distances_and_indices [ : k ] k_nearest_labels = [ data [ i ] [ - 1 ] for distance , i in k_nearest_distances_and_indices ] return k , Counter ( k_nearest_labels ) . most_common ( 1 ) [ 0 ] [ 0 ] The above code provides an implementation of the k-NN algorithm from scratch. The k_nearest_neighbors function computes the Euclidean distance between the query example and each example in the dataset, sorts these distances, selects the top k nearest, and conducts a majority vote on their labels. The Counter class is utilized to count hashable objects in the form of (label, count).

Alternative Answer

To enhance code organization, the k-NN algorithm can be implemented as a class in Python. Below is a straightforward example. import numpy as np from collections import Counter class KNN : def __init__ ( self , k ) : self . k = k def fit ( self , X , y ) : self . X_train = X self . y_train = y def predict ( self , X ) : predicted_labels = [ self . _predict ( x ) for x in X ] return np . array ( predicted_labels ) def _predict ( self , x ) : distances = [ euclidean_distance ( x , x_train ) for x_train in self . X_train ] k_indices = np . argsort ( distances ) [ : self . k ] k_nearest_labels = [ self . y_train [ i ] for i in k_indices ] most_common = Counter ( k_nearest_labels ) . most_common ( 1 ) return most_common [ 0 ] [ 0 ] def euclidean_distance ( x1 , x2 ) : return np . sqrt ( np . sum ( ( x1 - x2 ) ** 2 ) ) This implementation defines the KNN algorithm within a class, featuring fit and predict methods akin to those in Scikit-Learn. The fit method is utilized for training the model, while the predict method is employed to determine the class for the provided test data. The _predict method is a private function that computes the Euclidean distance and returns the most frequently occurring label from the k nearest neighbors.

Question Details

Difficulty & Category

Medium
concept

Companies

Pinterest

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.