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...
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)](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Ffe5af5d5-f811-48fd-8e6e-3830a7fa1b94%2Fc1fc1349-6549-44a7-9309-094063572575%2F8vh8uoc_processed.png&w=3840&q=75)
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.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Ffe5af5d5-f811-48fd-8e6e-3830a7fa1b94%2Fc1fc1349-6549-44a7-9309-094063572575%2Fpihpekf_processed.png&w=3840&q=75)
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
![](/static/compass_v2/shared-icons/check-mark.png)
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
Recommended textbooks for you
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
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…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
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)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
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…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
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)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
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](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY