Generate a Function & apply it to all/some attributes automatically

1705410G
1705410G New Altair Community Member
edited November 2024 in Community Q&A
I've created a new attribute using the Generate Attribute operator. 
Attribute name N_speed, with 'N' representing the location. 
E.g. 2_speed represents speed at location 2. 

(New) Attribute Name:             2speed 
Function Expressions:             if([2_speed]>=70,"DANGER","SAFE").

However, I have nearly 300 attributes. I've tried adding new entries and replacing each with the name of the next attribute, but its just too much work. 

Hence, Is there any way to apply the same function to all attributes automatically? 

if([2_speed]>=70,"DANGER","SAFE")
if([3_speed]>=70,"DANGER","SAFE")
if([4_speed]>=70,"DANGER","SAFE")
if([5_speed]>=70,"DANGER","SAFE")
if([6_speed]>=70,"DANGER","SAFE")
if([7_speed]>=70,"DANGER","SAFE") and so on... 


Thanks.

Regards,
AY

Best Answers

  • lionelderkrikor
    lionelderkrikor New Altair Community Member
    Answer ✓
    Hi @1705410G,

    You have to put the Generate Attributes operator inside the Loop Attributes operator : 
    Here a process to show you how to proceed : 
    <?xml version="1.0" encoding="UTF-8"?><process version="9.2.000-SNAPSHOT">
      <context>
        <input/>
        <output/>
        <macros/>
      </context>
      <operator activated="true" class="process" compatibility="9.2.000-SNAPSHOT" expanded="true" name="Process">
        <parameter key="logverbosity" value="init"/>
        <parameter key="random_seed" value="2001"/>
        <parameter key="send_mail" value="never"/>
        <parameter key="notification_email" value=""/>
        <parameter key="process_duration_for_mail" value="30"/>
        <parameter key="encoding" value="SYSTEM"/>
        <process expanded="true">
          <operator activated="true" class="operator_toolbox:create_exampleset" compatibility="1.7.000" expanded="true" height="68" name="Create ExampleSet" width="90" x="112" y="136">
            <parameter key="generator_type" value="numeric_series"/>
            <parameter key="number_of_examples" value="10"/>
            <parameter key="use_stepsize" value="false"/>
            <list key="function_descriptions"/>
            <parameter key="add_id_attribute" value="false"/>
            <list key="numeric_series_configuration">
              <parameter key="att_1" value="linear.0\.0.10\.0"/>
              <parameter key="att_2" value="linear.10\.0.20\.0"/>
            </list>
            <list key="date_series_configuration"/>
            <list key="date_series_configuration (interval)"/>
            <parameter key="date_format" value="yyyy-MM-dd HH:mm:ss"/>
            <parameter key="column_separator" value=","/>
            <parameter key="parse_all_as_nominal" value="false"/>
            <parameter key="decimal_point_character" value="."/>
            <parameter key="trim_attribute_names" value="true"/>
          </operator>
          <operator activated="true" class="concurrency:loop_attributes" compatibility="9.2.000-SNAPSHOT" expanded="true" height="82" name="Loop Attributes" width="90" x="380" y="136">
            <parameter key="attribute_filter_type" value="all"/>
            <parameter key="attribute" value=""/>
            <parameter key="attributes" value=""/>
            <parameter key="use_except_expression" value="false"/>
            <parameter key="value_type" value="attribute_value"/>
            <parameter key="use_value_type_exception" value="false"/>
            <parameter key="except_value_type" value="time"/>
            <parameter key="block_type" value="attribute_block"/>
            <parameter key="use_block_type_exception" value="false"/>
            <parameter key="except_block_type" value="value_matrix_row_start"/>
            <parameter key="invert_selection" value="false"/>
            <parameter key="include_special_attributes" value="false"/>
            <parameter key="attribute_name_macro" value="loop_attribute"/>
            <parameter key="reuse_results" value="true"/>
            <parameter key="enable_parallel_execution" value="true"/>
            <process expanded="true">
              <operator activated="true" class="generate_attributes" compatibility="9.2.000-SNAPSHOT" expanded="true" height="82" name="Generate Attributes" width="90" x="313" y="85">
                <list key="function_descriptions">
                  <parameter key="%{loop_attribute}_bis" value="if(eval(%{loop_attribute})&gt;5,&quot;true&quot;,&quot;false&quot;)"/>
                </list>
                <parameter key="keep_all" value="true"/>
              </operator>
              <connect from_port="input 1" to_op="Generate Attributes" to_port="example set input"/>
              <connect from_op="Generate Attributes" from_port="example set output" to_port="output 1"/>
              <portSpacing port="source_input 1" spacing="0"/>
              <portSpacing port="source_input 2" spacing="0"/>
              <portSpacing port="sink_output 1" spacing="0"/>
              <portSpacing port="sink_output 2" spacing="0"/>
            </process>
          </operator>
          <connect from_op="Create ExampleSet" from_port="output" to_op="Loop Attributes" to_port="input 1"/>
          <connect from_op="Loop Attributes" 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>
    
    Hope it helps,

    Regards,

    Lionel

