#C12347. K-Nearest Neighbors Performance Evaluation
K-Nearest Neighbors Performance Evaluation
K-Nearest Neighbors Performance Evaluation
You are given a task to evaluate the performance of a k-Nearest Neighbors (k-NN) classifier on the classic Iris dataset. The evaluation involves normalizing the data, splitting it into training and testing subsets, and using cross-validation to search for the optimal number of neighbors (k) within a specified range. Then, using the best k, the classifier is trained on the training data and its performance is measured on the test set.
The performance metrics to be reported are:
- Best k: the optimal number of neighbors found.
- Accuracy: \(\frac{TP+TN}{TP+TN+FP+FN}\)
- Precision: \(\frac{TP}{TP+FP}\) (weighted average in a multi-class setting)
- Recall: \(\frac{TP}{TP+FN}\) (weighted average)
- F1-score: the harmonic mean of precision and recall
You are required to read the parameters from the standard input and output the five values (best k, accuracy, precision, recall, and F1-score) separated by spaces. Each metric (except best k) should be rounded to four decimal places.
Note: Although the underlying machine learning implementation uses libraries such as scikit-learn in some languages, for certain languages (like C, C++, Java, and JavaScript) you may simulate the outcome with predetermined values based on the input parameters.
inputFormat
The input consists of three space‐separated integers:
- cv: an integer representing the number of cross-validation folds.
- k_min: the lower bound (inclusive) for the k values in k-NN.
- k_max: the upper bound (exclusive) for the k values in k-NN.
For example: 5 1 11
outputFormat
Output a single line with five space‐separated values:
- best_k (integer): The optimal number of neighbors found.
- accuracy (float): Accuracy of the classifier, rounded to 4 decimal places.
- precision (float): Weighted precision, rounded to 4 decimal places.
- recall (float): Weighted recall, rounded to 4 decimal places.
- f1_score (float): Weighted F1-score, rounded to 4 decimal places.
For example: 9 0.9667 0.9667 0.9667 0.9667
5 1 11
9 0.9667 0.9667 0.9667 0.9667