def get_neighbors(self, training, test_instance, k): # Get the list of nearest neighbors to a test instance distances =[] for i in range(len(training)-1): dist = edit_dist(test_instance, training[i][1]) distances.append((training[i], dist)) distances.sort(key=operator.itemgetter(1)) neighbors = [] for x in range(0, k): neighbors.append(distances[x][0]) return neighbors
def get_neighbors(self, data, data_label, test_instance, k): # Get the list of nearest neighbors to a test instance distances = [] for i in range(len(data)): dist = edit_dist(test_instance, data[i]) distances.append((data[i], data_label[i], dist)) distances.sort(key=operator.itemgetter(2)) neighbors = [] for x in range(0, k): neighbors.append([distances[x][0], distances[x][1]]) return neighbors
def get_neighbors(self, training, test_instance, k): # Get the list of nearest neighbors to a test instance distances = [] for i in range(len(training) - 1): dist = edit_dist(test_instance, training[i][1]) distances.append((training[i], dist)) distances.sort(key=operator.itemgetter(1)) neighbors = [] for x in range(0, k): neighbors.append(distances[x][0]) return neighbors
def lev_metric(self, x, y): i, j = int(x[0]), int(y[0]) # extract indices # if self.data[i] == self.data[j]: # print self.data[i], self.data[j], edit_dist(self.data[i], self.data[j]) return edit_dist(self.data[i], self.data[j])