Save/Append result of SubsetByExpression to ARFF file

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Save/Append result of SubsetByExpression to ARFF file

Gaetano
I have a txt file (file.txt):
2012,1,10
2012,2,10
2012,3,10

And this my arff file (pv3.arff):
@attribute anno numeric
@attribute day numeric
@attribute ora numeric

I should make a selection on pv3.arff file based on the values of the three attributes (values are contained in txt file). The problem is that I can not save the results of the selections based on each line of the txt file. I would like to make a loop that allows me to save based on the values of each line on testSet.arff file but for how i did it i only save the values of the last line.

This is my code:
        DataSource source = new DataSource("C:/Users/user/Documenti/pv3.arff");
        Instances dataset = source.getDataSet();
        File testSet = new File("C:/Users/user/Documenti/testSet.arff");
        ArffSaver saver = new ArffSaver();
        SubsetByExpression filter = new SubsetByExpression();
        BufferedReader reader = new BufferedReader(new FileReader("C:/Users/user/Documenti/file.txt"));
       
        try
        {
            String linea;
            while ( (linea = reader.readLine()) != null)
            {
            String year = "";
            String day = "";
            String hour = "";
            /* Divisione in tokens della linea letta */
                StringTokenizer st = new StringTokenizer(linea,",");
                while (st.hasMoreTokens())
                {
                    year = st.nextToken();
                    day = st.nextToken();
                    hour = st.nextToken();
                    String[] options = new String[2];
                    options[0]="-E";
                    options[1]="(ATT1= "+year+" )"+"and"+"(ATT2= "+day+" )"+"and"+"(ATT3= "+hour+" )";
                    filter.setOptions(options);
                    filter.setInputFormat(dataset);
                    Instances testset = SubsetByExpression.useFilter(dataset, filter);
                    testset.setClassIndex(testset.numAttributes()-1);
                    saver.setInstances(testset);
                    saver.setFile(testSet);
             saver.writeBatch();
                }
             }
          }
        catch(Exception e)
        {
        e.printStackTrace();
        }

How can i modify the code to save on testSet.arff the result of the selection based on the values of every row?
Thanks
Reply | Threaded
Open this post in threaded view
|

Re: Save/Append result of SubsetByExpression to ARFF file

Mark Hall
Possibly the easiest thing to do is to have a master set of instances that you add to from each set that results from a call to SubsetByExpression (testset). Then you can write the master set out to a file just once at the end of the process.

You will need to iterate over the instances in each testset:

for (int i=0; i < testset.numInstances(); i++) {
   masterset.add(testset.instance(i));
}

Cheers,
Mark.

On 30/05/17, 8:27 AM, "Gaetano" <[hidden email] on behalf of [hidden email]> wrote:

    I have a txt file (file.txt):
    2012,1,10
    2012,2,10
    2012,3,10
   
    And this my arff file (pv3.arff):
    @attribute anno numeric
    @attribute day numeric
    @attribute ora numeric
   
    I should make a selection on pv3.arff file based on the values of the three
    attributes (values are contained in txt file). The problem is that I can not
    save the results of the selections based on each line of the txt file. I
    would like to make a loop that allows me to save based on the values of each
    line on testSet.arff file but for how i did it i only save the values of the
    last line.
   
    This is my code:
            DataSource source = new
    DataSource("C:/Users/user/Documenti/pv3.arff");
            Instances dataset = source.getDataSet();
            File testSet = new File("C:/Users/user/Documenti/testSet.arff");
            ArffSaver saver = new ArffSaver();
            SubsetByExpression filter = new SubsetByExpression();
            BufferedReader reader = new BufferedReader(new
    FileReader("C:/Users/user/Documenti/file.txt"));
           
            try
            {
                String linea;
                while ( (linea = reader.readLine()) != null)
                {
                String year = "";
                String day = "";
                String hour = "";
                /* Divisione in tokens della linea letta */
                    StringTokenizer st = new StringTokenizer(linea,",");
                    while (st.hasMoreTokens())
                    {
                        year = st.nextToken();
                        day = st.nextToken();
                        hour = st.nextToken();
                        String[] options = new String[2];
                        options[0]="-E";
                        options[1]="(ATT1= "+year+" )"+"and"+"(ATT2= "+day+"
    )"+"and"+"(ATT3= "+hour+" )";
                        filter.setOptions(options);
                        filter.setInputFormat(dataset);
                        Instances testset =
    SubsetByExpression.useFilter(dataset, filter);
                        testset.setClassIndex(testset.numAttributes()-1);
                        saver.setInstances(testset);
                        saver.setFile(testSet);
                 saver.writeBatch();
                    }
                 }
              }
            catch(Exception e)
            {
            e.printStackTrace();
            }
   
    How can i modify the code to save on testSet.arff the result of the
    selection based on the values of every row?
    Thanks
   
   
   
    --
    View this message in context: http://weka.8497.n7.nabble.com/Save-Append-result-of-SubsetByExpression-to-ARFF-file-tp40801.html
    Sent from the WEKA mailing list archive at Nabble.com.
    _______________________________________________
    Wekalist mailing list
    Send posts to: [hidden email]
    List info and subscription status: https://list.waikato.ac.nz/mailman/listinfo/wekalist
    List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html
   


_______________________________________________
Wekalist mailing list
Send posts to: [hidden email]
List info and subscription status: https://list.waikato.ac.nz/mailman/listinfo/wekalist
List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html