Is there any easy way to find out what's the trend of the list?
For example:
[5.0, 6.0, 9.0, 4.0, 10.0]
. On the whole, its elements are increasing over time.[6.0, 4.0, 5.0, 4.0, 3.0]
. Its elements are decreasing over time.Ideally, I want a Boolean result from these kinds of lists.
Actually, I'd like to know the trend of a collection of data. not linear increasing or exactly increasing one by one. In the real world, some data not always good, Perhaps there are one or two quarters of data not as good as it used to be(but not too bad, also not too much), but as long as Its trend is good, It is good.
On the whole, Its elements are increasing.
I take this to mean you want to consider the change in moving average. Half the job is defining what you really want, so I advise you think carefully about this before starting to write logic.
I've combined a moving average solution by @Jaime with np.diff
to suggest a possible way to infer what you want.
import numpy as np
def moving_average(a, n=3) :
ret = np.cumsum(a, dtype=float)
ret[n:] = ret[n:] - ret[:-n]
return ret[n - 1:] / n
lst1 = [5.0, 6.0, 9.0, 4.0, 10.0]
lst2 = [6.0, 4.0, 5.0, 4.0, 3.0]
res1 = np.all(np.diff(moving_average(np.array(lst1), n=4))>0)
# True; i.e. "generally increasing"
res2 = np.all(np.diff(moving_average(np.array(lst2), n=4))>0)
# False, i.e. "generally not increasing"
Explanation
moving_average
calculates the moving average across a window of 4 entries.np.diff
then calculates the pairwise changes between these numbers.np.all
with test >0
determines if the changes are all positive or not all positive. An oversimplification driven by no clear requirement.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With