[SOLVED] nested if statements

sukhsukh Member Posts: 43 Contributor II
edited November 2018 in Help
HI all,
i need help in  solving the problem of rapid miner, As i am formulating some rules using nested if. but getting error message.
my rules are as:
two columns: Dtag and Ttag and the result is saved in result column
   Ttag            Dtag        result Column
   present        past            past
   present        present      present
   past             past            past
   past             present        past
   future           past            past
   future           present       future

if(Ttag== “present”, if(Dtag== “past”, “past”, if(Dtag== “present”, “present” , Dtag)),
if(Ttag== “past”, if(Dtag== “past” , “past” , if(Dtag== “present”, “present” , Dtag)),
if(Ttag== “future” , if(Dtag == “past”, “past”, if(Dtag== “present”, “future”, Dtag))))

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<process version="6.3.000">
 <context>
   <input/>
   <output/>
   <macros/>
 </context>
 <operator activated="true" class="process" compatibility="6.3.000" expanded="true" name="Process">
   <process expanded="true">
     <operator activated="true" class="retrieve" compatibility="6.3.000" expanded="true" height="60" name="Retrieve" width="90" x="45" y="30">
       <parameter key="repository_entry" value="//Local Repository/CSVDateTime"/>
     </operator>
     <operator activated="true" class="generate_attributes" compatibility="6.3.000" expanded="true" height="76" name="Generate Attributes" width="90" x="246" y="30">
       <list key="function_descriptions">
         <parameter key="AA" value="if(Ttag== “present”, if(Dtag== “past”, “past”, if(Dtag== “present”, “present” , Dtag)),  if(Ttag== “past”, if(Dtag== “past” , “past” , if(Dtag== “present”, “present” , Dtag)), if(Ttag== “future” , if(Dtag == “past”, “past”, if(Dtag== “present”, “future”, Dtag))))"/>
       </list>
     </operator>
     <connect from_op="Retrieve" from_port="output" to_op="Generate Attributes" to_port="example set input"/>
     <connect from_op="Generate Attributes" from_port="example set output" 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>
 
 Kindly help me.

Thanks and Regards:
Sukh

 

Answers

  • sgenzersgenzer Administrator, Moderator, Employee, RapidMiner Certified Analyst, Community Manager, Member, University Professor, PM Moderator Posts: 2,959 Community Manager
    Hmm...I don't think that works. I would either use the branch/select sub process operator (setting macros for past, present, future) and then do an if statement in each branch, or use two sets of filter examples in the same way. Either way you will be left with only two choices and can use the if statement, set data, etc...

    Scott
  • JEdwardJEdward RapidMiner Certified Analyst, RapidMiner Certified Expert, Member Posts: 578 Unicorn
    Nested If statements can get confusing.  :)
    One way to make it easier to calculate them on RapidMiner is to chain them together as the Generate Attributes operator calculates each expression in series. 
    See this as an example.
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <process version="6.4.000">
      <context>
        <input/>
        <output/>
        <macros/>
      </context>
      <operator activated="true" class="process" compatibility="6.4.000" expanded="true" name="Process">
        <process expanded="true">
          <operator activated="false" class="retrieve" compatibility="6.4.000" expanded="true" height="60" name="Retrieve" width="90" x="45" y="30">
            <parameter key="repository_entry" value="//Local Repository/CSVDateTime"/>
          </operator>
          <operator activated="true" class="subprocess" compatibility="6.4.000" expanded="true" height="76" name="Generate Testing Data" width="90" x="179" y="165">
            <process expanded="true">
              <operator activated="true" class="text:create_document" compatibility="6.4.001" expanded="true" height="60" name="Create Document" width="90" x="45" y="345">
                <parameter key="text" value="Ttag;Dtag;Expected&#10;present;present;present&#10;present;past;past&#10;past;past;past&#10;past;present;past&#10;future;past;past&#10;future;present;future"/>
              </operator>
              <operator activated="true" class="text:write_document" compatibility="6.4.001" expanded="true" height="76" name="Write Document" width="90" x="112" y="435"/>
              <operator activated="true" class="read_csv" compatibility="6.4.000" expanded="true" height="60" name="Read CSV" width="90" x="246" y="480">
                <list key="annotations"/>
                <list key="data_set_meta_data_information">
                  <parameter key="0" value="Ttag.true.nominal.attribute"/>
                  <parameter key="1" value="Dtag.true.nominal.attribute"/>
                  <parameter key="2" value="ExpectedResult.true.nominal.attribute"/>
                </list>
              </operator>
              <connect from_op="Create Document" from_port="output" to_op="Write Document" to_port="document"/>
              <connect from_op="Write Document" from_port="file" to_op="Read CSV" to_port="file"/>
              <connect from_op="Read CSV" from_port="output" to_port="out 1"/>
              <portSpacing port="source_in 1" spacing="0"/>
              <portSpacing port="sink_out 1" spacing="0"/>
              <portSpacing port="sink_out 2" spacing="0"/>
            </process>
          </operator>
          <operator activated="true" class="generate_attributes" compatibility="6.4.000" expanded="true" height="76" name="Generate Attributes" width="90" x="447" y="120">
            <list key="function_descriptions">
              <parameter key="AA" value="if(((Ttag == &quot;present&quot;) &amp;&amp; (Dtag == &quot;present&quot;)),&quot;present&quot;,&quot;TBC&quot;)"/>
              <parameter key="AA" value="if(((Ttag == &quot;present&quot;) &amp;&amp; (Dtag == &quot;past&quot;) &amp;&amp; (AA == &quot;TBC&quot;)),&quot;past&quot;,AA)"/>
              <parameter key="AA" value="if(((Ttag == &quot;past&quot;) &amp;&amp; (Dtag == &quot;past&quot;) &amp;&amp; (AA == &quot;TBC&quot;)),&quot;past&quot;,AA)"/>
              <parameter key="AA" value="if(((Ttag == &quot;past&quot;) &amp;&amp; (Dtag == &quot;present&quot;) &amp;&amp; (AA == &quot;TBC&quot;)),&quot;past&quot;,AA)"/>
              <parameter key="AA" value="if(((Ttag == &quot;future&quot;) &amp;&amp; (Dtag == &quot;past&quot;) &amp;&amp; (AA == &quot;TBC&quot;)),&quot;past&quot;,AA)"/>
              <parameter key="AA" value="if(((Ttag == &quot;future&quot;) &amp;&amp; (Dtag == &quot;present&quot;) &amp;&amp; (AA == &quot;TBC&quot;)),&quot;future&quot;,AA)"/>
            </list>
            <description align="center" color="transparent" colored="false" width="126">Generate Attributes executes each calulation in series so you can chain them together.</description>
          </operator>
          <connect from_op="Generate Testing Data" from_port="out 1" to_op="Generate Attributes" to_port="example set input"/>
          <connect from_op="Generate Attributes" from_port="example set output" 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>
  • Marco_BoeckMarco_Boeck Administrator, Moderator, Employee, Member, University Professor Posts: 1,993 RM Engineering
    Hi,

    you can use nested if functions without problems, however when using such highly nested ones, you must absolutely make sure each function consists of the 3 different parts: The condition, the result for condition = true and the result for condition = false.

    if (condition, valueForTrue, valueForFalse)
    Your expression does not do that, you have multiple if expressions left which do not consist of these 3 parts.

    Simple example:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <process version="6.5.000-SNAPSHOT">
      <context>
        <input/>
        <output/>
        <macros/>
      </context>
      <operator activated="true" class="process" compatibility="6.0.002" expanded="true" name="Process">
        <process expanded="true">
          <operator activated="true" class="retrieve" compatibility="6.5.000-SNAPSHOT" expanded="true" height="60" name="Retrieve Golf" width="90" x="45" y="30">
            <parameter key="repository_entry" value="//Samples/data/Golf"/>
          </operator>
          <operator activated="true" class="generate_attributes" compatibility="6.5.000-SNAPSHOT" expanded="true" height="76" name="Generate Attributes" width="90" x="179" y="30">
            <list key="function_descriptions">
              <parameter key="Temperature_Nominal" value="if (Temperature&lt;=75, &quot;cool&quot;, if (Temperature&gt;=85, &quot;hot&quot;, &quot;medium&quot;))"/>
            </list>
          </operator>
          <connect from_op="Retrieve Golf" from_port="output" to_op="Generate Attributes" to_port="example set input"/>
          <connect from_op="Generate Attributes" from_port="example set output" 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>
    Regards,
    Marco
  • sukhsukh Member Posts: 43 Contributor II
    Dear All,

    I have tried using generate attribute with series of expressions, it worked accordingly.
    Thanks alot.

    Regards:
    Sukh
Sign In or Register to comment.