bundles / scipy latest / scipy / spatial / distance / is_valid_dm
function
scipy.spatial.distance:is_valid_dm
source: /scipy/spatial/distance.py :2307
Signature
def is_valid_dm ( D , tol = 0.0 , throw = False , name = D , warning = False ) Summary
Return True if input array satisfies basic distance matrix properties (symmetry and zero diagonal).
Extended Summary
This function checks whether the input is a 2-dimensional square NumPy array with a zero diagonal and symmetry within a specified tolerance. These are necessary properties for a distance matrix but not sufficient -- in particular, this function does not check the triangle inequality, which is required for a true metric distance matrix.
The triangle inequality states that for any three points i, j, and k: D[i,k] <= D[i,j] + D[j,k]
Parameters
D: array_likeThe candidate object to test for basic distance matrix properties.
tol: float, optionalThe distance matrix is considered symmetric if the absolute difference between entries
ijandjiis less than or equal totol. The same tolerance is used to determine whether diagonal entries are effectively zero.throw: bool, optionalIf True, raises an exception when the input is invalid.
name: str, optionalThe name of the variable to check. This is used in exception messages when
throwis True to identify the offending variable.warning: bool, optionalIf True, a warning message is raised instead of throwing an exception.
Returns
valid: boolTrue if the input satisfies the symmetry and zero-diagonal conditions.
Raises
: ValueErrorIf
throwis True andDis not a valid distance matrix.: UserWarningIf
warningis True andDis not a valid distance matrix.
Notes
This function does not check the triangle inequality, which is required for a complete validation of a metric distance matrix. Only structural properties (symmetry and zero diagonal) are verified. Small numerical deviations from symmetry or exact zero diagonal are tolerated within the tol parameter.
Examples
import numpy as np from scipy.spatial.distance import is_valid_dm✓
d = np.array([[0.0, 1.1, 1.2, 1.3], [1.1, 0.0, 1.0, 1.4], [1.2, 1.0, 0.0, 1.5], [1.3, 1.4, 1.5, 0.0]]) is_valid_dm(d)✓
is_valid_dm([[0, 2, 2], [2, 0, 2]])
✓is_valid_dm([[0, 1, 1], [1, 2, 3], [1, 3, 0]])
✓is_valid_dm([[0, 1, 3], [2, 0, 1], [3, 1, 0]])
✓Aliases
-
scipy.cluster._optimal_leaf_ordering.is_valid_dm