Answers

  • 1705410G
    1705410G New Altair Community Member
    edited January 2019
    @ mschmitz 

    Hi! I tried using it earlier on, but failed.
    Hence, What are the Parameters of Loop Attributes I should make changes to?

    How can I insert the function expression?

    Regards,
    AY

  • lionelderkrikor
    lionelderkrikor New Altair Community Member
    Answer ✓
    Hi @1705410G,

    You have to put the Generate Attributes operator inside the Loop Attributes operator : 
    Here a process to show you how to proceed : 
    <?xml version="1.0" encoding="UTF-8"?><process version="9.2.000-SNAPSHOT">
      <context>
        <input/>
        <output/>
        <macros/>
      </context>
      <operator activated="true" class="process" compatibility="9.2.000-SNAPSHOT" expanded="true" name="Process">
        <parameter key="logverbosity" value="init"/>
        <parameter key="random_seed" value="2001"/>
        <parameter key="send_mail" value="never"/>
        <parameter key="notification_email" value=""/>
        <parameter key="process_duration_for_mail" value="30"/>
        <parameter key="encoding" value="SYSTEM"/>
        <process expanded="true">
          <operator activated="true" class="operator_toolbox:create_exampleset" compatibility="1.7.000" expanded="true" height="68" name="Create ExampleSet" width="90" x="112" y="136">
            <parameter key="generator_type" value="numeric_series"/>
            <parameter key="number_of_examples" value="10"/>
            <parameter key="use_stepsize" value="false"/>
            <list key="function_descriptions"/>
            <parameter key="add_id_attribute" value="false"/>
            <list key="numeric_series_configuration">
              <parameter key="att_1" value="linear.0\.0.10\.0"/>
              <parameter key="att_2" value="linear.10\.0.20\.0"/>
            </list>
            <list key="date_series_configuration"/>
            <list key="date_series_configuration (interval)"/>
            <parameter key="date_format" value="yyyy-MM-dd HH:mm:ss"/>
            <parameter key="column_separator" value=","/>
            <parameter key="parse_all_as_nominal" value="false"/>
            <parameter key="decimal_point_character" value="."/>
            <parameter key="trim_attribute_names" value="true"/>
          </operator>
          <operator activated="true" class="concurrency:loop_attributes" compatibility="9.2.000-SNAPSHOT" expanded="true" height="82" name="Loop Attributes" width="90" x="380" y="136">
            <parameter key="attribute_filter_type" value="all"/>
            <parameter key="attribute" value=""/>
            <parameter key="attributes" value=""/>
            <parameter key="use_except_expression" value="false"/>
            <parameter key="value_type" value="attribute_value"/>
            <parameter key="use_value_type_exception" value="false"/>
            <parameter key="except_value_type" value="time"/>
            <parameter key="block_type" value="attribute_block"/>
            <parameter key="use_block_type_exception" value="false"/>
            <parameter key="except_block_type" value="value_matrix_row_start"/>
            <parameter key="invert_selection" value="false"/>
            <parameter key="include_special_attributes" value="false"/>
            <parameter key="attribute_name_macro" value="loop_attribute"/>
            <parameter key="reuse_results" value="true"/>
            <parameter key="enable_parallel_execution" value="true"/>
            <process expanded="true">
              <operator activated="true" class="generate_attributes" compatibility="9.2.000-SNAPSHOT" expanded="true" height="82" name="Generate Attributes" width="90" x="313" y="85">
                <list key="function_descriptions">
                  <parameter key="%{loop_attribute}_bis" value="if(eval(%{loop_attribute})&gt;5,&quot;true&quot;,&quot;false&quot;)"/>
                </list>
                <parameter key="keep_all" value="true"/>
              </operator>
              <connect from_port="input 1" to_op="Generate Attributes" to_port="example set input"/>
              <connect from_op="Generate Attributes" from_port="example set output" to_port="output 1"/>
              <portSpacing port="source_input 1" spacing="0"/>
              <portSpacing port="source_input 2" spacing="0"/>
              <portSpacing port="sink_output 1" spacing="0"/>
              <portSpacing port="sink_output 2" spacing="0"/>
            </process>
          </operator>
          <connect from_op="Create ExampleSet" from_port="output" to_op="Loop Attributes" to_port="input 1"/>
          <connect from_op="Loop Attributes" 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>
    
    Hope it helps,

    Regards,

    Lionel

  • 1705410G
    1705410G New Altair Community Member
    edited January 2019
    Hi lionelderkrikor ,

    Do I have to change the attribute name and function to my own attribute name or should I leave it to what you've provided me?

    As I kept getting this error inside the Generate Attribute operator.
    Expression Evaluation Error: The function 'eval' failed to parse the subexpression '2_speed'. Cause: token recognition error at '_'  

    And how do I make an exception for my date_time? So that it wouldn't be involved in any looping?

    P.S. I've also changed my function to if(([2_speed]<=35),"LOW",if(([2_speed]<=53),"MODERATE",if(([2_speed]<=70),"HIGH","VERY HIGH"))) instead. How should I insert it?

    Regards,
    AY
  • 1705410G
    1705410G New Altair Community Member
    edited January 2019
    Hi lionelderkrikor ,

    Also, after pasting it in the process, I do not have the loop_attribute macro  (inside Generate Attribute) as compared to pasting it into a brand new process. Does it matter?

    Regards,
    AY

  • 1705410G
    1705410G New Altair Community Member
    edited January 2019
    Hi Telcontar120 

    Sorry, I don't get what you mean. I followed the steps shown in the video below but the error stills occur.

    Expression Evaluation Error: The function 'eval' failed to parse the subexpression '2_speed'. Cause: token recognition error at '_'  .

    Does it mean that I have to remove the underscore used in all my attributes' name?

    Video Reference: https://academy.rapidminer.com/learn/video/loop-attributes

    Regards,
    AY
  • SGolbert
    SGolbert New Altair Community Member

    at this point providing a sample dataset and process would help a lot.

    Regards,
    Sebastian
  • 1705410G
    1705410G New Altair Community Member
    SGolbert,

    Sorry, but I won't be able to share any dataset and process as it is school related. :( 

    However, what I has is 2 Loop Attributes Operator, each containing a Generate Attribute Operator.

    Attribute Name(Inside Generate Attribute) for both Loop Attributes Operator: %{loop_attribute}

    Function for 1st Loop Attributes Operator: if((eval(%{loop_attribute})<=35),"LOW",if((eval(%{loop_attribute})<=53),"MODERATE",if((eval(%{loop_attribute})<=70),"HIGH","VERY HIGH")))

    Function for 1st Loop Attributes Operator: if((eval(%{loop_attribute})<=35),"LOW",if((eval(%{loop_attribute})<=53),"MODERATE",if((eval(%{loop_attribute})<=70),"HIGH","VERY HIGH")))

    This are the errors I've been receiving (Shown in below). 
    But I've selected subset in attribute filter type. Iterations should not have been 0. 



    I am very confused. Once I solved the first error, second error pops up. Solved it, first error back again and vice versa. I've changed my naming countless times, with underscore to no underscore, numbers (10) to spelling (ten). 

    I have no idea what went wrong.

    Hope this can help you better analyse!

    Regards,
    AY

  • 1705410G
    1705410G New Altair Community Member
    edited January 2019
    -
  • 1705410G
    1705410G New Altair Community Member
    Hi lionelderkrikor ,

    Just to add on, how do I copy your codes into my process without removing my other existing operators? As I can only paste into a new process.

    Regards,
    AY
  • 1705410G
    1705410G New Altair Community Member
    edited January 2019
    -
  • 1705410G
    1705410G New Altair Community Member
    Hi lionelderkrikor ,

    Just to add on, how do I copy your codes into my process without removing my other existing operators? As I can only paste into a new process.

    Regards,
    AY
  • 1705410G
    1705410G New Altair Community Member
    Hi lionelderkrikor ,

    Do I have to change the attribute name and function inside the Generate Attribute operator to my own attribute name or should I leave it to what you've provided me? 

    As I kept getting this error inside the Generate Attribute operator.
    Expression Evaluation Error: The function 'eval' failed to parse the subexpression '2_speed'. Cause: token recognition error at '_'  

    Regards,
    AY
  • 1705410G
    1705410G New Altair Community Member
    @ mschmitz 

    Hi! I tried using it earlier on, but failed.
    Hence, What are the Parameters of Loop Attributes I should make changes to?


    Regards,
    AY
  • 1705410G
    1705410G New Altair Community Member
    SGolbert,

    Sorry, but I won't be able to share any dataset and process as it is school related. :( 

    However, what I has is 2 Loop Attributes Operator, each containing a Generate Attribute Operator.

    Attribute Name(Inside Generate Attribute) for both Loop Attributes Operator: %{loop_attribute}

    Function for 1st Loop Attributes Operator: if((eval(%{loop_attribute})<=35),"LOW",if((eval(%{loop_attribute})<=53),"MODERATE",if((eval(%{loop_attribute})<=70),"HIGH","VERY HIGH")))

    Function for 1st Loop Attributes Operator: if((eval(%{loop_attribute})<=35),"LOW",if((eval(%{loop_attribute})<=53),"MODERATE",if((eval(%{loop_attribute})<=70),"HIGH","VERY HIGH")))

    This are the errors I've been receiving (Shown in below). 
    But I've selected subset in attribute filter type. Iterations should not have been 0. 



    I am very confused. Once I solved the first error, second error pops up. Solved it, first error back again and vice versa. I've changed my naming countless times, with underscore to no underscore, numbers (10) to spelling (ten). 

    I have no idea what went wrong.

    Regards,
    AY
  • 1705410G
    1705410G New Altair Community Member
    Hi lionelderkrikor ,

    Do I have to change the attribute name and function inside the Generate Attribute operator to my own attribute name or should I leave it to what you've provided me? 

    As I kept getting this error inside the Generate Attribute operator.
    Expression Evaluation Error: The function 'eval' failed to parse the subexpression '2_speed'. Cause: token recognition error at '_'  

    Regards,
    AY
  • 1705410G
    1705410G New Altair Community Member
    @ mschmitz 

    Hi! I tried using it earlier on, but failed.
    Hence, What are the Parameters of Loop Attributes I should make changes to?


    Regards,
    AY