K Nearest Neighbors MLExpert
K Nearest Neighbors MLExpert
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.
{
"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:
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
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.