Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Apply the k-Nearest Neighbor algorithm to classify the test data for k=1 and k=3

ID: 3903759 • Letter: A

Question

Apply the k-Nearest Neighbor algorithm to classify the test data for k=1 and k=3 using the Euclidean distance metric. Show the resulting distance matrix used for the classification. Use Excel or Python please.

Training Set

#

x1

x2

true label

1

0.453705

-0.0106

1

2

3.258589

0.169734

1

3

3.184656

-0.83691

0

4

-0.42561

1.385033

0

5

0.658765

-1.87715

0

6

-0.40507

-1.9574

0

7

-4.52775

4.123102

1

8

2.538689

-1.5386

1

9

-1.04649

-3.59664

1

10

2.967113

0.505111

0

Testing Set

#

x1

x2

true label

predicted label

11

-4.69237

-4.77898

1

12

-2.1147

-1.81277

0

13

4.277164

-4.83136

1

14

-1.33862

-0.93995

0

15

-4.02728

-4.96129

1

16

4.968125

3.757161

1

17

-2.19987

-3.48712

0

18

2.849136

-3.33965

0

19

-4.30273

2.530094

1

20

4.690116

-0.36379

1

Training Set

#

x1

x2

true label

1

0.453705

-0.0106

1

2

3.258589

0.169734

1

3

3.184656

-0.83691

0

4

-0.42561

1.385033

0

5

0.658765

-1.87715

0

6

-0.40507

-1.9574

0

7

-4.52775

4.123102

1

8

2.538689

-1.5386

1

9

-1.04649

-3.59664

1

10

2.967113

0.505111

0

Explanation / Answer

Hello Student!

Here goes the code :

***** Code ends here ****

Output for k = 3

Predicted value =1, Actual value =1
Predicted value =1, Actual value =0
Predicted value =1, Actual value =1
Predicted value =1, Actual value =0
Predicted value =1, Actual value =1
Predicted value =1, Actual value =1
Predicted value =1, Actual value =0
Predicted value =1, Actual value =0
Predicted value =0, Actual value =1
Predicted value =1, Actual value =1
Accuracy: 50.0%

Output for k = 1

Predicted value =1, Actual value =1
Predicted value =0, Actual value =0
Predicted value =1, Actual value =1
Predicted value =0, Actual value =0
Predicted value =1, Actual value =1
Predicted value =0, Actual value =1
Predicted value =1, Actual value =0
Predicted value =1, Actual value =0
Predicted value =1, Actual value =1
Predicted value =1, Actual value =1
Efficiency: 70.0%

Thank you. Feel free to ask anything. If you like the answer. Please upvote it. It means a lot. Thank you again.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote