-1

Trying to implement K-NN in python on spyder IDE

for i in range(5):
    neighbors = get_neighbors(learnset_data, 
                              learnset_labels, 
                              testset_data[i], 
                              3, 
                              distance=distance)
    print(i,testset_data[i],testset_labels[i],neighbors)

from collections import Counter
def vote(neighbors):
    class_counter = Counter()
    for neighbor in neighbors:
        class_counter[neighbor[2]] += 1
    return class_counter.most_common(1)[0][0]
#We will test 'vote' on our training samples:
    for i in range(n_training_samples):
    neighbors = get_neighbors(learnset_data,learnset_labels,testset_data[i],3,distance=distance)
    print("index: ", i,", result of vote: ", vote(neighbors),", label: ", testset_labels[i],", data: ", testset_data[i])

File "C:\Users\TAGORE\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/TAGORE/Desktop/pyt/nn1.py", line 119 neighbors = get_neighbors(learnset_data,learnset_labels,testset_data[i],3,distance=distance) ^ IndentationError: expected an indented block

4
  • you forgot the identations on line get_neighbors(learnset_data,learnset_labels,testset_data[i],3,distance=distance)
    – Sociopath
    Oct 10, 2019 at 6:36
  • I think you added indentation on line for i in range(n_training_samples): when there shouldn't be any
    – kuro
    Oct 10, 2019 at 6:40
  • Your code is very hard to read, you need to put empty lines after the import section, and after every function definition.
    – smci
    Oct 10, 2019 at 7:04
  • @KostasCharitidis: don't edit out the OP's wrong indentation, that's the whole point of the question :) I reverted your edit #3
    – smci
    Oct 10, 2019 at 7:05

1 Answer 1

-1

You forgot to indent your line in the 'for' loop, and wrongly indented your 'for' loop after the 'return' statement.

for i in range(5):
    neighbors = get_neighbors(learnset_data, 
                              learnset_labels, 
                              testset_data[i], 
                              3, 
                              distance=distance)
    print(i,testset_data[i],testset_labels[i],neighbors)

from collections import Counter
def vote(neighbors):
    class_counter = Counter()
    for neighbor in neighbors:
        class_counter[neighbor[2]] += 1
    return class_counter.most_common(1)[0][0]
#We will test 'vote' on our training samples:
for i in range(n_training_samples):
    neighbors = get_neighbors(learnset_data,learnset_labels,testset_data[i],3,distance=distance)
    print("index: ", i,", result of vote: ", vote(neighbors),", label: ", testset_labels[i],", data: ", testset_data[i])
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.