Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize mean_change (fixes issue #542) and correct documentation #574

Merged
merged 1 commit into from
Nov 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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