Author Topic: Learning using the Java API  (Read 9127 times)

Offline Anders L Madsen

  • HUGIN Expert
  • Hero Member
  • *****
  • Posts: 2288
    • View Profile
Learning using the Java API
« on: September 23, 2011, 11:10:37 »
Here is an example on learning using the HUGIN Java API
Code: [Select]
/*
 * This simple Java program illustrates how to learn (the parameters)
 * of a Bayesian network from data
 *
 * The result is stored in a file name learning.net
 *
 *
 * Author: Anders L Madsen @ HUGIN EXPERT A/S
 *
 * For any questions or comments, please contact the author at
 * alm@hugin.com
 */
import COM.hugin.HAPI.*;
import java.text.DecimalFormat;

class EnterDataExample {
    // the model
    Domain dom = null;

    // the nodes / variables in the model
    LabelledDCNode Sex, Height;

    public EnterDataExample ()
    {
try {
     // Create domain. Done once.
     dom = new Domain ();
     
    Sex = new LabelledDCNode(dom); Sex.setName ("Sex");
     Height = new LabelledDCNode(dom); Height.setName ("Height");

     Sex.setNumberOfStates (2);
     Sex.setStateLabel (0, "Female");
     Sex.setStateLabel (1, "Male");

     Height.setNumberOfStates (2);
     Height.setStateLabel (0, "Low");
     Height.setStateLabel (1, "High");

     // Add Sex as parent of Height. Comment this line if you
     // uncomment "dom.learnStructure ();" below
     Height.addParent (Sex);

     // create Experience tables to enable CPT estimatio
     Sex.getExperienceTable ();
     Height.getExperienceTable ();

} catch (Exception e) {
     e.printStackTrace ();
     System.err.println (e.getMessage ());      
}
    }

    // Data is an array of strings. First entry is Sex and second is
    // Height;
    String dataArray[][]  =
{
    {"Male", "High"},
     {"Male", "High"},
     {"Male", "High"},
     {"Female", "High"},
     {"Female", "Low"},
     {"Male", "Low"},
     {"Female", "Low"},
     {"Male", "High"},
     {"Male", ""},
     {"Female", "Low"}
};
;
    int N=10;

    // Main procedure. 
    protected void doLearning () {
try{
    // enter data
     enterData ();

     // (learn structure) compile and estimate parameters
     // dom.learnStructure (); // uncomment this line (and the
     // addParent line) above to learn structure from data
     dom.compile ();
     dom.learnTables ();

     // save domain in file
     dom.saveAsNet ("learning.net");
} catch (Exception e) {
     e.printStackTrace ();
     System.err.println (e.getMessage ());      
}
    }

    //
    protected void enterData () {
try{
    long j, s;
     for (int i = 0; i < N; i++) {
j = dom.newCase ();

s = Sex.getStateIndex (dataArray[i][0]);
if (s >= 0)
     Sex.setCaseState (j, s);

s = Height.getStateIndex (dataArray[i][1]);
if (s >= 0)
     Height.setCaseState (j, s);
     }
     //... more data
} catch (Exception e) {
     e.printStackTrace ();
     System.err.println (e.getMessage ());      
}
    }
   
    static public void main (String args[])
    {
EnterDataExample ede = new EnterDataExample ();
ede.doLearning ();
    }
}


HUGIN EXPERT A/S