"""
Maximum Entropy Classifier. Maximum entropy is a technique for learning
probability distributions from data. In maximum entropy models, the
observed data itself is assumed to be the testable information. Maximum
entropy models don't assume anything about the probability distribution
other than what have been observed and always choose the most uniform
distribution subject to the observed constraints.
"""
from java.io import *
from jhplot import *
from smile.classification import Maxent
from jarray import zeros,array
import java
def loadData(xfile):
x = java.util.ArrayList();
y = java.util.ArrayList();
br=BufferedReader(FileReader(xfile));
line = br.readLine()
words = (line.strip()).split(" ");
nseq = int(words[0]);
k = int(words[1]);
p = int(words[2]);
while line is not None:
line = br.readLine()
if (line == None): break
words = (line.strip()).split(" ");
seqid = int(words[0]);
pos = int(words[1]);
xlen = int(words[2]);
feature =[]
for i in range(xlen):
feature.append(int(words[xlen+3]));
x.add(array(feature, "i"))
y.add(int(words[xlen+3]));
return p,x,y
http="http://datamelt.org/examples/data/sequence/"
print "Reading data from",http
datasource="sparse.protein.11.train"
print Web.get(http+datasource)
p,x,y=loadData(datasource)
maxent = Maxent(p, x, y, 0.1, 1E-5, 500);
datasource="sparse.protein.11.test"
print "Now testing ..",Web.get(http+datasource)
ptest,xtest,ytest=loadData(datasource)
pred=maxent.predict(xtest)
rows=len(xtest)
e=0.0
for i in range(rows):
expected=ytest[i]
predicted=pred[i]
print expected," predicted=",predicted
if (expected != predicted): e=e+1.0
e=e/rows
print "Error rate=",e
Ads help maintain this website.