Answer the question
In order to leave comments, you need to log in
Python - how to determine the "trend" of data?
Good evening . There was a need to find a trend (growth / decline) of data in Python. The trend must be found almost in real-time (maximum delay of 1 s).
The variable float is given at the input every 0.05 s, at the output I want to get the directions of the graph (trend) - growing (a noticeable clear increase in peaks), decreasing (a clear decline in peaks) or without noticeable fluctuations (the peaks differ very little from each other (2 - 5%) , "stable") . I tried to apply the method of least squares () and other variants of regression analysis, but due to poor skills in Python, I could not implement it.
I would be grateful for any help on this subject.
Answer the question
In order to leave comments, you need to log in
What went wrong with the least squares method?
Here is the simplest linear regression solution:
def linregress(x, y, w=None, b=None):
x = np.array(x, dtype=np.float64)
y = np.array(y, dtype=np.float64)
if w is None:
w = np.ones(x.size, dtype=np.float64)
wxy = np.sum(w*y*x)
wx = np.sum(w*x)
wy = np.sum(w*y)
wx2 = np.sum(w*x*x)
sw = np.sum(w)
den = wx2*sw - wx*wx
if den == 0:
den = np.finfo(np.float64).eps
if b is None:
k = (sw*wxy - wx*wy) / den
b = (wy - k*wx) / sw
else:
k = (wxy - wx*b) / wx2
return k, b
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question