Question: Implement in Python a function snn_diss(data, x, y, k) which outputs the k-shared near neighbour dissimilarity of objects x and y with respect to dataset

Implement in Python a function snn_diss(data, x, y, k) which outputs the k-shared near neighbour dissimilarity of objects x and y with respect to dataset data, which is defined as the number of objects which are k-nearest neighbours of both x and y in data. Assume data is a list of tuples or lists of fixed length. The k-shared near neighbour dissimilarity can be computed by the following algorithm compute the list knni containing the pairs (p, dist(x, p)) for all objects p in data, where dist(x, p) is the euclidean squared distance between x and p - sort the list knni by the second elements of the pairs - repeat the steps above for object y, creating a sorted list knn2 - remove from knn1 and knn2 all elements of index greater than, or equal to, k - snn_list = empty list - for all el in knn1 - for all e2 in knn2 - if the first element of el equals the first element of e2 - append the first element of el to snn_list - return the number of elements in snn_list The function sorted(mylist, key = lambda x: x[1]) returns a copy of list mylist sorted by second element. To compute the euclidean squared distance of two objects use the following Python functions def sq_sum(x): return sum((y ** 2 for y in x)) def sq_norm_diff(x, y): return sq_sum((z[0] - z[1] for z in zip(x, y)))
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
