"writing an Input Filter for One-Class SVM"

niccay
niccay New Altair Community Member
edited November 5 in Community Q&A
Hello,

I'm using a dataset with a binary (nominal) label property I'd like to classify by a One-Class SVM. In my project, I'm importing my dataset with a csv Import Operator and using this as Input for a X-validation operator.

X-Validation contains just a simple One-Class (Lib)SVM Learner and for testing a Apply Model+Performance Operator.

Unfortunately the One ClassLibSVM Learner expects my trainingsset to just have 1 label value (I'm having 2 different values..), thus I thought writing a Filter throwing away positive(minority) class examples should be sufficient for this task (see source below). Unfortunately it doesn't. Well, I think I've to fix the Nominal Mapping of my label attribute but I'm not sure where to start and what to do.

Does anyone has a clue how to solve my issue? (But please do me a little favor and don't refer to the whitepaper)

Thanks,
Nico

/*
*
* Removes minority class examples of exampleset in order to use an one-class SVM
*
* */
public ExampleSet apply(ExampleSet trainingsset) throws OperatorException {
List<Example> majorityExamples = new LinkedList<Example>();
NominalAttribute label = (NominalAttribute) trainingsset.getAttributes().getLabel();
double minorityLabelValue = getMinorityClass(trainingsset);


// only care iff exampleset contains 2 different classes
if(label.isNominal() && label.getMapping().size()==2)

for (Example e : trainingsset)
{
if(e.getLabel()!=minorityLabelValue)
majorityExamples.add(e);

}

// NOT WORKING :-(
//NominalMapping labelMapping = label.getMapping();
//(String majorityLabelMapping = label.getMapping().getNegativeString();
//labelMapping.clear();
//labelMapping.setMapping(majorityLabelMapping, 0);

return computeExampleSet(majorityExamples);
}

protected ExampleSet computeExampleSet(List<Example> examples)
{
int anzBsp = examples.size();
int anzAtr = examples.get(0).getAttributes().size();
double[][] values = new double[anzBsp][anzAtr];
double[] labels = new double[anzBsp];
String[] attrNames = new String[anzAtr];
String labelName;
int i=0,n=0;

for(Attribute a : examples.get(0).getAttributes()) {
attrNames = a.getName();
i++;
}
Attribute label = examples.get(0).getAttributes().getLabel();
labelName = label.getName();
i=0;
for(Example e : examples) {
n=0;
for(Attribute a : e.getAttributes()) {
values = e.getValue(a);
n++;
}
labels = e.getLabel();
i++;
}

return ExtendedExampleSetFactory.createExampleSet(values,labels,attrNames,labelName,label.getMapping());


}

public static ExampleSet createExampleSet(double[][] data, double[] labels, String[] attrNames, String labelName, NominalMapping lblMapping) {
        if (data.length == 0) {
            throw new RuntimeException("ExampleSetFactory.createExampleSet(double[][], double[]): data matrix is not allowed to be empty.");
        }
               
        // create attributes
        int numberOfAttributes = data[0].length;
        List<Attribute> attributeList = new ArrayList<Attribute>(numberOfAttributes + (labels != null ? 1 : 0));
        for (int a = 0; a < numberOfAttributes; a++) {
            attributeList.add(AttributeFactory.createAttribute(attrNames, Ontology.NUMERICAL));
        }
        Attribute labelAttribute = null;
       
        if (labels != null) {
            labelAttribute = AttributeFactory.createAttribute(labelName, Ontology.NOMINAL);
            labelAttribute.setMapping(lblMapping);
            attributeList.add(labelAttribute);
        }

       
        // create table
        MemoryExampleTable table = new MemoryExampleTable(attributeList);
        for (int e = 0; e < data.length; e++) {
            double[] dataRow = data;
            if (labelAttribute != null) {
                dataRow = new double[numberOfAttributes + 1];
                System.arraycopy(data, 0, dataRow, 0, data.length);
                dataRow[dataRow.length - 1] = labels;
            }
            table.addDataRow(new DoubleArrayDataRow(dataRow));
        }
       
        return table.createExampleSet(labelAttribute);
    }

Answers

  • land
    land New Altair Community Member
    Hi,
    actually there's a whitepaper... :P Just a joke.

    Check out the CommunityExtension and download the process about one class svms. Marco Stolpe describe in another thread, that he has created a process for training and validating a one class SVM on a trainingset with binominal label.

    Greetings,
      Sebastian