Skip to content

Instantly share code, notes, and snippets.

Created September 5, 2020 18:21
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save douglaspsteen/2f71a3abed860e07b5970b960406a981 to your computer and use it in GitHub Desktop.
# Calculate distance between two points
def minkowski_distance(a, b, p=1):
# Store the number of dimensions
dim = len(a)
# Set initial distance to 0
distance = 0
# Calculate minkowski distance using parameter p
for d in range(dim):
distance += abs(a[d] - b[d])**p
distance = distance**(1/p)
return distance
# Test the function
minkowski_distance(a=X.iloc[0], b=X.iloc[1], p=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment