Skip to content

Instantly share code, notes, and snippets.

@LauraLangdon
Created August 10, 2021 05:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LauraLangdon/e06d551e715ecd3774b9d8b63ae4265c to your computer and use it in GitHub Desktop.
Save LauraLangdon/e06d551e715ecd3774b9d8b63ae4265c to your computer and use it in GitHub Desktop.
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