Saeide
Saeide

Reputation: 149

remove specific attributes(columns) of weka instances

I've done kind of attribute selection(Information gain) in Weka. After that it returns new data with the new arrange of attributes due to the importance of each attribute within information gain method. I want to remove one or more columns of new data to just have informative attribute in my dataset. Here you can see my code:

    Instances data = new Instances(new BufferedReader(new FileReader("iris.arff")));

    InfoGainAttributeEval eval = new InfoGainAttributeEval();
    Ranker search = new Ranker();

    AttributeSelection attSelect = new AttributeSelection();
    attSelect.setEvaluator(eval);
    attSelect.setSearch(search);
    attSelect.SelectAttributes(data);

    int[] indices = attSelect.selectedAttributes();

    data = attSelect.reduceDimensionality(data); //re-arrange attributes but not remove them

Thanks in advance!

Upvotes: 1

Views: 5445

Answers (1)

Henry Gouk
Henry Gouk

Reputation: 36

You can use the Remove filter to accomplish this. Specifically, something along these lines should achieve the desired effect:

Remove removeFilter = new Remove();
removeFilter.setAttributeIndicesArray(indices);
removeFilter.setInvertSelection(true);
removeFilter.setInputFormat(data);
Instances newData = Filter.useFilter(data, removeFilter);

This assumes that indices contains the indices of attributes that you want to keep. If it contains the indices of the attributes you want to remove, the delete the call to the setInvertSelection method.

Upvotes: 2

Related Questions