简体   繁体   中英

How to use Naive Bayes of Weka in a Java program

I have been working in a machine learning project. I was using Kstar and it gave me good results. In order to gain more accuracy I am going to use Naive Bayes. When I try to do something like this

try {
        NaiveBayes ks= new NaiveBayes();
        String[] options={"-B", "20"};
        ks.setOptions(options);
        for (String s : ks.getOptions()) {
            System.err.println(s);
        }
        classifier=ks;

It gives this error

java.lang.Exception: Illegal options: -B 20

When I tried this

try {
        NaiveBayes ks= new NaiveBayes();
        String[] options={"-K", "20"};
        ks.setOptions(options);
        for (String s : ks.getOptions()) {
            System.err.println(s);
        }
        classifier=ks;

It gives the following error

java.lang.Exception: Illegal options: 20

So I tried this

try {
        NaiveBayes ks= new NaiveBayes();
        String[] options={"-K"};
        ks.setOptions(options);
        for (String s : ks.getOptions()) {
            System.err.println(s);
        }
        classifier=ks;

This worked. But the accuracy is so much lesser than of Kstar. I think the reason is that not setting options for an Polynomial Kernel. So I need to set options properly(to give a exponent value for the polynomial kernel)

Please help me here

You can do a grid search for tune the parameters. Take the code under a for loop and iterate the algorithm through a series of different values for the exponent value.Which after you find a good value.ran a grid search again with range of parameters very close to the selected value. That way you can find a more accurate parameters

Each classifier has its own set of parameters. The KStar contains the '-B 20' option (for Manual Blend) but is not a parameter for NaiveBayes. The NaiveBayes -K option is also used for Kernel Density Estimation but is not a parameter for KStar.

As for the accuracy of the NaiveBayes model, it is difficult to say whether it is to do with these settings or not, the structure of the data or the fit of the model. Hopefully these answers will be revealed to you further as you continue your experimentation of the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM