New attribute created for PredictionModel subclass doesn't show up

colo
colo New Altair Community Member
edited November 5 in Community Q&A
Hi,

I am currently stuck with a quite simple problem. I have created a subclass of PredictionModel and modified the method createPredictionAttributes() as follows:
protected Attribute createPredictionAttributes(ExampleSet exampleSet, Attribute label) {
// create and add prediction attribute

/* make the label attribute independent from an existing label in the example set (since this had to be generated as dummy when classifying documents)
* original code:
* Attribute predictedLabel = AttributeFactory.createAttribute(label, Attributes.PREDICTION_NAME);
* predictedLabel.clearTransformations();
*/

Attribute predictedLabel = AttributeFactory.createAttribute(Attributes.PREDICTION_NAME, Ontology.NOMINAL);

ExampleTable table = exampleSet.getExampleTable();
Attributes attributes = exampleSet.getAttributes();

table.addAttribute(predictedLabel);
attributes.setPredictedLabel(predictedLabel);

// check whether confidence labels should be constructed
if (supportsConfidences(predictedLabel)) {
Iterator<String> labelIterator = model.classIndex.iterator();
while (labelIterator.hasNext()) {
String labelValue = labelIterator.next();
Attribute confidence = AttributeFactory.createAttribute(Attributes.CONFIDENCE_NAME + "(" + labelValue + ")", Ontology.REAL);
table.addAttribute(confidence);
attributes.setSpecialAttribute(confidence, Attributes.CONFIDENCE_NAME + "_" + labelValue);
}

// create an additional attribute for the confidence of the predicted value
Attribute predictionConfidence = AttributeFactory.createAttribute(Attributes.CONFIDENCE_NAME, Ontology.REAL);
table.addAttribute(predictionConfidence);
attributes.setSpecialAttribute(predictionConfidence, Attributes.CONFIDENCE_NAME);
}

return predictedLabel;
}
Creating the "confidence(...)" attributes works fine. After this I want to have an additional attribute "confidence" that holds the confidence of the value that was actually predicted. When printing the attribute to console I get the index of the attribute, which is the number of the existing attributes + 1. But the additional attribute doesn't show up in my example set after "Apply Model". I tried using different names or adding it as regular attribute. No chance...

I would be glad about any help with this.

Best regards
Matthias

Answers

  • Marco_Boeck
    Marco_Boeck New Altair Community Member
    Hi,

    I'm sorry it took so long to answer, but it has been rather busy, and being ill didn't help anything either :-[
    If you want to add attributes to an ExampleSet, you need to call these two methods:

    exampleSet.getExampleTable().addAttribute(newAttribute);
    exampleSet.getAttributes().addRegular(newAttribute);
    Regards,
    Marco
  • colo
    colo New Altair Community Member
    Hi Marco,

    I will let you get away with this answer because you were ill :D Did you even have a look at my code? ;)
    I have created new attributes pretty often, but in this case it simply isn't possible. I tried regular attributes as well as the shown attempt to add a special attribute. The strange thing is, that the attribute seems to be added (as printing out the attribute id indicates) but won't become visible. I guess there is some special context for the PredictionModel which is using a header example set, but I can't understand that adding lots of attributes for the confidences is working and an additional single one simply won't work...

    Regards
    Matthias
  • Marco_Boeck
    Marco_Boeck New Altair Community Member
    Hi,

    well yes, I did look at your code, but I may have misinterpreted the problem a bit :P
    This is something I wrote a couple of weeks ago which is working just fine:

    exampleSet.getExampleTable().addAttribute(newAttribute);
    exampleSet.getAttributes().addRegular(newAttribute);
    exampleSet.getAttributes().setSpecialAttribute(newAttribute, "YourSpecialAttributeName");
    Regards,
    Marco
  • colo
    colo New Altair Community Member
    Hi Marco,

    this works fine for me also - at any place and for any operator I modified or created except the PredictionModel subclass. I really can't understand this, since the "confidence(<labelValue>)" attributes are created properly.

    I added this for testing
    Attribute newAttribute = AttributeFactory.createAttribute("TestAttribut", Ontology.NOMINAL);
    exampleSet.getExampleTable().addAttribute(newAttribute);
    exampleSet.getAttributes().addRegular(newAttribute);
    exampleSet.getAttributes().setSpecialAttribute(newAttribute, "YourSpecialAttributeName");
    System.out.println(newAttribute);
    and get
    #16: TestAttribut (nominal/single_value)/values=[]
    printed to the console. Before adding the attribute via exampleSet.getAttributes().addRegular(newAttribute); the id is -1, so adding it seems to work but it is not displayed!

    I also tried to create this attribute if it's not existing from the other method performPrediction() implemented for this class, but it's the same behavior here.

    It seems like I will have to add another workaround and create the attribute before the "Apply Model" operator is used. But I think you should test this in the special context given if you find the time for this.

    Edit:
    I just discovered another "funny" thing, when applying my workaround. I added "Generate Empty Attribute" and "Set Role" before using the "Apply Model" operator. Setting the attribute's values is done inside the performPrediction() method of the implemented model. But I only got missing values for the attribute. This was due to the code I initially posted, that should create this attribute. After commenting this out the correct values were assigned. So I must correct my opinion. Attempting to add the attribute is not only ignored it's also destroying existing attributes having the same name.  :-\


    Regards
    Matthias