IniziaInizia 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.

Questo esercizio fa parte del corso

Anomaly Detection in Python

Visualizza il corso

Istruzioni dell'esercizio

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

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

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)
Modifica ed esegui il codice