Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# 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