From bb9b1f6aa7cd53c9ea4e5a705ee2ecc3fedffea9 Mon Sep 17 00:00:00 2001 From: Nicolas Weber Date: Fri, 20 Feb 2009 08:53:58 -0800 Subject: [PATCH] more graphs --- drawnumpredict.py | 40 ++++++++++++++++++++++++++++++++++------ numpredict.py | 3 ++- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/drawnumpredict.py b/drawnumpredict.py index 2dcddcc..8950d6a 100644 --- a/drawnumpredict.py +++ b/drawnumpredict.py @@ -2,17 +2,45 @@ from pylab import * +S = 1.0 + def cumulativegraph(data, vec1, high, k=5, weightfun=numpredict.gaussianweight): - t1 = arange(0.0, high, 1) + t1 = arange(0.0, high, S) cprob = array([numpredict.probguess(data, vec1, 0, v, k, weightfun) for v in t1]) plot(t1, cprob) - show() # this does user interaction + + +def probabilitygraph(data, vec1, high, k=5, weightfun=numpredict.gaussianweight, + ss=5.0/10): + t1 = arange(0.0, high, S) + probs = [numpredict.probguess(data, vec1, v, v+S, k, weightfun) for v in t1] + + # gaussian smooth with nearby points + smoothed = [] + for i in range(len(probs)): # O(n^2) + sv = 0.0 + for j in range(0, len(probs)): + dist = abs(i - j)*0.1 + weight = numpredict.gaussianweight(dist, sigma=ss) + sv += weight*probs[j] + smoothed.append(sv) + + plot(t1, array(probs)) + plot(t1, array(smoothed)) + if __name__ == '__main__': s = numpredict.wineset3(k=500) - # Draw graph that shows p(price | [rating=99, age=20]) - print 'Real price 1:', numpredict.wineprice(99.0, 20.0) - print 'Real price 2:', 0.6 * numpredict.wineprice(99.0, 20.0) - cumulativegraph(s, [99.0, 20.0], 120) + import pprint + pprint.pprint(s) + + #wine = [99.0, 20.0] # choose params were the sample data is somewhat dense + wine = [99.0, 20.0] # choose params were the sample data is somewhat dense + # Draw graph that shows p(price | wine) + print 'Real price 1:', numpredict.wineprice(wine[0], wine[1]) + print 'Real price 2:', 0.6 * numpredict.wineprice(wine[0], wine[1]) + cumulativegraph(s, wine, 120) + probabilitygraph(s, wine, 120) + show() # this does user interaction diff --git a/numpredict.py b/numpredict.py index 654968c..678368b 100644 --- a/numpredict.py +++ b/numpredict.py @@ -71,7 +71,8 @@ def subtractweight(dist, const=1.0): return max(0, const - dist) -def gaussianweight(dist, sigma=10.0): +#def gaussianweight(dist, sigma=10.0): +def gaussianweight(dist, sigma=5.0): return math.exp(-0.5 * (dist/sigma)**2)