Problem 2: Compare Different Values of k
Easypython
Learn the Concept: K-Nearest NeighborsProblem Description
Write a Python program that trains KNN classifiers with k=1, k=3, and k=5 on the same dataset, and prints the prediction for the same new data point using each value of k.
Input
A labeled dataset and one new data point.
Output
The prediction from each of the three k values.
Constraints
- At least 8 labeled training examples.
Example Input
textX=[[1],[2],[3],[7],[8],[9],[10],[11]], y=[0,0,0,1,1,1,1,1], new=[[6]]
Example Output
textk=1: [0] k=3: [1] k=5: [1]
Concepts Covered
n_neighbors parameter, comparing hyperparameter choices.
Related Problems
- Problem 1: Basic KNN ClassificationEasy · pythonSolve Problem
- Problem 3: Scale Features Before Applying KNNEasy · pythonSolve Problem
- Problem 4: Manual Euclidean Distance CalculatorEasy · pythonSolve Problem