Selected Reading

SciPy - maxdists() Method



The SciPy maxdists() method refer to the module "from scipy.spatial.distance import pdist" which allows the function pdist() to calculate the pairwise distances between the points from the given set.

This function doesn't have any direct method in scipy. If the user wants to find the maximum pairwise distance, they will likely prefer the method pdist() which returns all pairwise distances.

Syntax

Its syntax is as follows −

pdist(point)

Parameters

This method accept only a single parameter −

  • point: This is a simple variable which store the array of points using array().

Return value

This method return the nd array of values integer and float.

Example 1

Following is the SciPy maxdists() method that illustrate the pairwise and maximum distance using the function pdist() and max() respectively.

import numpy as np
from scipy.spatial.distance import pdist

# input data
point = np.array([
    [0, 0],
    [1, 1],
    [2, 2],
    [3, 3],
    [4, 4]
])

# pairwise distances
dist = pdist(point)

# maximum distance
max_dist = np.max(dist)

print("The result of pairwise distances:", dist)
print("The result of maximum distance:", max_dist)

Output

The above code produces the following output −

The result of pairwise distances: [1.41421356 2.82842712 4.24264069 5.65685425 1.41421356 2.82842712
 4.24264069 1.41421356 2.82842712 1.41421356]
The result of maximum distance: 5.656854249492381

Example 2

This program perform the same calculation in similar to example 1 but here you will get the result of different distance metrics.

import numpy as np
from scipy.spatial.distance import pdist

# input data in the form of array of points
point = np.array([
    [0, 0],
    [1, 1],
    [2, 2],
    [3, 3],
    [4, 4]
])

# calculate pairwise distances using Manhattan (Cityblock) distance
dist = pdist(point, metric='cityblock')

# find the maximum distance
max_dist = np.max(dist)

print("Pairwise distances (Manhattan):", dist)
print("Maximum distance (Manhattan):", max_dist)

Output

The above code produces the following output −

Pairwise distances (Manhattan): [2. 4. 6. 8. 2. 4. 6. 2. 4. 2.]
Maximum distance (Manhattan): 8.0
scipy_reference.htm
Advertisements