Mercurial Hosting > traffic-intelligence
comparison python/utils.py @ 199:ca9d9104afba
added utility to calibrate polynoms and plot
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Mon, 20 Feb 2012 19:32:27 -0500 |
parents | 2788b2827670 |
children | ab1a11176d7b |
comparison
equal
deleted
inserted
replaced
198:c91c8fd8bf1e | 199:ca9d9104afba |
---|---|
184 win = ones(width,'d') | 184 win = ones(width,'d') |
185 result = convolve(win/width,array(inputSignal),'same') | 185 result = convolve(win/width,array(inputSignal),'same') |
186 result[:halfWidth] = inputSignal[:halfWidth] | 186 result[:halfWidth] = inputSignal[:halfWidth] |
187 result[-halfWidth:] = inputSignal[-halfWidth:] | 187 result[-halfWidth:] = inputSignal[-halfWidth:] |
188 return result | 188 return result |
189 | |
190 def linearRegression(x, y, deg = 1, plotData = False): | |
191 '''returns the least square estimation of the linear regression of y = ax+b | |
192 as well as the plot''' | |
193 from numpy.lib.polynomial import polyfit | |
194 from matplotlib.pyplot import plot | |
195 from numpy.core.multiarray import arange | |
196 coef = polyfit(x, y, deg) | |
197 if plotData: | |
198 def poly(x): | |
199 result = 0 | |
200 for i in range(len(coef)): | |
201 result += coef[i]*x**(len(coef)-i-1) | |
202 return result | |
203 plot(x, y, 'x') | |
204 xx = arange(min(x), max(x),(max(x)-min(x))/1000) | |
205 plot(xx, [poly(z) for z in xx]) | |
206 return coef | |
189 | 207 |
190 ######################### | 208 ######################### |
191 # plotting section | 209 # plotting section |
192 ######################### | 210 ######################### |
193 | 211 |