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)