1
# Test for conditional models
5
from scipy.maxentropy import *
7
# Two contexts W, four labels x
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:
17
# We can achieve this by creating a feature matrix with one row per constraint,
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),
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]])
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.
41
model = conditionalmodel(F, counts, numcontexts)
44
# Do it again, since the CG algorithm gets stuck sometimes. Why is this??
46
# Note: to use the bound-constrained limited memory BFGS algorithm instead, we
48
# model.fit(algorithm='LBFGSB')
50
# Display the fitted model
52
# The elements of this are flatted like the rows of F and p_tilde. We display
54
print "x \ w \t 0 \t 1",
58
print ' \t %.3f' % pmf[w*numlabels + x],
59
# print ' \t %.3f' % pmf[indices_context[w]][x],