Skip to content

Commit

Permalink
Optimize mean_change (fixes issue #542) and correct documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
blu3r4y committed Oct 11, 2019
1 parent c3d35c4 commit 955479f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
3 changes: 3 additions & 0 deletions tests/units/feature_extraction/test_feature_calculations.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,9 @@ def test_mean_abs_change(self):
def test_mean_change(self):
self.assertEqualOnAllArrayTypes(mean_change, [-2, 2, 5], 3.5)
self.assertEqualOnAllArrayTypes(mean_change, [1, 2, -1], -1)
self.assertEqualOnAllArrayTypes(mean_change, [10, 20], 10)
self.assertIsNanOnAllArrayTypes(mean_change, [1])
self.assertIsNanOnAllArrayTypes(mean_change, [])

def test_mean_second_derivate_central(self):
self.assertEqualOnAllArrayTypes(mean_second_derivative_central, list(range(10)), 0)
Expand Down
5 changes: 3 additions & 2 deletions tsfresh/feature_extraction/feature_calculators.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,14 +531,15 @@ def mean_change(x):
.. math::
\\frac{1}{n} \\sum_{i=1,\ldots, n-1} x_{i+1} - x_{i}
\\frac{1}{n-1} \\sum_{i=1,\ldots, n-1} x_{i+1} - x_{i} = \\frac{1}{n-1} (x_{n} - x_{1})
:param x: the time series to calculate the feature of
:type x: numpy.ndarray
:return: the value of this feature
:return type: float
"""
return np.mean(np.diff(x))
x = np.asarray(x)
return (x[-1] - x[0]) / (len(x) - 1) if len(x) > 1 else np.NaN


@set_property("fctype", "simple")
Expand Down

0 comments on commit 955479f

Please sign in to comment.