Weka一类SVM:如何为预测设置类值?

时间:2019-05-16 20:03:12

标签: java weka one-class-classification

我正在尝试使用weka库训练一类SVM模型。 我在定义测试数据集时遇到麻烦。模型支持仅一元类。那么我应该如何在测试集中设置预测?

我尝试了几种设置,但其中一些以异常值或正常实例的错误或错误分配为结尾。

    // define instances
    Instances trainSet = new Instances("OneClassSVMDataset",
            new ArrayList<Attribute>() {{
                add(new Attribute("value"));
                add(new Attribute("class"));
            }},0);
    trainSet.setClassIndex(1);

    Instances testSet = new Instances(trainSet);

    // training instance
    Instance trainInst = new DenseInstance(2);
    trainInst.setValue(0, 1);
    trainInst.setValue(1, 1);
    trainInst.setDataset(trainSet);

    // testing instance
    Instance testInst = new DenseInstance(2);
    testInst.setValue(0, 1);
    testInst.setDataset(testSet);


    // set instances as values 1 or 0 
    for (int i = 0; i < 20; i++) {
        trainInst.setValue(0, 1);
        trainSet.add(trainInst);
        trainInst.setValue(0, 0);
        trainSet.add(trainInst);
    }

    // this instance should be normal
    testInst.setValue(0, 0);
    testSet.add(testInst);
    // this instance should be normal
    testInst.setValue(0, 1);
    testSet.add(testInst);
    // this instance should be outlier
    testInst.setValue(0, -10);
    testSet.add(testInst);
    // this instance should be outlier
    testInst.setValue(0, 10);
    testSet.add(testInst);

    // convert class attribute to nominal
    NumericToNominal toNominal = new NumericToNominal();
    toNominal.setAttributeIndicesArray(new int[]{1});
    try {
        toNominal.setInputFormat(trainSet);
        trainSet = Filter.useFilter(trainSet, toNominal);
        testSet = Filter.useFilter(testSet, toNominal);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // create One-Class SVM classifficator 
    LibSVM oneClassSVM = new LibSVM();
    oneClassSVM.setOptions(weka.core.Utils.splitOptions("-S 2 -K 2 -W 1 -V"));
    // train model
    oneClassSVM.buildClassifier(trainSet);

    // test model
    Evaluation eval = new Evaluation(trainSet);
    eval.evaluateModel(oneClassSVM, testSet);

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

下面的代码将所有测试实例作为“忽略的类未知实例”返回

某些值的分布(括号中的分布): -1(0.0); 0(0.0); 1(0.0); 2(0.0); 0.3(1.0); 1000(0.0)

0 个答案:

没有答案