Problem with the Execute-Script-Operator

Thrhw31Thrhw31 Member Posts: 2 Contributor I
edited November 2018 in Help
Hey,

I'm trying to generate an attribute Y in a moving sum from another attribute X:

Y(1) = X(1)

for i = 2 ... length(X) {

Y(i) = X(i) + Y(i-1)

}

The Result should look like this:
X Y
1 1
0 1
0 1
1 2
0 2
I do not found a possibility to do this in Rapidminer and therefore I used the Execute-Script-Operator. Unfortunately I have nearly no experience in Java/Groovy :( The problem with my script is that it overwrites my input attribute X. I would be very grateful if someone have a look on the code below.

Greetings from Germany

import com.rapidminer.example.table.NumericalAttribute;
import com.rapidminer.tools.Ontology;

ExampleSet input= input[0];
Attributes all_attributes = input.getAttributes();
Attribute bool_attrib = all_attributes.get("X");
Attribute target = AttributeFactory.createAttribute("Y", Ontology.INTEGER)

target.setTableIndex(bool_attrib.getTableIndex());
all_attributes.addRegular(target);
ExampleTable bool_tbl = input.getExampleTable();

for (int i = 0; i < bool_tbl.size(); i++) {

DataRow tmp_DataRow = bool_tbl.getDataRow(i);
if (i == 0)
{
tmp_DataRow.set(target, tmp_DataRow.get(bool_attrib));
}

if (i != 0)
{
tmp_DataRow.set(target, tmp_DataRow.get(bool_attrib) + bool_tbl.getDataRow(i-1).get(target));
}
}
return (input);

Best Answer

  • Marco_BoeckMarco_Boeck Administrator, Moderator, Employee, Member, University Professor Posts: 1,993 RM Engineering
    Solution Accepted

    Hi,

     

    here is your fixed script:

     

    import com.rapidminer.tools.Ontology

    ExampleSet inputExampleSet = input[0];

    Attributes inputAttributes = inputExampleSet.getAttributes();
    Attribute sourceAttribute = inputAttributes.get("ai_description");
    Attribute targetAttribute = AttributeFactory.createAttribute("ai_description_numberOfBulletp", Ontology.INTEGER);

    inputAttributes.addRegular(targetAttribute);
    inputExampleSet.getExampleTable().addAttribute(targetAttribute);

    for (Example actExample : inputExampleSet) {
    int counter = 0;
    String actValue = actExample.getNominalValue(sourceAttribute);
    for( int i=0; i<actValue.length(); i++ ) {
    if(actValue.charAt(i) == '•') {
    counter++;
    }

    }
    actExample.setValue(targetAttribute, counter);
    }

    return(inputExampleSet);

    You just need to call "inputExampleSet.getExampleTable().addAttribute(targetAttribute);" after adding a new attribute to the Attributes.

     

    Regards,

    Marco

     

Answers

  • MariusHelfMariusHelf RapidMiner Certified Expert, Member Posts: 1,869 Unicorn
    Hi,

    what you need is the Integrate operator from the Value Series extension. This will get the job done without any coding :)

    Best regards,
    Marius
  • jhillerjhiller Member Posts: 12 Contributor II

    Hi Thrhw31,

     

    I have the same issue in another context.

    In my for loop (for exploring the examples) I'm taking the text from a column into a string-variable. Then I count the occurences of a specific character and want wo write the result in a new column. Unfortunately the values of my source-column are overwritten.

     

    Have you got a solution for that issue?

     

    My code:

    import com.rapidminer.tools.Ontology

    ExampleSet inputExampleSet = input[0];

    Attributes inputAttributes = inputExampleSet.getAttributes();
    Attribute sourceAttribute = inputAttributes.get("ai_description");
    Attribute targetAttribute = AttributeFactory.createAttribute("ai_description_numberOfBulletp", Ontology.INTEGER);

    targetAttribute.setTableIndex(sourceAttribute.getTableIndex());
    inputAttributes.addRegular(targetAttribute);

    for (Example actExample : inputExampleSet) {
    int counter = 0;
    String actValue = actExample.getNominalValue(sourceAttribute);
    for( int i=0; i<actValue.length(); i++ ) {
    if(actValue.charAt(i) == '•') {
    counter++;
    }

    }
    actExample.setValue(targetAttribute, counter);
    }

    return(inputExampleSet);

     

    Regards

    Johannes

     

  • jhillerjhiller Member Posts: 12 Contributor II

    Hi again,

     

    i think the sourceAttribute is overwritten by that line:

        actExample.setValue(targetAttribute, counter);

    Has anyone an idea to do that in a better way?

     

    Regards

    Johannes

  • jhillerjhiller Member Posts: 12 Contributor II

    Solved!

     

    Thanks a lot!

Sign In or Register to comment.