简体   繁体   中英

How can I copy attribute in java weka?

I am using Weka libraries for feature selection problem solution. I have read data as follows : The data set is in arff format

    BufferedReader reader = new BufferedReader(new FileReader("D:\\Exp\\golf.arff"));
    Instances data = new Instances(reader);
    reader.close();
    data.setClassIndex(data.numAttributes() - 1);

For example, attributes are Temperature, humidity, Windy, Outlook and the last one is class.

Now I want to store Temperature attribute as instances. The new data will consists only temperature (and it should be instance type because in further processing I have to use methods of instance)

Quick google search brought this: Instead of picking the features just remove all features that you don't need.

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)
{}
}
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM