~ubuntu-branches/ubuntu/raring/python-scipy/raring-proposed

« back to all changes in this revision

Viewing changes to Lib/maxentropy/examples/conditionalexample1.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-01-07 14:12:12 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070107141212-mm0ebkh5b37hcpzn
* Remove build dependency on python-numpy-dev.
* python-scipy: Depend on python-numpy instead of python-numpy-dev.
* Package builds on other archs than i386. Closes: #402783.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Test for conditional models
 
2
# Ed Schofield, 2006
 
3
 
 
4
from numpy import *
 
5
from scipy.maxentropy import *
 
6
 
 
7
# Two contexts W, four labels x
 
8
# E_p f_0(W, X) = 0.4
 
9
# where f_0(w, x) = indicator func "is the label x=0 and is the context w=0?"
 
10
# So we want the distribution:
 
11
# x \ w         0       1
 
12
# 0             0.4     0.25
 
13
# 1             0.2     0.25
 
14
# 2             0.2     0.25
 
15
# 3             0.2     0.25
 
16
 
 
17
# We can achieve this by creating a feature matrix with one row per constraint,
 
18
# as follows:
 
19
F = array([[1, 0, 0, 0, 0, 0, 0, 0]])
 
20
# Each column represents one (w, x) pair.  The number of columns is the product
 
21
# |w| * |x|, here 8.  The order is (w0,x0), (w0,x1), (w0, x2), ..., (w1, x0),
 
22
# etc.
 
23
numcontexts = 2
 
24
numlabels = 4
 
25
 
 
26
# OLD:
 
27
# These can be in any order. The indices_context parameter to the
 
28
# conditionalmodel constructor records this order, so indices_context[0] is an
 
29
# array of indices all labels x in context w=0.  The simplest ordering is:
 
30
#     (w0, x0), (w0, x1), ..., (w0, x{n-1}), (w1, x0), ...
 
31
# in which case the parameter is:
 
32
# indices_context = array([[0, 1, 2, 3], [4, 5, 6, 7]])
 
33
 
 
34
# The counts of each (w, x) pair, estimated from a corpus or training dataset, is
 
35
# stored as an array with |w| * |x| elements in same order as before.
 
36
counts = array([4, 3, 2, 1, 4, 3, 2, 1])
 
37
# Note that, since the only active feature was for the first element (w0, x0),
 
38
# only the first value is relevant.  The others are subject to no constraints,
 
39
# and will be chosen to maximize entropy.
 
40
 
 
41
model = conditionalmodel(F, counts, numcontexts)
 
42
model.verbose = True
 
43
model.fit()
 
44
# Do it again, since the CG algorithm gets stuck sometimes.  Why is this??
 
45
model.fit()
 
46
# Note: to use the bound-constrained limited memory BFGS algorithm instead, we
 
47
# would use:
 
48
# model.fit(algorithm='LBFGSB')
 
49
 
 
50
# Display the fitted model
 
51
pmf = model.pmf()
 
52
# The elements of this are flatted like the rows of F and p_tilde.  We display
 
53
# them nicely:
 
54
print "x \ w \t 0 \t 1",
 
55
for x in range(4):
 
56
    print '\n' + str(x),
 
57
    for w in range(2):
 
58
        print ' \t %.3f' % pmf[w*numlabels + x],
 
59
        # print ' \t %.3f' % pmf[indices_context[w]][x],
 
60
print
 
61