How to learn a Bayesian Network (structure+parameters) with the WEKA API?
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 w开发者_开发百科hat 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?
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.
精彩评论