Finding Bray-Curtis distance using SciPy

Bray-Curtis distance finds the dissimilarity between the species found at two regions and is often used in ecology studies. The Python example finds the dissimilarity using the scipy function braycurtis().

Distance Calculcations Updated 11 Jul 2025 2 min read

Overview

  • The Bray-Curtis distance is more of a measure of dissimilarity than a distance.
  • Given two samples, the Bray-Curtis distance indicates how dissimilar two samples are.
  • The Bray-Curtis distance was first introduced in ecological studies and is predominantly used in ecology to study about the population of species found in different locations.
  • The Bray-Curtis distance is a normalized value that varies between 0 and 1. A Bray-Curtis dissimilarity of 1 means the data compared are completely dissimilar. A value of 0 means both the data are exactly similar.
  • The formula for the Bray-Curtis dissimilarity matrix is given by

    Dissimilarity index = 1-(2w/a+b)

  • The species with the lowest count that are common to both the samples are added together and multiplied by two which is divided by the total number of species in both the samples.
  • The obtained value is inverted by subtracting from 1 which gives the Bray-Curtis dissimilarity mesure.
  • w – Total number of species which are the lowest in Sample 1 and Sample 2, for the ones that are commonly present in both the samples.

    a – Total number of species from Sample 1

    b – Total number of species from Sample 2

  • The SciPy function from braycurtis() the scipy.spatial.distance module computes and returns the Bray-Curtis distance for the data passed as two one-dimensional arrays.

Example

python
# Example Python program that finds the Bray-curtis distance

            # between the species data from two regions

            # i) Western ghats of India

            # ii) Eastern ghats of India

            #

            # Data courtesy:

            #https://www.researchgate.net/publication/

            #381245063_Faunal_Diversity_in_Western_Ghats_India_A_Review

            #https://kalpavriksh.org/wp-content/uploads/2018/12/

            #Eastern-Ghats-Final-July-2004.pdf

            #

            import scipy.spatial.distance as dist

# Number of species(species pluralis) in each group of

            # Mammals, birds, reptiles, amphibians

            westernGhats_data = [140, 458, 267,178]

            easternGhats_data = [81, 363, 59, 14]

# Find how dissimilar the two mountain ranges are in

            # terms of the species found there

            dissimilarity = dist.braycurtis(westernGhats_data,

                                            easternGhats_data)

            print("Bray-curtis distance:")

            print(dissimilarity)

Output

Output
Bray-curtis distance:

            0.3371794871794872