I'm in the process of implementing a K-nearest neighbour algorithm in Python (for those of you that don't know about learning, it's an algorithm used to classify objects based on data that is already classified, using Euclidean distance).
I've got my distances computed, and I can take the k nearest distances, and find the classes of those objects. My problem is, if K is greater than 1, say 3 or 5, I'm not sure how I can get the most frequent element in the list.
For example, my output is:
[10, 9, 7, 10]
10 occurs the most, so I'd like to return this number. In case of a tie (2 or more elements occuring the same frequency), it returns an error (I can deal with this myself). I'd just like some opinion on how to return the maximum of the above list. (Using python 2.6.6 so I can't use the collections imports).
Second question:
I'm attempting to convert a numpy array to a normal array. My code looks like this:
def getClassesOfIndexes(l):
tmp1 = []
for i in l:
tmp1.append(classes[i])
return tmp1
print(getClassesOfIndexes([1024, 9128, 394, 39]))
This prints something like: [array([10], dtype=uint8), array([7], dtype=uint8), array([10], dtype=uint8), array([9], dtype=uint8)]
What could I do for it to simply return [10, 7, 10, 9]
?
Thanks for any help.