"Get and set values using Groovy Script"

韦塞尔韦塞尔 MemberPosts:537Guru
edited May 2019 inHelp
Dear All,

I want to learn how to use groovy script.

How to get example values for a particular attribute?
and how to set example values for a particular attribute?

The example by Ingo, pasted below only shows how to iterate over all attributes and all examples.
This is nice, but what if you only want to iterate over all examples of attribute 1,
calculate the sum, and place the result in attribute 2?
How to do this?

Like you start with:

att1 att2
0.1 NaN
0.2 NaN
0.3 NaN
0.4 NaN

And the result will be:
att1 att2
0.1 0.1
0.2 0.3
0.3 0.6
0.4 1.0

Best regards,

Wesesl

ExampleSet exampleSet = operator.getInput(ExampleSet.class);

exampleSet.recalculateAllAttributeStatistics();

for (Attribute attribute : exampleSet.getAttributes()) {
double mean = exampleSet.getStatistics(attribute, Statistics.AVERAGE);
String name = attribute.getName();
for (Example example : exampleSet) {
example[name] = example[name] - mean;
}
}

return exampleSet;

Tagged:

Answers

  • colocolo MemberPosts:236Maven
    Hi wessel,

    I guess you don't have the tutorial for the extension development at hand. This starts with an example for using the script operator. Since the other existing documentation is very rare, I built a little process for your example (just containing other values). I added a third attribute to demonstrate the creation of new attributes. The whole process is appended to the end of the post, here comes just the content of the script operator:
    import com.rapidminer.tools.Ontology;

    ExampleSet exampleSet = input[0];

    Attributes attributes = exampleSet.getAttributes();
    Attribute att1 = attributes.get("att1");
    Attribute att2 = attributes.get("att2");

    //generate additional attribute
    Attribute att3 = AttributeFactory.createAttribute("att3", Ontology.REAL);
    //add new attribute to example set
    attributes.addRegular(att3);
    //insert new column into example set's data table
    exampleSet.getExampleTable().addAttribute(att3);

    double sum = 0.0;

    for (Example example : exampleSet) {
    double currentValue = example.getValue(att1);
    sum += currentValue;
    example.setValue(att2, sum);
    example.setValue(att3, sum);
    }

    return exampleSet;
    I hope this makes things a bit clearer.

    Best regards
    Matthias










    <参数键= " number_examples " value = " 5 " / >






    <参数键= " invert_selection " value = " true " / >






























  • dgibbonsdgibbons MemberPosts:5Contributor II
    Hi all,

    Thank you for the help above, very useful.

    我有一个简单的脚本(商店wn below) to add 300 to each numerical example. However when I run the script the script outputs correctly but also changes all the previous blocks in my process. Is it possible to make the script operator work in one direction only? i.e. not effect previous results in the process?

    Many Thanks,
    大卫


    ExampleSet exampleSet2 = input[0];


    Attributes attributes = exampleSet2.getAttributes();
    Attribute att2 = attributes.get("Midterm Exam");

    String name = att2.getName();
    for (Example example : exampleSet2) {

    example[name] = example[name] + 300;
    }


    return exampleSet2;











































  • dgibbonsdgibbons MemberPosts:5Contributor II
    HI,

    I think that this RapidMiner description which I found:

    "The process logic which RapidMiner uses is not "linear", but recursive. We dont apply operators linearly, one after another."

    explains my query a bit. Could anybody expand on this description please?

    Thanks a lot,
    大卫
  • MariusHelfMariusHelf RapidMiner Certified Expert, MemberPosts:1,869Unicorn
    If you change the actual data and not just meta-data like attribute names, roles or add and remove attributes, the changes are also reflected to previous data, because by default only the meta-data is copied for each operator, but not the actual data. This is necessary for performance and memory usage reasons.

    You can however create a deep copy of an example set prior to your script with the Materialize operator. That way the changes won't get progagated backwards.

    Best, Marius
  • dgibbonsdgibbons MemberPosts:5Contributor II
    Thank you Marius, that works. My issue regarding the script operator is [SOLVED].

    That explanation makes sense. I will be more careful with the operators. By any chance, is there a way to turn off the default setting (meta-data only passed) for a process?
  • MariusHelfMariusHelf RapidMiner Certified Expert, MemberPosts:1,869Unicorn
    No, that's not possible.
  • dgibbonsdgibbons MemberPosts:5Contributor II
    Hello,

    Could you please let me know if it is possible to select a specific instance attribute?

    For example, the first example in the attribute "Midterm Exam".

    Attributes attributes = exampleSet.getAttributes();
    Attribute exam = attributes.get("Mideterm Exam");

    float ff = exam.getElementAt[0];


    Many Thanks.
Sign InorRegisterto comment.