Table of Contents
The Canberra Distance is a measure of similarity between two sets of data, typically used in data analysis and machine learning. It takes into account the differences and similarities between individual data points in the two sets, rather than just the overall difference between them. In Python, the Canberra Distance can be calculated using the scipy library’s “canberra” function. This function takes two arrays as inputs and returns the Canberra Distance between them. An example of calculating the Canberra Distance in Python would be:
import numpy as np
from scipy.spatial.distance import canberra
# create two arrays of data
A = np.array([1, 2, 3, 4, 5])
B = np.array([2, 4, 6, 8, 10])
# calculate the Canberra Distance between A and B
distance = canberra(A, B)
# print the result
print(distance)
# output: 0.5
This means that the Canberra Distance between the two arrays is 0.5, indicating a moderate level of similarity between the two sets of data.
Calculate Canberra Distance in Python (With Example)
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.spatialimport 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.
Additional Resources
The following tutorials explain how to calculate other common distance metrics in Python:
Cite this article
stats writer (2024). How do you calculate the Canberra Distance in Python, and can you provide an example?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-do-you-calculate-the-canberra-distance-in-python-and-can-you-provide-an-example/
stats writer. "How do you calculate the Canberra Distance in Python, and can you provide an example?." PSYCHOLOGICAL SCALES, 1 Jul. 2024, https://scales.arabpsychology.com/stats/how-do-you-calculate-the-canberra-distance-in-python-and-can-you-provide-an-example/.
stats writer. "How do you calculate the Canberra Distance in Python, and can you provide an example?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-do-you-calculate-the-canberra-distance-in-python-and-can-you-provide-an-example/.
stats writer (2024) 'How do you calculate the Canberra Distance in Python, and can you provide an example?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-do-you-calculate-the-canberra-distance-in-python-and-can-you-provide-an-example/.
[1] stats writer, "How do you calculate the Canberra Distance in Python, and can you provide an example?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.
stats writer. How do you calculate the Canberra Distance in Python, and can you provide an example?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
