0
votes

I am using InputMappedClassifer in weka to replace all the missing attribute values by a missing value ("?") but for some reason I still get the Value not defined for given nominal attribute exception my code is as follows :

Instance mappedInstance = inputMappedClassifier.constructMappedInstance(testData.instance(index));

clsLabel = inputMappedClassifier.classifyInstance(mappedInstance);
mappedInstance.setClassValue(clsLabel);

String key = mappedInstance.stringValue(mappedInstance.numAttributes() - 1);

Note that the method to load the inputmappedclassifier is this:

public static InputMappedClassifier loadInputMappedClassifier(SerializedClassifier serializedClassifier, String modelName) throws Exception {
    InputMappedClassifier inputMappedClassifier = new InputMappedClassifier();
    inputMappedClassifier.setClassifier(serializedClassifier);
    try {
        inputMappedClassifier.setModelPath("src/main/resources/" + modelName + ".model");
    } catch (Exception e) {
        e.printStackTrace();
    }
    inputMappedClassifier.setModelHeader((Instances) SerializationHelper.readAll("src/main/resources/" + modelName + ".model")[1]);
    inputMappedClassifier.setDebug(true);
    return inputMappedClassifier;
}

The exception is as follows :

java.lang.IllegalArgumentException: Value not defined for given nominal attribute!
    at weka.core.AbstractInstance.setValue(AbstractInstance.java:507)
    at classifiers.Verbs.loadTestInstance(Verbs.java:122)
    at realizer.TestRealizer.main(TestRealizer.java:119)

I create a test instance using java code and I want to classify it using the inputmappedclassifier but the test instance could contain an instance value that does not exist in the nominal attribute values of the training dataset therefore it will raise that exception. If I get all of this right.

For example the nominal attribute .

@attribute LemmaLast {a,o,n,e,l,y,d,s,r,t,z,u,c,b,f,p,m,é,ú,i,k,j,x,v,g,á,ó,w,.,í,h,7,q,4,+,3,0,5,9,6,2,1,!,à,ç,8}

which represent the last character in a lemma. The last character of the test instance could be '|' which is not presented in the nominal value attributes therefore the inputmappedclassifier should automatically fill that value with ? (missing value)

1

1 Answers

0
votes

If you want to replace all the missing attribute values, then you should use Remove Missing Values Fiter:

   public Instances removeMissingValues(Instances data) throws Exception {        
            ReplaceMissingValues replaceMissingValues = new ReplaceMissingValues();
            replaceMissingValues.setInputFormat(data);
            return Filter.useFilter(data, replaceMissingValues);
    }

but if you need to change the names of some values, you can use Rename Nominal Values Filter

I think that you didn't point your issue correctly. Please add your exception log here and example part of your data.

--edit

Probably you put a new instance with the value where the nominal attribute doesn't have this value. When you create a nominal instance in weka you have to specify nominal values. For example: (red, blue, white) but when you try to classify a instance with value=black then this exception is raised.