First page Back Continue Last page Overview Graphics

Solution

def vote_prob(neighbors):

class_counter = Counter()

for neighbor in neighbors:

class_counter[neighbor[2]] += 1

labels, votes = zip(*class_counter.most_common())

winner = class_counter.most_common(1)[0][0]

votes4winner = class_counter.most_common(1)[0][1]

return winner, votes4winner/sum(votes)

for i in range(n_training_samples):

neighbors = get_neighbors(learnset_data,

learnset_labels,

testset_data[i],

5,

distance=distance)

print("index: ", i,

", vote_prob: ", vote_prob(neighbors),

", label: ", testset_labels[i],

", data: ", testset_data[i])