Map operator -entering multiple old values on one line in the value mapping parameter

B00093053B00093053 Member Posts: 2 Contributor I
edited November 2018 in Help

Hi,

 

I need to map a large range old of values to one new value. For example I need to change instances values 1,2,3,4,5,6.... to the value Red.

Is it possible to enter them all on the one line in the value mappings parameter and what is the syntax. Or do i need to add a new line for each old attribute?

 

Thanks,

J.

Answers

  • Thomas_OttThomas_Ott RapidMiner Certified Analyst, RapidMiner Certified Expert, Member Posts: 1,761 Unicorn

    You could use the Generate Attributes operator and do something like this:

     

    If(attribute < 10, "Red", "Not Red")

  • B00093053B00093053 Member Posts: 2 Contributor I

    Hi T-Bone,

     

    Thanks for the response.

    The old values that I want to replace are nominal attributes, and they range from 1 to 999 and there are also some alpha numeric codes as well such as V1 to V10 and E1 to E10.

    So for example 1 to 20, and 25 and V1 might correspond to Red, 30 to 35, and V2 might correspond to Green, 50 to 100, and 125 and E1 might correspond to Yellow etc.

     

    The Map operator works for what I want to do, but I don't want to have to enter a separate line for each individual code as it would be too time consuming. Is it possible to enter a range of old values on one line of the value mapping parameter that maps to one new value?

     

    I think the problem with using Generate Attributes is if I replace the code 8 with Red, it will replace the 8 in 18, 28, 38, 48 etc with Red was well.

     

    Thanks,

    J.

  • Telcontar120Telcontar120 Moderator, RapidMiner Certified Analyst, RapidMiner Certified Expert, Member Posts: 1,635 Unicorn

    @B00093053 You may also be interested in checking out the "Replace (Dictionary)" operator, which allows you to create a separate input txt file of all the replacements you want to make and to do it that way---you can use Excel or something similar to generate the input values quickly.  If I have a large volume of substitutions to do I find it easier than using "Map" and doing all the data entry in RapidMiner by hand. 

    The other option would be to use "Parse Numbers" first and then do the "Generate Attributes" using the GT/LT logic.  But you may need to copy the original attribute into multiple attributes first if it contains both numbers and characters.

     

     

     

     

     

    Brian T.
    Lindon Ventures 
    Data Science Consulting from Certified RapidMiner Experts
  • JEdwardJEdward RapidMiner Certified Analyst, RapidMiner Certified Expert, Member Posts: 578 Unicorn

    Hi @B00093053 ,

     

    Yes you can.  May I introduce you to regular expressions. 

    They are pretty powerful and will allow you to do what you need, you just need to find the right one for each replacement.

    Here's a wee example using RegEx in both the Generate Attributes & Mapping operators. 

     

    <?xml version="1.0" encoding="UTF-8"?><process version="7.4.000">
    <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="subprocess" compatibility="7.4.000" expanded="true" height="82" name="Generate Pretend Data" width="90" x="179" y="85">
    <process expanded="true">
    <operator activated="true" class="generate_nominal_data" compatibility="7.4.000" expanded="true" height="68" name="Generate Nominal Data" width="90" x="45" y="34">
    <parameter key="number_of_attributes" value="1"/>
    <parameter key="number_of_values" value="20"/>
    </operator>
    <operator activated="true" class="generate_nominal_data" compatibility="7.4.000" expanded="true" height="68" name="Generate Nominal Data (2)" width="90" x="45" y="136">
    <parameter key="number_of_attributes" value="1"/>
    <parameter key="number_of_values" value="20"/>
    </operator>
    <operator activated="true" class="replace" compatibility="7.4.000" expanded="true" height="82" name="Replace" width="90" x="179" y="136">
    <parameter key="replace_what" value="value"/>
    <parameter key="replace_by" value="hello"/>
    </operator>
    <operator activated="true" class="append" compatibility="7.4.000" expanded="true" height="103" name="Append" width="90" x="380" y="34"/>
    <connect from_op="Generate Nominal Data" from_port="output" to_op="Append" to_port="example set 1"/>
    <connect from_op="Generate Nominal Data (2)" from_port="output" to_op="Replace" to_port="example set input"/>
    <connect from_op="Replace" from_port="example set output" to_op="Append" to_port="example set 2"/>
    <connect from_op="Append" from_port="merged set" 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="7.4.000" expanded="true" height="82" name="Generate Attributes" width="90" x="447" y="85">
    <list key="function_descriptions">
    <parameter key="att1" value="replaceAll(att1,&quot;hello[0-9]*&quot;,&quot;Mapping1&quot;)"/>
    <parameter key="att1" value="replaceAll(att1,&quot;value[0-5]$&quot;,&quot;Mapping2&quot;)"/>
    <parameter key="att1" value="replaceAll(att1,&quot;value[0-9]*&quot;,&quot;Mapping3&quot;)"/>
    </list>
    <description align="center" color="transparent" colored="false" width="126">It is possible to generate the mappings with a single line of RegEx, but I wanted to demonstrate how to use the GenerateAttributes as a chain for easier maintenance</description>
    </operator>
    <operator activated="true" class="map" compatibility="7.4.000" expanded="true" height="82" name="Map" width="90" x="715" y="187">
    <list key="value_mappings">
    <parameter key="hello[0-9]*" value="Mapping1"/>
    <parameter key="value[0-5]$" value="Mapping2"/>
    <parameter key="value[0-9]*" value="Mapping3"/>
    </list>
    <parameter key="consider_regular_expressions" value="true"/>
    <description align="center" color="transparent" colored="false" width="126">And you can use regular expressions in the map operator too!</description>
    </operator>
    <connect from_op="Generate Pretend 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"/>
    <connect from_op="Generate Attributes" from_port="original" to_op="Map" to_port="example set input"/>
    <connect from_op="Map" from_port="example set output" to_port="result 2"/>
    <portSpacing port="source_input 1" spacing="0"/>
    <portSpacing port="sink_result 1" spacing="0"/>
    <portSpacing port="sink_result 2" spacing="147"/>
    <portSpacing port="sink_result 3" spacing="0"/>
    </process>
    </operator>
    </process>
Sign In or Register to comment.