SMO,Sequential Minimal Optimization in WEKA

若如初见. 提交于 2020-01-23 18:29:16

问题


I'm new with Weka. I want to use Sequential Minimal Optimization in WEKA. Could anyone tell me how to proceed? here is my Java code but it doesn't work:

public class SVMTest {
public void test(File input) throws Exception{
File tmp = new File("tmp-file-duplicate-pairs.arff");
String path = input.getParent();
//tmp.deleteOnExit();
////removeFeatures(input,tmp,useType,useNames, useActivities, useOccupation,useFriends,useMailAndSite,useLocations);
Instances data = new weka.core.converters.ConverterUtils.DataSource(tmp.getAbsolutePath()).getDataSet();
data.setClassIndex(data.numAttributes() - 1);
Classifier c = null;        
String ctype = null;
boolean newmodel = false;

ctype ="SMO";
c = new SMO();
String[] options = {"-M"};
c.setOptions(options);
c.buildClassifier(data);
newmodel = true;
//c = loadClassifier(input.getParentFile().getParentFile(),ctype);
if(newmodel)
    saveModel(c,ctype, input.getParentFile().getParentFile());
Evaluation eval = new Evaluation(data);
eval.crossValidateModel(c, data, 10, new Random(1));

System.out.println(c);
System.out.println(eval.toSummaryString());
System.out.println(eval.toClassDetailsString());
System.out.println(eval.toMatrixString());

tmp.delete();
}
 private static void saveModel(Classifier c, String name, File path) throws Exception {

ObjectOutputStream oos = null;
try {
    oos = new ObjectOutputStream(
            new FileOutputStream(path.getAbsolutePath()+"/"+name+".model"));
} catch (FileNotFoundException e1) {
    e1.printStackTrace();
} catch (IOException e1) {
    e1.printStackTrace();
}
oos.writeObject(c);
oos.flush();
oos.close();

 }
}

I want to know how to provide .arff file? my Dataset is in the form of XML files.


回答1:


I guess you have figured it out by now, but in case it helps others, there is a wiki page about it:

http://weka.wikispaces.com/Text+categorization+with+WEKA

to use SMO, let's say you have some train instances "trainset", and a test set "testset" to build the classifier:

            // train SMO and output model
            SMO classifier = new SMO();
            classifier.buildClassifier(trainset);

to evaluate it using cross validation for example:

    Evaluation eval = new Evaluation(testset);
    Random rand = new Random(1); // using seed = 1
    int folds = 10;
    eval.crossValidateModel(classifier, testset, folds, rand);

then eval holds all the stats, etc.




回答2:


You can Read input file from these line:

Instances training_data = new Instances(new BufferedReader(
        new FileReader("tmp-file-duplicate-pairs.arff")));
training_data.setClassIndex(training_data.numAttributes() - 1);



回答3:


The following link explains about using SMO in weka http://preciselyconcise.com/apis_and_installations/training_a_weka_classifier_in_java.php



来源:https://stackoverflow.com/questions/9451651/smo-sequential-minimal-optimization-in-weka

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!