I needed to run a Bayesian network in Java, so I searched for a nice library.
As a condition
--Maintenance is still ongoing --Distributed as a jar file --Complete tutorial
In the Bayesian network library I searched for a person who could learn the network structure and conditional probabilities.
The following two can be used
Other than that, the following
--Netica-j (Error could not be resolved) --jayes (Build failed, GitHub update stopped 5-6 years ago and there is an unmaintained atmosphere) --BayesServer (It feels really good, but it costs about 70,000 yen)
weka Weka is a machine learning software developed at the University of Waikato in New Zealand, and jar files can also be used as GUI applications. This time, I will use the class in the jar file in Java. I used this for the data for learning. https://gist.github.com/carl0967/7a3588cd6f0d40d02a26
Below source code
import java.io.*;
import java.util.*;
import weka.core.converters.ArffLoader;
import weka.classifiers.Evaluation;
import weka.classifiers.bayes.BayesNet;
import weka.classifiers.bayes.net.search.SearchAlgorithm;
import weka.classifiers.bayes.net.search.local.SimulatedAnnealing;
import weka.classifiers.bayes.net.search.local.K2;
import weka.core.Instances;
import weka.core.converters.ConverterUtils.DataSource;
public class bayesNet{
Instances instances;
BayesNet bnet;
Evaluation evaluation;
public bayesNet(){}
void setFile(File dataFile){
try {
ArffLoader al = new ArffLoader();
al.setFile(dataFile);
instances = al.getDataSet();
instances.setClassIndex(instances.numAttributes() - 1);
} catch (Exception e) {
e.printStackTrace();
}
}
void buildClassifier(){
try{
bnet = new BayesNet();
//Search algorithm generation
SearchAlgorithm searchAlgorithm = new K2();
//Set on BayesNet
bnet.setSearchAlgorithm(searchAlgorithm);
//Start classification
bnet.buildClassifier(instances);
} catch(Exception e){
e.printStackTrace();
}
}
void evalute(){
try{
//Evaluation
evaluation = new Evaluation(instances);
evaluation.evaluateModel(bnet, instances);
} catch(Exception e){
e.printStackTrace();
}
}
void showResult(){
System.out.println(evaluation.toSummaryString("Results\n",false));
}
public static void main(String[] args) {
bayesNet classifier = new bayesNet();
classifier.setFile(new File(args[0]));
classifier.buildClassifier();
classifier.evalute();
classifier.showResult();
}
}
Reference: Create a Bayesian network using Weka's API in Java
result
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by weka.core.WekaPackageClassLoaderManager (file:/Users/Mnb0130/unirvFILE/semi/Engineering special exercise/bayesNet/weka/weka.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of weka.core.WekaPackageClassLoaderManager
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Results
Correctly Classified Instances 34 89.4737 %
Incorrectly Classified Instances 4 10.5263 %
Kappa statistic 0.6122
Mean absolute error 0.204
Root mean squared error 0.299
Relative absolute error 59.8749 %
Root relative squared error 73.3056 %
Total Number of Instances 38
If the correct answer rate is about 89% with a small data set, I think that it is an assumed movement operation. After all, I felt that using weka was appropriate in most cases.
Recommended Posts