I'm working on a knn function. Within it, I already have a function that will return the indices of the k-nearest neighbors for a given data point, when that point is compared to the data set.
Now, I'm trying to get the actual classifier function to work but I'm having trouble with an if/then statement. There's no weighting to the nearest neighbors so it's a majority vote and as such, calculating the mode should do it.
However, since "k" can be an even number, there's a possibility of a tie, in which case, the nearest neighbor "wins" the vote.
So I'm basically trying to code the following:
if the mode exists/'statistics.mode()' does not produce an error, then the doc will be classified by the mode
else, the doc will be classified by the nearest neighbor.
TIA for any help! Here's the code I have
def getPrediction(indices_of_nearest_neighbors, training_labels):
# get vector of "votes"
nearest_Y = training_labels.iloc[indices_of_nearest_neighbors, 1].values
if statistics.mode(nearest_Y) ##does not throw an error:
doc_classification=statistics.mode(nearest_Y)
else doc_classification= training_labels.iloc[closest_neighbor,1]
return doc_classification
Aucun commentaire:
Enregistrer un commentaire