Search code examples
javamachine-learningattributesweka

Java Weka count number of appearances of distinct values of an attribute


I have an attribute (Using Weka!)

@attribute age {10-19,20-29,30-39,40-49,50-59,60-69,70-79,80-89,90-99}

Now, for a given Instances data, I want to get the number of appearances of each Instance at attribute age. So if in data I have 50 instances with the attribute age 10-19, 100 people with the attribute age 20-29, and 150 people with the attribute age 30-39, then I want to count it, and get something like:

age 10-19: 50
age 20-29: 100
age 30-39: 150
age 40-49: 0
age 50-59:0
...
age 90-99: 0

My first guess was just doing some naive approach to count each one of them using a hash map or something like that, but I think there is a better solution out there. Any help would be appreciated (even the naive approach). Thanks!


Solution

  • Found one solution.

    for attribute as index i, by looping through the dataset and finding the attribute value index of an instance by instance.value(i) for attribute i.

    Instances training = loadData("...");
    int[] countAttributes = new int[training.attribute(i).numValues()];
    for(Instance instance: training){
        countAttributes[(int)instance.value(i)]++;
    }