Skip to content

Commit

Permalink
Use chebyshev distance
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
Scott-Simmons committed Jan 15, 2025
1 parent 917f4ba commit e8e890e
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion tsfresh/feature_extraction/feature_calculators.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from numpy.linalg import LinAlgError
from scipy.signal import cwt, find_peaks_cwt, ricker, welch
from scipy.stats import linregress
from scipy.spatial.distance import cdist
from statsmodels.tools.sm_exceptions import MissingDataError
from statsmodels.tsa.ar_model import AutoReg

Expand Down Expand Up @@ -1782,7 +1783,7 @@ def approximate_entropy(x, m, r):
def _phi(m):
x_re = np.lib.stride_tricks.sliding_window_view(x, window_shape=m)
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)
Expand Down

0 comments on commit e8e890e

Please sign in to comment.