Maximum Entropy Classifier for data using Smile
Code: "classify_mec.py". Programming language: Python DMelt Version 2.2. Last modified: 03/13/2018. License: Pro
https://datamelt.org/code/cache/classify_mec_6804.py
To run this script using the DMelt IDE, copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.


"""
 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




You see the box below because you did not login.