Mercurial Hosting > traffic-intelligence
comparison python/utils.py @ 433:d40ad901b272
added kernel smoothing
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Fri, 29 Nov 2013 02:38:41 -0500 |
parents | f738fa1b69f0 |
children | 9a714f32fc9f |
comparison
equal
deleted
inserted
replaced
432:4970fa64f636 | 433:d40ad901b272 |
---|---|
152 | 152 |
153 | 153 |
154 ######################### | 154 ######################### |
155 # maths section | 155 # maths section |
156 ######################### | 156 ######################### |
157 | |
158 # def kernelSmoothing(sampleX, X, Y, weightFunc, halfwidth): | |
159 # '''Returns a smoothed weighted version of Y at the predefined values of sampleX | |
160 # Sum_x weight(sample_x,x) * y(x)''' | |
161 # from numpy import zeros, array | |
162 # smoothed = zeros(len(sampleX)) | |
163 # for i,x in enumerate(sampleX): | |
164 # weights = array([weightFunc(x,xx, halfwidth) for xx in X]) | |
165 # if sum(weights)>0: | |
166 # smoothed[i] = sum(weights*Y)/sum(weights) | |
167 # else: | |
168 # smoothed[i] = 0 | |
169 # return smoothed | |
170 | |
171 def kernelSmoothing(x, X, Y, weightFunc, halfwidth): | |
172 '''Returns the smoothed estimate of (X,Y) at x | |
173 Sum_x weight(sample_x,x) * y(x)''' | |
174 from numpy import zeros, array | |
175 weights = array([weightFunc(x,observedx, halfwidth) for observedx in X]) | |
176 if sum(weights)>0: | |
177 return sum(weights*Y)/sum(weights) | |
178 else: | |
179 return 0 | |
180 | |
181 def uniform(center, x, halfwidth): | |
182 if abs(center-x)<halfwidth: | |
183 return 1. | |
184 else: | |
185 return 0. | |
186 | |
187 def gaussian(center, x, halfwidth): | |
188 from numpy import exp | |
189 return exp(-((center-x)/halfwidth)**2/2) | |
190 | |
191 def epanechnikov(center, x, halfwidth): | |
192 diff = abs(center-x) | |
193 if diff<halfwidth: | |
194 return 1.-(diff/halfwidth)**2 | |
195 else: | |
196 return 0. | |
197 | |
157 | 198 |
158 def argMaxDict(d): | 199 def argMaxDict(d): |
159 return max(d.iterkeys(), key=(lambda key: d[key])) | 200 return max(d.iterkeys(), key=(lambda key: d[key])) |
160 | 201 |
161 def framesToTime(nFrames, frameRate, initialTime = time()): | 202 def framesToTime(nFrames, frameRate, initialTime = time()): |