From f4bf1571b9f6986d83a9ce5cffdcfa94d847ae7c Mon Sep 17 00:00:00 2001 From: Scott-Simmons <52365471+Scott-Simmons@users.noreply.github.com> Date: Wed, 15 Jan 2025 19:57:41 +1100 Subject: [PATCH] Use chebyshev distance MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Functionally equivalent output, but much faster, see example below: In [16]: from scipy.spatial.distance import cdist In [17]: import numpy as np In [18]: x = [12,13,15,16,17]*10 In [19]: x_re = np.lib.stride_tricks.sliding_window_view(x, window_shape=2) In [20]: %timeit np.max(np.abs(x_re[:, np.newaxis] - x_re[np.newaxis, :]), axis=2) ...: 62.9 μs ± 660 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each) In [21]: %timeit cdist(x_re, x_re, metric="chebyshev") 5.58 μs ± 19.8 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each) --- tsfresh/feature_extraction/feature_calculators.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tsfresh/feature_extraction/feature_calculators.py b/tsfresh/feature_extraction/feature_calculators.py index 38f2bbf9..212074e2 100644 --- a/tsfresh/feature_extraction/feature_calculators.py +++ b/tsfresh/feature_extraction/feature_calculators.py @@ -28,6 +28,7 @@ import stumpy from numpy.linalg import LinAlgError from scipy.signal import cwt, find_peaks_cwt, ricker, welch +from scipy.spatial.distance import cdist from scipy.stats import linregress from statsmodels.tools.sm_exceptions import MissingDataError from statsmodels.tsa.ar_model import AutoReg @@ -1782,7 +1783,7 @@ def approximate_entropy(x, m, r): def _phi(m): x_re = np.array([x[i : i + m] for i in range(N - m + 1)]) C = np.sum( - np.max(np.abs(x_re[:, np.newaxis] - x_re[np.newaxis, :]), axis=2) <= r, + cdist(x_re, x_re, metric="chebyshev") <= r, axis=0, ) / (N - m + 1) return np.sum(np.log(C)) / (N - m + 1.0)