Search code examples
javamachine-learningwekadata-cleaning

How to convert String attribute into a nominal attribute using Weka API in Java


I have a dataset (weka3 Instances object) loaded in weka API. I need to convert an attribute type from String into Nominal. Any one know how to do this ?


Solution

  • I resolved it that way : My Colmn named Situation is the first column in my dataset. I converted it successfully from String type into Nominal type

        //Method to convert "Situation attribute type from String to Nominal"
    private Instances StringToNominal(Instances dataset, String columnName) throws Exception {
            StringToNominal stringtoNominal = new StringToNominal();
             String[] options = new String[2];
                options[0] = "-R";
                options[1] = Integer.toString(dataset.classIndex()+2);  //this changes the Situation Type from String Into Nominal
                stringtoNominal.setOptions(options);
            stringtoNominal.setInputFormat(dataset);
            dataset = Filter.useFilter(dataset, stringtoNominal); 
    
        return dataset;
    }