INT 21h

Hi, I am Vladimir Smagin, SysAdmin and Kaptain. Telegram Email / GIT / RSS / GPG

Python: Нахождение тренда

№ 9343 В разделе Programming от September 3rd, 2018,
В подшивках:

Код создаст CSV файл, который можно открыть в табличном редакторе. Код взят отсюда и чуток дополнен. Еще о трендах можно прочитать здесь.

import csv

def linreg(X, Y):
    """
    return a,b in solution to y = ax + b such that root mean square 
    distance between trend line and original points is minimized
    """
    N = len(X)
    Sx = Sy = Sxx = Syy = Sxy = 0.0
    for x, y in zip(X, Y):
        Sx = Sx + x
        Sy = Sy + y
        Sxx = Sxx + x*x
        Syy = Syy + y*y
        Sxy = Sxy + x*y
    det = Sxx * N - Sx * Sx
    return (Sxy * N - Sy * Sx)/det, (Sxx * Sy - Sx * Sxy)/det


x = [12, 34, 29, 38, 34, 51, 29, 34, 47, 34, 55, 94, 68, 81]
a,b = linreg(range(len(x)),x)  #your x,y are switched from standard notation

extrapolatedtrendline=[a*index + b for index in range(len(x))]

with open('open_in_calc.csv','w') as csvfile:
    spamwriter = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
    spamwriter.writerow(x)
    spamwriter.writerow(extrapolatedtrendline)

Нет комментариев »

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Облачная платформа
Яндекс.Метрика

Fortune cookie: One night a girl had an affair With a fellow all covered with hair. Then she picked up his hat And realized that She'd been had by Smokey the Bear.