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
.
Este exercício faz parte do curso
Anomaly Detection in Python
Instruções do exercício
- Find the absolute differences between the elements of
M
andN
. - Find the sum of differences to calculate the final manhattan distance.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
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)