How to remove particular attributes from arff file and produce modified arff?

被刻印的时光 ゝ 提交于 2019-12-11 13:28:48

问题


(not manually) i have 96 features and want to remove some 20 features from arff and produce modified arff. used weka for feature selection now want to remove those less imp features. can anyone suggest code for this


回答1:


Here you go... just change the source and destination file path...

import java.io.File;
import weka.core.Instances;
import weka.core.converters.ArffLoader;
import weka.core.converters.ArffSaver;
import weka.filters.Filter;
import weka.filters.unsupervised.attribute.Remove;


public class Convert4 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try
        {
            ArffLoader loader2= new ArffLoader();
            loader2.setSource(new File("C:/Users/RAHUL/Desktop/stack.arff"));
            Instances data2= loader2.getDataSet();
            //Load Arff
             String[] options = new String[2];
             options[0] = "-R";                                    // "range"
             options[1] = "1";                                     // first attribute
             Remove remove = new Remove();                         // new instance of filter
             remove.setOptions(options);                           // set options
             remove.setInputFormat(data2);                          // inform filter about dataset **AFTER** setting options
             Instances newData2 = Filter.useFilter(data2, remove);   // apply filter
             ArffSaver saver = new ArffSaver();
             saver.setInstances(newData2);
             saver.setFile(new File("C:/Users/RAHUL/Desktop/stack2.arff"));
             saver.writeBatch();
}
catch (Exception e)
{}
}
}

Cheers :)




回答2:


Short answer is here for more, check this out https://stackoverflow.com/a/43972890/7588668

BufferedReader datafile = new BufferedReader(new FileReader("bbcsport.arff")); 
BufferedReader attrfile = new BufferedReader(new FileReader("attributes.txt"));


Instances data = new Instances(datafile); 
List<Integer> myList = new ArrayList<Integer>();
String line;

while ((line = attrfile.readLine()) != null) {
  for (n = 0; n < data.numAttributes(); n++) {
    if (data.attribute(n).name().equalsIgnoreCase(line)) {
      if(!myList.contains(n)) 
        myList.add(n); 
    } 
  }
}

int[] attrs = myList.stream().mapToInt(i -> i).toArray();
Remove remove = new Remove();
remove.setAttributeIndicesArray(attrs);
remove.setInvertSelection(false);
remove.setInputFormat(data); // init filter

Instances filtered = Filter.useFilter(data, remove);


来源:https://stackoverflow.com/questions/37086056/how-to-remove-particular-attributes-from-arff-file-and-produce-modified-arff

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