0% found this document useful (0 votes)
59 views3 pages

K Nearest Neighbors MLExpert

This document describes using the k-nearest neighbors algorithm to classify processes as intrusive or not based on their features. It provides examples data mapping process IDs to features and labels. The task is to write code to find the k nearest neighbors of a provided feature vector and predict its label. Euclidean distance should be used to find neighbors, and no external libraries. Tests are provided to check the code returns the correct k neighbors and prediction.

Uploaded by

GEMA HERNANDEZ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views3 pages

K Nearest Neighbors MLExpert

This document describes using the k-nearest neighbors algorithm to classify processes as intrusive or not based on their features. It provides examples data mapping process IDs to features and labels. The task is to write code to find the k nearest neighbors of a provided feature vector and predict its label. Euclidean distance should be used to find neighbors, and no external libraries. Tests are provided to check the code returns the correct k neighbors and prediction.

Uploaded by

GEMA HERNANDEZ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

K Nearest Neighbors | MLExpert https://www.algoexpert.io/machine-learning/coding-questions/K%20Nea...

MLExpert Quad Layout 14px


00:00:00 | 00:00:00
Sublime

Prompt Scratchpad Our Solution(s) Video


Your
Explanation
Solutions Run Code

Solution 1 Solution 2 Solution 3


Category: Model Concepts
1 # Should use the `find_k_nearest_neighbors`

K Nearest Neighbors 2
3
def predict_label(examples, features, k, lab
# Write your code here.
4 pass
Use the k-nearest neighbors algorithm to
5
return the k nearest neighbors of the 6
provided features. 7 def find_k_nearest_neighbors(examples, featu
8 # Write your code here.
These features are the result of a 9 pass
dimensionality reduction by PCA on some 10
operating-system data related to processes
and their intrusivity in some network. You'll
have access to an EXAMPLES dictionary,
mapping each process identi�er "pid_i" to
a respective dictionary containing its
associated features as well as a label
representing whether the relevant process
was intrusive to the network. A label of 0
means that it wasn't intrusive, while a label of
1 means that it was intrusive.

Below is an example portion of the


EXAMPLES :

{
"pid_0": {
"features": [3.968642003034218, 3.9553899901567955,
"is_intrusive": 0,
},
"pid_1": {
"features": [3.905930716908446, 3.9843869514613046,
"is_intrusive": 0,
},
# ...
# More of the same kind of data.
"pid_500": {
"features": [4.309361217767318, 4.287392829732823,
"is_intrusive": 1,
},
"pid_501": {

1 de 3 21/04/2022 08:42 p. m.
K Nearest Neighbors | MLExpert https://www.algoexpert.io/machine-learning/coding-questions/K%20Nea...

"pid_501": {
"features": [4.310938448487633, 4.298506635010131,
"is_intrusive": 1,
},
# ...
# More of the same kind of data.
}

Note that:

• You should use the Euclidean distance


as the distance metric when �nding the
k-nearest-neighbors.

• You shouldn't use any libraries that

Tests Quick Test Sandbox Custom Output Raw Output Submit Code
79 def test_case_9(self):
80 features = [4.30936122, 4.28739283
81 k = 9
82 expected = ["pid_500", "pid_535"
83 actual = program.find_k_nearest_neighbors
84 self.assertEqual(sorted(actual),
85
86 def test_case_10(self):
87 features = [4.30936122, 4.28739283
88 k = 9
89 expected = 0
90 actual = program.predict_label
91 self.assertEqual(actual, expected)
92

Run or submit code when


you're ready.

2 de 3 21/04/2022 08:42 p. m.
K Nearest Neighbors | MLExpert https://www.algoexpert.io/machine-learning/coding-questions/K%20Nea...

3 de 3 21/04/2022 08:42 p. m.

You might also like