Search code examples
wekabayesian-networks

WEKA: How to get the CPT values for every node in BayesNet?


I'm using BayesNet and SimpleEstimator in an unsupervised manner and looking for the joint distribution of the network. I know that by using the following:

BayesNet bn=new BayesNet();
...
SimpleEstimator sbne = new SimpleEstimator();
sbne.estimateCPTs(bn);
...
distributionForInstance(bn,testingsource.instance( i ))

We will get the Conditional Probability Table(CPT) of the class index for instance i. However I don't know how to get the (CPT) for every other node (in addition to the class index).

One way would be to recursively change the class index and again call this function but this would be very inefficient.

I'd be very thankful if you help me to retrieve the estimated CPT for every other node.


Solution

  • Try getProbability method on BayesNet class. Here is what I do.

    for(int i = 0; i < bnet.getCardinality(nodeIndex); i++)
          {
            System.out.print(bnet.getNodeValue(nodeIndex, i) + " = " + bnet.getProbability(nodeIndex, row, i) + " ");
          }
    

    Where row is 0 <= row < bnet.getParentCardinality() and each value of row corresponds to a single combination of parent's output symbols.

    Thus if your node has 2 parents and each parent outputs 2 symbols, then you will have 0 <= row < 4 (2 parents x 2 symbols)

    row = 0 corresponds to combination 0,0 // symbol value at index 0 for parent at index 0 and symbol value at index 0 for parent at index 1

    row = 1 corresponds to combination 0,1

    row = 2 corresponds to combination 1,0 // symbol value at index 1 of parent 0 and so on.

    row = 3 corresponds to combination 1,1


    Here is sample output

    ----- Node --------------

    Name: TuberculosisOrCancer parents: 2 : LungCancer Tuberculosis

    Number of values a node can take: 2

    [0] = yes // index and value

    [1] = no // index and value

    Distributions ---

    When LungCancer = yes, Tuberculosis = no

    yes = 0.9990990990990991 no = 9.009009009009009E-4

    When LungCancer = yes, Tuberculosis = yes

    yes = 0.9444444444444444 no = 0.05555555555555555

    When LungCancer = no, Tuberculosis = no

    yes = 5.3475935828877E-5 no = 0.9999465240641712

    When LungCancer = no, Tuberculosis = yes

    yes = 0.9944444444444445 no = 0.005555555555555556