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

qwertzqwertz Member Posts: 130  Maven
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

Sachs


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

Answers

  • Marco_BoeckMarco_Boeck Team Lead Software Engineering Administrator, Moderator, Employee, Member, University Professor Posts: 1,905   RM Engineering
    Hi,

    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">
     <context>
       <input/>
       <output/>
       <macros/>
     </context>
     <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>
         <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;"/>
         </operator>
         <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"/>
       </process>
     </operator>
    </process>
    Note that this implementation will be slow on larger data as it is just an example.

    Regards,
    Marco
  • qwertzqwertz Member Posts: 130  Maven
    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:
    http://fossies.org/dox/rapidminer-5.3.013/interfacecom_1_1rapidminer_1_1example_1_1Attribute.html



    Best regards
    Sachs
Sign In or Register to comment.