ComenzarEmpieza gratis

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 ejercicio forma parte del curso

Anomaly Detection in Python

Ver curso

Instrucciones del ejercicio

  • Find the absolute differences between the elements of M and N.
  • Find the sum of differences to calculate the final manhattan distance.

Ejercicio interactivo práctico

Prueba este ejercicio completando el código de muestra.

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)
Editar y ejecutar código