Skip to content
CampusEduX

Problem 2: Compare Different Values of k

Easypython
Learn the Concept: K-Nearest Neighbors

Problem 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

text
X=[[1],[2],[3],[7],[8],[9],[10],[11]], y=[0,0,0,1,1,1,1,1], new=[[6]]

Example Output

text
k=1: [0] k=3: [1] k=5: [1]

Concepts Covered

n_neighbors parameter, comparing hyperparameter choices.

Related Problems

  • Problem 1: Basic KNN Classification
    Easy · python
    Solve Problem
  • Problem 3: Scale Features Before Applying KNN
    Easy · python
    Solve Problem
  • Problem 4: Manual Euclidean Distance Calculator
    Easy · python
    Solve Problem

Input (stdin)

Output

Run your code to see output here...