Your question is K-Nearest Neighbors From Scratch. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Implement the core prediction step of a k-nearest neighbors classifier for an Outgive machine learning workflow. Given labeled training samples and one query sample, return the class represented most often among the query's k nearest neighbors.
Use Euclidean distance over all feature dimensions. If multiple classes have the same frequency among the selected neighbors, return the lexicographically smallest class label. When distances are equal, sort the tied samples by their label before selecting neighbors, ensuring deterministic behavior.
Implement knn_predict(train_features, train_labels, query, k), where train_features is a list of numeric feature vectors, train_labels is a list of strings with one label per vector, query is a numeric feature vector with the same dimension, and k is a positive integer. Return one string containing the predicted class label.
def knn_predict(train_features, train_labels, query, k):