[Solved] How to address single examples with the script operator?

qwertzqwertz Member Posts: 130 Contributor II
edited November 2018 in Help
Dear community,

I recently started to use the script operator. In this context I studied the sample processes.
But now I have another challenge.

(To make the explanation easier the question is simplified. So it might be able to use other operators but in this case I look especially for a script realization.)

The problem: How to access example 4 of attribute "att1", write it to a variable "var" and finally write "var" to example 6.

Looking forward to your comments
Kind regards


PS: Is there on overview of all these useful functions like .getStatistics, .getName, etc.?


  • Options
    Marco_BoeckMarco_Boeck Administrator, Moderator, Employee, Member, University Professor Posts: 1,996 RM Engineering

    the "Execute Script" operator essentially can do everything you could do by calling the Java API of Studio.
    You will have to work with the Studio API (e.g. in Eclipse) to see what you can do.

    Your script in question would be:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <process version="6.1.000">
     <operator activated="true" class="process" compatibility="6.1.000" expanded="true" name="Process">
       <process expanded="true">
         <operator activated="true" class="retrieve" compatibility="6.1.000" expanded="true" height="60" name="Retrieve Iris" width="90" x="45" y="30">
           <parameter key="repository_entry" value="//Samples/data/Iris"/>
         <operator activated="true" class="execute_script" compatibility="6.1.000" expanded="true" height="76" name="Execute Script" width="90" x="179" y="30">
           <parameter key="script" value="ExampleSet es = input[0];&#10;&#10;Attribute att = es.getAttributes().get(&quot;a1&quot;);&#10;double value = es.getExample(4).getValue(att);&#10;es.getExample(6).setValue(att, value);&#10;&#10;return input;"/>
         <connect from_op="Retrieve Iris" from_port="output" to_op="Execute Script" to_port="input 1"/>
         <connect from_op="Execute Script" from_port="output 1" to_port="result 1"/>
         <portSpacing port="source_input 1" spacing="0"/>
         <portSpacing port="sink_result 1" spacing="0"/>
         <portSpacing port="sink_result 2" spacing="0"/>
    Note that this implementation will be slow on larger data as it is just an example.

  • Options
    qwertzqwertz Member Posts: 130 Contributor II
    Fantastic! Thank you so much! Especially as you provided this code which is easy to follow. When I first saw the sample processes which come with Rapidminer I was a little confused:

    "for (Attribute attribute : exampleSet.getAttributes())"

    As a newbie in programming I am not so familiar with the syntax. But this seems to be a for each loop which does multiple things like initializing an iteration variable, getting all the attributes and iterating over all the attributes. Without knowing it better I probably would have done it in three lines.

    Meanwhile I also found a kind of documentation, but not sure whether this is official and probably it is not maintained any longer. However, it might help someone out there:

    Best regards
Sign In or Register to comment.