Created
August 10, 2021 05:18
-
-
Save LauraLangdon/e06d551e715ecd3774b9d8b63ae4265c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def knn(tweet_vector, train_set, k) -> list: | |
""" | |
Find k nearest neighbors of a given tweet | |
:param tweet_vector: vector of tweet whose neighbors we seek | |
:param train_set: training set | |
:param k: desired number of nearest neighbors | |
:return: list of indices in main tweet list of k nearest neighbors, and distances of those | |
neighbors to given tweet | |
""" | |
knn_indices_and_distances = [] | |
distance = 0 | |
for x in range(len(train_set)): | |
distance = get_distance(tweet_vector[0], train_set[x]) | |
knn_indices_and_distances.append([train_set[x][-2], distance]) | |
# Sort by distance (smallest to largest) | |
knn_indices_and_distances.sort(key=lambda x: x[1]) | |
return knn_indices_and_distances[:k] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment