Midou
Midou

Reputation: 57

i want to show the number of instances for each attribute value in weka java

package test;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.converters.ArffLoader.ArffReader;

public class main {

    public static void main(String[] args) {
        String s = null;
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(
                new FileReader(
                    "C:\\Program Files\\Weka-3-8\\data\\weather.numeric.arff"
                )
            );
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        ArffReader arff = null;
        try {
            arff = new ArffReader(reader);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Instances data = arff.getData();
        data.setClassIndex(data.numAttributes() - 1);
        System.out.println("The number of attributes is  : " +data.numAttributes());
        for(int i= 0; i< data.numAttributes(); i++){
            if(data.attribute(i).isNominal()){
                s = data.attribute(i).name().toString();
                System.out.println(
                    "the " + s + " attribute is nominal and takes "
                    + data.attribute(i).numValues() + " values"
                );
            }
        }
    }
}

In the code above I am showing for each attribute the number of values that it takes, but i'd like rather to show values (Strings) that it takes I am working on the file weather.numeric.arff. here is the file description :

@relation weather

@attribute outlook {sunny, overcast, rainy}
@attribute temperature numeric
@attribute humidity numeric
@attribute windy {TRUE, FALSE}
@attribute play {yes, no}

@data
sunny,85,85,FALSE,no
sunny,80,90,TRUE,no
overcast,83,86,FALSE,yes
rainy,70,96,FALSE,yes
rainy,68,80,FALSE,yes
rainy,65,70,TRUE,no
overcast,64,65,TRUE,yes
sunny,72,95,FALSE,no
sunny,69,70,FALSE,yes
rainy,75,80,FALSE,yes
sunny,75,70,TRUE,yes
overcast,72,90,TRUE,yes
overcast,81,75,FALSE,yes
rainy,71,91,TRUE,no

Upvotes: 2

Views: 2234

Answers (1)

xro7
xro7

Reputation: 751

You can print the String values of the nominal attribute using this: data.attribute(attributeNo).value(position)

where attributeNois the number of the attribute and position is an integer that indicates the position of the nominal value as defined in the .arff file.

e.g data.attribute(0).value(0) is attribute's outlook , sunny nominal value

Upvotes: 1

Related Questions