# This program computes the function f -- the similarity of one data item # with a set of others. The data items defined here are example 2D vectors. # That is, each 'cell' is a 2-item tuple or list: cell = [_, _]. # See lab slides for an illustration. import numpy as np # Compute the similarity of 'center_cell' with 'neighbors' center_cell = (4,1) neighbors = [(2,1), (3,6), (5,4)] alpha = 4 #Scale of dissimilarity or discrimination factor neighborhood_size = 9 xi1 = center_cell[0] xi2 = center_cell[1] total_similarity = 0 print('Distance values ---') # Compute similarity of [xi1, xi2] cell with neighboring [xj1, xj2] cells. for cell in neighbors: xj1 = cell[0] xj2 = cell[1] distance = np.sqrt(pow(xi1-xj1,2) + pow(xi2-xj2,2)) print 'Distance between', center_cell, 'and', cell, ' = ', distance current_similarity = 1 - (distance / alpha) total_similarity = total_similarity + current_similarity total_similarity = total_similarity / neighborhood_size print 'Similarity between', center_cell, 'and its neighbors', neighbors, ' = ', total_similarity # NOTE that for the same set of data, changing 'alpha' will yield different similarity values