Create a gist now

Instantly share code, notes, and snippets.

online_regr.py
from matplotlib.pylab import figure, plot, title, xlabel, ylabel, xlim,show
import numpy as np
from pprint import pprint
import math
from onlineregression import OnlineRegression
def draw_plot(arr_x,arr_y,plot_title):
plot(arr_x,arr_y,'bo-')
title(plot_title)
xlabel("X")
ylabel("Y")
xlim((0,len(arr_x)))
if __name__ == "__main__":
arr_x=np.asarray([0, 1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10,11,12])
arr_y=np.asarray([20,25,40,35,50,40,35,30,25,30,45,40,50])
draw_plot(arr_x,arr_y,"Online Regression")
sz=len(arr_x)
data=dict()
temp=OnlineRegression()
idx=[]
for i in range(sz):
temp.push(arr_x[i],arr_y[i])
if(i%3==0):
idx.append(i)
data[i]=dict()
data[i][i] = OnlineRegression()
data[i][i].push(arr_x[i],arr_y[i])
idx_sz=len(idx)
if idx_sz>1:
prev = idx[-2]
for j in reversed(idx[:-1]):
data[i][j] = data[prev][j]+temp
a=data[i][j]
print("------- %d <--> %d -------" %(j,i))
print("x :",arr_x[j:i+1])
print("y :",arr_y[j:i+1])
print("sum:",a.y)
print("count:",a.n)
print("mean:",a.mean_y())
print("slope:",a.slope())
print("intercept:",a.intercept())
print("residuals:",a.residuals())
print("std-error:",a.stderr())
temp = OnlineRegression()
from itertools import combinations
segments = []
for ifrom,ito in combinations(idx,2):
if ito-ifrom>1:
r = data[ito][ifrom]
a= r.slope()
b= r.intercept()
line_x=[arr_x[ifrom],arr_x[ito]]
line_y=[a*arr_x[ifrom]+b,a*arr_x[ito]+b]
plot(line_x,line_y,'-')
show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment