Skip to content

Commit 06119cd

Browse files
authored
added cid_ce feature calculator (blue-yonder#332)
* added cid_ce feature calculator * polish cid_ce docstring
1 parent a73a166 commit 06119cd

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

tests/units/feature_extraction/test_feature_calculations.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,15 @@ def test_abs_energy(self):
274274
self.assertAlmostEqualOnAllArrayTypes(abs_energy, [-1, 1.3], 2.69)
275275
self.assertEqualOnAllArrayTypes(abs_energy, [1], 1)
276276

277+
def test_cid_ce(self):
278+
self.assertEqualOnAllArrayTypes(cid_ce, [1, 1, 1], 0, normalize=True)
279+
self.assertEqualOnAllArrayTypes(cid_ce, [0, 4], 2, normalize=True)
280+
self.assertEqualOnAllArrayTypes(cid_ce, [100, 104], 2, normalize=True)
281+
282+
self.assertEqualOnAllArrayTypes(cid_ce, [1, 1, 1], 0, normalize=False)
283+
self.assertEqualOnAllArrayTypes(cid_ce, [0.5, 3.5, 7.5], 5, normalize=False)
284+
self.assertEqualOnAllArrayTypes(cid_ce, [-4.33, -1.33, 2.67], 5, normalize=False)
285+
277286
def test_ratio_beyond_r_sigma(self):
278287

279288
x = [0, 1]*10 + [10, 20, -30] # std of x is 7.21, mean 3.04

tsfresh/feature_extraction/feature_calculators.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,44 @@ def abs_energy(x):
391391
return sum(x * x)
392392

393393

394+
@set_property("fctype", "simple")
395+
def cid_ce(x, normalize):
396+
"""
397+
This function calculator is an estimate for a time series complexity [1] (A more complex time series has more peaks,
398+
valleys etc.). It calculates the value of
399+
400+
.. math::
401+
402+
\\sqrt{ \\sum_{i=0}^{n-2lag} ( x_{i} - x_{i+1})^2 }
403+
404+
.. rubric:: References
405+
406+
| [1] Batista, Gustavo EAPA, et al (2014).
407+
| CID: an efficient complexity-invariant distance for time series.
408+
| Data Mining and Knowledge Difscovery 28.3 (2014): 634-669.
409+
410+
:param x: the time series to calculate the feature of
411+
:type x: pandas.Series
412+
:param normalize: should the time series be z-transformed?
413+
:type normalize: bool
414+
415+
:return: the value of this feature
416+
:return type: float
417+
"""
418+
419+
x = np.asarray(x)
420+
421+
if normalize:
422+
s = np.std(x)
423+
if s!=0:
424+
x = (x - np.mean(x))/s
425+
else:
426+
return 0.0
427+
428+
x = np.diff(x)
429+
return np.sqrt(np.sum((x * x)))
430+
431+
394432
@set_property("fctype", "simple")
395433
def mean_abs_change(x):
396434
"""

tsfresh/feature_extraction/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ def __init__(self):
105105
name_to_param.update({
106106
"time_reversal_asymmetry_statistic": [{"lag": lag} for lag in range(1, 4)],
107107
"c3": [{"lag": lag} for lag in range(1, 4)],
108+
"cid_ce": [{"normalize": True}, {"normalize": False}],
108109
"symmetry_looking": [{"r": r * 0.05} for r in range(20)],
109110
"large_standard_deviation": [{"r": r * 0.05} for r in range(1, 20)],
110111
"quantile": [{"q": q} for q in [.1, .2, .3, .4, .6, .7, .8, .9]],

0 commit comments

Comments
 (0)