w3resource

Compute various distance metrics using NumPy and SciPy

NumPy: Integration with SciPy Exercise-12 with Solution

Write a NumPy program to create a dataset and compute various distance metrics (Euclidean, Manhattan, etc.) using SciPy.

Sample Solution:

Python Code:

import numpy as np  # Import NumPy library
from scipy.spatial.distance import euclidean, cityblock, cosine, hamming  # Import distance functions from SciPy

# Create a dataset using NumPy
data = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
    [2, 2, 2]
])

# Select two points from the dataset to compute distances
point1 = data[0]
point2 = data[1]

# Compute Euclidean distance
euclidean_distance = euclidean(point1, point2)

# Compute Manhattan (Cityblock) distance
manhattan_distance = cityblock(point1, point2)

# Compute Cosine distance
cosine_distance = cosine(point1, point2)

# Compute Hamming distance
hamming_distance = hamming(point1, point2)

# Print the distances
print("Euclidean Distance between point1 and point2:", euclidean_distance)
print("Manhattan Distance between point1 and point2:", manhattan_distance)
print("Cosine Distance between point1 and point2:", cosine_distance)
print("Hamming Distance between point1 and point2:", hamming_distance)

Output:

Euclidean Distance between point1 and point2: 5.196152422706632
Manhattan Distance between point1 and point2: 9
Cosine Distance between point1 and point2: 0.0253681538029239
Hamming Distance between point1 and point2: 1.0

Explanation:

  • Import libraries:
    • Import the NumPy library for creating and manipulating arrays.
    • Import distance functions from SciPy's spatial module to compute various distance metrics.
  • Create a dataset:
    • Define a dataset as a NumPy array with multiple data points.
  • Select points:
    • Select two points from the dataset to compute the distance between them.
  • Compute Euclidean Distance:
    • Use the euclidean function from SciPy to compute the Euclidean distance between the selected points.
  • Compute Manhattan (Cityblock) Distance:
    • Use the cityblock function from SciPy to compute the Manhattan distance between the selected points.
  • Compute cosine distance:
    • Use the cosine function from SciPy to compute the Cosine distance between the selected points.
  • Compute Hamming Distance:
    • Use the hamming function from SciPy to compute the Hamming distance between the selected points.
  • Finally print the computed distances to verify the results.

Python-Numpy Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Generate random data and perform clustering using SciPy's Hierarchical clustering.
Next: Apply QR, LU, and Cholesky decompositions using SciPy on a matrix.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.