Meta Platforms uses feature vectors to represent content such as Instagram Reels. Implement a k-nearest neighbors classifier that predicts each validation item's label from labeled training vectors, then computes validation accuracy.
Given train_features, a list of numeric vectors, and train_labels, the corresponding string labels, classify every vector in validation_features. Use squared Euclidean distance, which avoids an unnecessary square root. For each validation vector, select the k closest training vectors. The predicted label is the label with the highest frequency among those neighbors. If multiple labels tie, return the lexicographically smallest label.
Return a dictionary with predictions, a list of predicted labels in validation order, and accuracy, the fraction of predictions equal to validation_labels.
def knn_classify(train_features, train_labels, validation_features, validation_labels, k):