similaritymeasures
raw JSON → 1.4.0 verified Fri May 01 auth: no python
Python library for quantifying the difference between two arbitrary curves in space. Provides implementations of common similarity measures including discrete Fréchet distance, dynamic time warping, curve length measure, area between two curves, and more. Current version 1.4.0, supports Python >=3.8, released under MIT license.
pip install similaritymeasures Common errors
error ImportError: cannot import name 'frechet_dist' from 'similaritymeasures' ↓
cause Attempting to import the function directly instead of the module.
fix
Use
import similaritymeasures then similaritymeasures.frechet_dist(...). error ValueError: operands could not be broadcast together with shapes ↓
cause Input curves have different number of points and not compatible for element-wise operations (e.g., `area_between_two_curves` requires equal lengths).
fix
Ensure both curves have the same number of points, or use DTW which can handle different lengths.
error TypeError: only size-1 arrays can be converted to Python scalars ↓
cause Passing a list or tuple that isn't converted to numpy array, or ragged arrays.
fix
Convert your curves to numpy arrays:
p = np.array(p) and q = np.array(q). Warnings
breaking In version 1.0.0, `area_between_two_curves` changed behavior for degenerate cases (e.g., repeating points or collinear points). The area may now differ from previous versions. ↓
fix Review area values if upgrading from <1.0.0; the new behavior is correct per the mathematical definition.
breaking In version 0.6.0, `similaritymeasures.pcm` changed outputs. To get previous behavior, set `norm_seg_length=True`. ↓
fix If you need the old PCM results, call `similaritymeasures.pcm(p, q, norm_seg_length=True)`.
gotcha The functions expect inputs as 2D numpy arrays of shape (n, 2) for 2D curves. Passing lists or arrays of different shapes may cause errors or incorrect results. ↓
fix Ensure both curves are numpy arrays with shape (n, 2) or (n, 3) for 3D.
Imports
- similaritymeasures
import similaritymeasures - Frechet distance function wrong
frechet_distcorrectsimilaritymeasures.frechet_dist
Quickstart
import numpy as np
import similaritymeasures
# Define two curves as arrays of (x, y) points
p = np.array([[1, 1], [2, 1], [3, 2]])
q = np.array([[1, 1], [2, 3], [3, 2]])
# Discrete Frechet distance
frechet = similaritymeasures.frechet_dist(p, q)
print(f"Frechet distance: {frechet}")
# Dynamic time warping (DTW)
dtw, d = similaritymeasures.dtw(p, q)
print(f"DTW distance: {dtw}")
# Area between two curves
area = similaritymeasures.area_between_two_curves(p, q)
print(f"Area between curves: {area}")
# Curve length measure
clm = similaritymeasures.curve_length_measure(p, q)
print(f"Curve length measure: {clm}")