neighbours()

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

only needed to complete the get_neighbours() function

1 from mat+h import sgrt
View site information
3 # Function: calculates the distance between two input vectors
4 def vec_distance(vector1, vector2):
distance = 0.0
for cntr in range(len(vector1)-1):
distance += (vector1[cntr] - vector2[cntr])**2
return sqrt(distance)
7
8
9.
10 # Helper function: returns distance, from position 1 of distance_tuple
11 def get_distance( distance_tuple ):
12
return distance_tuple[1]
13
14 # Function: sorts list of distances
15 def sort_(distances):
16
distances.sort(key=get_distance)
17
return distances
18
19
20 # Return the required number of nearest neighbours
21 def get_neighbours(data, test_vector, num_neighbours):
22
23
#** Your code goes here
24
25
pass # remove this
26
27
28
29 if -_name_
' -_main__':
30
dataset
[2.7810836,2.550537003,0.5],
[1.465489372,2.362125076,1.54],
[3.396561688,4.400293529,0.72],
[1.38807019,1.850220317,1.65],
[3.06407232,3.005305973,0.98],
[7.627531214,2.759262235,1.2],
[5.332441248,2.088626775,1.1],
[6.922596716,1.77106367,0.8],
[8.675418651,-0.242068655,1.7],
[7.673756466,3.508563011,0.76]]
31
32
33
34
35
36
37
38
39
40
41
42
43
44
number_of_neighbours
index_of_vector
neighbours
if neighbours:
for neighbour in neighbours:
int(input())+1
int(input())
get_neighbours(dataset, dataset[index_of_vector], number_of_neighbours)
45
46
47
print(neighbour)
Transcribed Image Text:1 from mat+h import sgrt View site information 3 # Function: calculates the distance between two input vectors 4 def vec_distance(vector1, vector2): distance = 0.0 for cntr in range(len(vector1)-1): distance += (vector1[cntr] - vector2[cntr])**2 return sqrt(distance) 7 8 9. 10 # Helper function: returns distance, from position 1 of distance_tuple 11 def get_distance( distance_tuple ): 12 return distance_tuple[1] 13 14 # Function: sorts list of distances 15 def sort_(distances): 16 distances.sort(key=get_distance) 17 return distances 18 19 20 # Return the required number of nearest neighbours 21 def get_neighbours(data, test_vector, num_neighbours): 22 23 #** Your code goes here 24 25 pass # remove this 26 27 28 29 if -_name_ ' -_main__': 30 dataset [2.7810836,2.550537003,0.5], [1.465489372,2.362125076,1.54], [3.396561688,4.400293529,0.72], [1.38807019,1.850220317,1.65], [3.06407232,3.005305973,0.98], [7.627531214,2.759262235,1.2], [5.332441248,2.088626775,1.1], [6.922596716,1.77106367,0.8], [8.675418651,-0.242068655,1.7], [7.673756466,3.508563011,0.76]] 31 32 33 34 35 36 37 38 39 40 41 42 43 44 number_of_neighbours index_of_vector neighbours if neighbours: for neighbour in neighbours: int(input())+1 int(input()) get_neighbours(dataset, dataset[index_of_vector], number_of_neighbours) 45 46 47 print(neighbour)
18.7 LAB: Algorithm Example
In this lab you are asked to complete the provided code so that the algorithm calculates "n" nearest neighbours of a given vector. The point
of this exercise it to provide you with an example of an algorithm, get you to think about how to divide a complex task into a number of
smaller, more manageable steps and to show you an example of how different data types might be used for different purposes.
A vector in this context is a point in 3 dimensional space (x,y,z). It could be used to represent a location coordinate e.g. on a google map
with altitude. You can imagine each vector representing the location of an object and the algorithm is trying to find a number, "n", nearest
objects to the object of interest.
Some definitions:
• The "n-Nearest Neighbors" of a given vector 'VO' are defined as its 'n' closest vectors ie those with the shortest distance to 'V0'.
• The distance between vectors can be calculated using the vec_distance function
• Note: the functions vec_distance, and sort_function are provided. You don't need to understand how they work, but you need
to understand what their input and output is. To understand that you can print the input and output of them - this can help you figure
out what the rest of the code should look like.
Transcribed Image Text:18.7 LAB: Algorithm Example In this lab you are asked to complete the provided code so that the algorithm calculates "n" nearest neighbours of a given vector. The point of this exercise it to provide you with an example of an algorithm, get you to think about how to divide a complex task into a number of smaller, more manageable steps and to show you an example of how different data types might be used for different purposes. A vector in this context is a point in 3 dimensional space (x,y,z). It could be used to represent a location coordinate e.g. on a google map with altitude. You can imagine each vector representing the location of an object and the algorithm is trying to find a number, "n", nearest objects to the object of interest. Some definitions: • The "n-Nearest Neighbors" of a given vector 'VO' are defined as its 'n' closest vectors ie those with the shortest distance to 'V0'. • The distance between vectors can be calculated using the vec_distance function • Note: the functions vec_distance, and sort_function are provided. You don't need to understand how they work, but you need to understand what their input and output is. To understand that you can print the input and output of them - this can help you figure out what the rest of the code should look like.
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY