Calculating manhattan distance manually
While euclidean distance is very popular, it only scales well beyond two or three-dimensional data. In these cases, you can use manhattan distance as an alternative. It has the advantage of working exceptionally well with datasets with many categorical features.
Practice calculating it manually with NumPy, which has been loaded under its standard alias np.
Cet exercice fait partie du cours
Anomaly Detection in Python
Instructions
- Find the absolute differences between the elements of
MandN. - Find the sum of differences to calculate the final manhattan distance.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
M = np.array([14, 17, 18, 20, 14, 12, 19, 13, 17, 20])
N = np.array([63, 74, 76, 72, 64, 75, 75, 61, 50, 53])
# Subtract M from N and find the absolute value
abs_diffs = ____
# Calculate the final manhattan distance
manhattan_dist_MN = ____
print(manhattan_dist_MN)