15.1 C
London
Friday, July 5, 2024
HomePythonDescriptive Statistics in PythonHow to Calculate Canberra Distance in Python (With Example)

How to Calculate Canberra Distance in Python (With Example)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

The Canberra distance between two vectors, A and B, is calculated as:

Canberra distance = Σ |Ai-Bi| / (|Ai| + |Bi|)

where:

  • Ai: The ith value in vector A
  • Bi: The ith value in vector B

For example, suppose we have the following two vectors:

  • A = [2, 4, 4, 6]
  • B = [5, 5, 7, 8]

We would calculate the Canberra distance between A and B as:

  • Canberra Distance = |2-5|/(2+5) + |4-5|/(4+5) + |4-7|/(4+7) + |6-8|/(6+8)
  • Canberra Distance = 3/7 + 1/9 + 3/11 + 2/14
  • Canberra Distance = 0.95527

The Canberra distance between these two vectors is 0.95527.

The following example shows how to calculate the Canberra distance between these exact two vectors in Python.

Example: Calculating Canberra Distance in Python

First, let’s create a NumPy array to hold each of our vectors:

import numpy as np

#define two arrays
array1 = np.array([2, 4, 4, 6])
array2 = np.array([5, 5, 7, 8])

Next, we can use the canberra() function from the SciPy package in Python to calculate the Canberra distance between the two vectors:

from scipy.spatial import distance

#calculate Canberra distance between the arrays
distance.canberra(array1, array2)

0.9552669552

The Canberra distance between the two vectors is 0.95527.

Notice that this value matches the one we calculated earlier by hand.

Note: You can find the complete documentation for the canberra() function from the SciPy package here.

Additional Resources

The following tutorials explain how to calculate other common distance metrics in Python:

How to Calculate Euclidean Distance in Python
How to Calculate Manhattan Distance in Python
How to Calculate Hamming Distance in Python
How to Calculate Mahalanobis Distance in Python
How to Calculate Levenshtein Distance in Python

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories