L
L
ledman52014-03-06 18:12:45
Python
ledman5, 2014-03-06 18:12:45

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

2 answer(s)
X
xandox, 2014-03-07
@ledman5

Look here
stackoverflow.com/questions/7171356/python-calcula...

I
iroln, 2014-03-30
@iroln

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

The data vectors x and y are input (you can also set the weight vector), the output is the coefficients k, b for the model y = kx + b.
"Magic" functions in numpy: polyfit/polyval.
Error calculation functions, such as sse/mse/rmse/rsquare, are easy to write using formulas that can be found on the Internet or in books on statistical and regression analysis.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question