Skip to content

Commit

Permalink
more graphs
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Weber committed Feb 20, 2009
1 parent 5b4e941 commit bb9b1f6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
40 changes: 34 additions & 6 deletions drawnumpredict.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion numpredict.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down

0 comments on commit bb9b1f6

Please sign in to comment.