How to learn a Bayesian Network (structure+parameters) with the WEKA API?
Asked Answered
P

1

7

Does anyone know the "proper" procedure to learn a Bayesian Network from data using the WEKA API? I can't find good instructions in the WEKA documentation.

Based on the documentation and what each function is "supposed" to do, I thought this would work:

Instances ins = DataSource.read( filename );
ins.setClassIndex(0);

K2 learner = new K2();

MultiNomialBMAEstimator estimator = new MultiNomialBMAEstimator();
estimator.setUseK2Prior(true);

EditableBayesNet bn = new EditableBayesNet( ins );
bn.initStructure();

learner.buildStructure(bn, ins);
estimator.estimateCPTs(bn);

But it doesn't. I've tried this and other variations and I keep getting ArrayIndexOutOfBoundsException or NullPointerException somewhere inside WEKA code, so what am I missing?

Pedalfer answered 26/5, 2011 at 18:51 Comment(2)
I was looking for resources to get started with Bayesian networks. Will have a look at the WEKA API you mentioned.Arkwright
Does the WEKA GUI itself use the API? If so, you could use that as an example. You could also take a peek at the source code just to see if any of it makes sense.Distillate
M
5

It works for me. I tried with the following set of data:

@relation test

@attribute x {0,1}
@attribute y {0,1,2}
@attribute z {0,1}

@data
0,1,0
1,0,1
1,1,1
1,2,1
0,0,0

Let me mention that exceptions are expected when your target attribute is not nominal (e.g. numeric). Bayesian Networks work better when all your attributes are nominal. If you change the target attribute to numeric you'll get a NullPointerException or an ArrayIndexOutOfBoundsException. In particular, this exception is thrown at the line:

EditableBayesNet bn = new EditableBayesNet(ins);

You should first discretize your target class.

Melicent answered 26/5, 2011 at 20:44 Comment(1)
I don't know what was wrong before but it works for me too now.Pedalfer

© 2022 - 2024 — McMap. All rights reserved.