Options

Sort attributes by weight

qwertz2qwertz2 Member Posts: 49 Guru
edited November 2018 in Help

 

Dear all,

 

Basically I already found a workaround to my question which I would like to share with all of you as someone may have the same requirement... I was looking for a way to order attributes by weight. However, if you are aware of an easier way I would be happy to learn...

 

Best

Sachs

 

<?xml version="1.0" encoding="UTF-8"?><process version="7.5.001">
<context>
<input/>
<output/>
<macros/>
</context>
<operator activated="true" class="process" compatibility="7.5.001" expanded="true" name="Process">
<process expanded="true">
<operator activated="true" class="generate_data" compatibility="7.5.001" expanded="true" height="68" name="Generate Data" width="90" x="45" y="34"/>
<operator activated="true" class="weight_by_correlation" compatibility="7.5.001" expanded="true" height="82" name="Weight by Correlation" width="90" x="179" y="34"/>
<operator activated="true" class="weights_to_data" compatibility="7.5.001" expanded="true" height="68" name="Weights to Data" width="90" x="313" y="136"/>
<operator activated="true" class="execute_script" compatibility="7.5.001" expanded="true" height="82" name="Execute Script" width="90" x="447" y="136">
<parameter key="script" value="import com.rapidminer.tools.Ontology;&#10;&#10;ExampleSet inputTable = input[ 0 ];&#10;&#10;Attribute att = inputTable.getAttributes().get( &quot;Attribute&quot; );&#10;Attribute[] listOfAtts = new Attribute[ inputTable.size() ];&#10;int i = 0;&#10;&#10;for ( Example exa : inputTable )&#10;{&#10;&#9;String att_name = inputTable.getExample(i).getValueAsString(att);&#9;&#10;&#9;listOfAtts[ i ] = AttributeFactory.createAttribute( att_name , Ontology.NUMERICAL );&#10;&#9;i++;&#10;&#9;operator.logNote(att_name);&#10;}&#10;&#10;MemoryExampleTable table = new MemoryExampleTable(listOfAtts);&#10;&#10;ExampleSet outputTable = table.createExampleSet();&#10;return outputTable;"/>
</operator>
<operator activated="true" class="order_attributes" compatibility="7.5.001" expanded="true" height="82" name="Reorder Attributes" width="90" x="581" y="34">
<parameter key="sort_mode" value="reference data"/>
</operator>
<connect from_op="Generate Data" from_port="output" to_op="Weight by Correlation" to_port="example set"/>
<connect from_op="Weight by Correlation" from_port="weights" to_op="Weights to Data" to_port="attribute weights"/>
<connect from_op="Weight by Correlation" from_port="example set" to_op="Reorder Attributes" to_port="example set input"/>
<connect from_op="Weights to Data" from_port="example set" to_op="Execute Script" to_port="input 1"/>
<connect from_op="Execute Script" from_port="output 1" to_op="Reorder Attributes" to_port="reference_data"/>
<connect from_op="Reorder 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>
Tagged:

Best Answer

  • Options
    MartinLiebigMartinLiebig Administrator, Moderator, Employee, RapidMiner Certified Analyst, RapidMiner Certified Expert, University Professor Posts: 3,507 RM Data Scientist
    Solution Accepted

    Hi,

     

    you can do this w/o a script :) See attached process.

     

    ~Martin

     

    <?xml version="1.0" encoding="UTF-8"?><process version="7.5.001">
    <context>
    <input/>
    <output/>
    <macros/>
    </context>
    <operator activated="true" class="process" compatibility="7.5.001" expanded="true" name="Process">
    <process expanded="true">
    <operator activated="true" class="generate_data" compatibility="7.5.001" expanded="true" height="68" name="Generate Data" width="90" x="45" y="34"/>
    <operator activated="true" class="weight_by_correlation" compatibility="7.5.001" expanded="true" height="82" name="Weight by Correlation" width="90" x="179" y="34"/>
    <operator activated="true" class="weights_to_data" compatibility="7.5.001" expanded="true" height="68" name="Weights to Data" width="90" x="313" y="136"/>
    <operator activated="true" class="sort" compatibility="7.5.001" expanded="true" height="82" name="Sort" width="90" x="447" y="136">
    <parameter key="attribute_name" value="Weight"/>
    </operator>
    <operator activated="true" class="set_role" compatibility="7.5.001" expanded="true" height="82" name="Set Role" width="90" x="581" y="136">
    <parameter key="attribute_name" value="Attribute"/>
    <parameter key="target_role" value="id"/>
    <list key="set_additional_roles"/>
    </operator>
    <operator activated="true" breakpoints="after" class="transpose" compatibility="7.5.001" expanded="true" height="82" name="Transpose" width="90" x="715" y="136"/>
    <operator activated="false" class="execute_script" compatibility="7.5.001" expanded="true" height="68" name="Execute Script" width="90" x="447" y="340">
    <parameter key="script" value="import com.rapidminer.tools.Ontology;&#10;&#10;ExampleSet inputTable = input[ 0 ];&#10;&#10;Attribute att = inputTable.getAttributes().get( &quot;Attribute&quot; );&#10;Attribute[] listOfAtts = new Attribute[ inputTable.size() ];&#10;int i = 0;&#10;&#10;for ( Example exa : inputTable )&#10;{&#10;&#9;String att_name = inputTable.getExample(i).getValueAsString(att);&#9;&#10;&#9;listOfAtts[ i ] = AttributeFactory.createAttribute( att_name , Ontology.NUMERICAL );&#10;&#9;i++;&#10;&#9;operator.logNote(att_name);&#10;}&#10;&#10;MemoryExampleTable table = new MemoryExampleTable(listOfAtts);&#10;&#10;ExampleSet outputTable = table.createExampleSet();&#10;return outputTable;"/>
    </operator>
    <operator activated="true" class="order_attributes" compatibility="7.5.001" expanded="true" height="82" name="Reorder Attributes" width="90" x="849" y="34">
    <parameter key="sort_mode" value="reference data"/>
    </operator>
    <connect from_op="Generate Data" from_port="output" to_op="Weight by Correlation" to_port="example set"/>
    <connect from_op="Weight by Correlation" from_port="weights" to_op="Weights to Data" to_port="attribute weights"/>
    <connect from_op="Weight by Correlation" from_port="example set" to_op="Reorder Attributes" to_port="example set input"/>
    <connect from_op="Weights to Data" from_port="example set" to_op="Sort" to_port="example set input"/>
    <connect from_op="Sort" from_port="example set output" to_op="Set Role" to_port="example set input"/>
    <connect from_op="Set Role" from_port="example set output" to_op="Transpose" to_port="example set input"/>
    <connect from_op="Transpose" from_port="example set output" to_op="Reorder Attributes" to_port="reference_data"/>
    <connect from_op="Reorder 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>
    - Sr. Director Data Solutions, Altair RapidMiner -
    Dortmund, Germany

Answers

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

    Thanks for posting this process. I am curious, though, what is the use case?  Ordinarily I don't care about the specific order of attributes in my datasets.  I can see wanting to filter out attributes below a certain weight, but I am not sure why you would need to put them in any particular order.  Can you clarify why you were looking to do this?

     

    Brian T.
    Lindon Ventures 
    Data Science Consulting from Certified RapidMiner Experts
  • Options
    qwertz2qwertz2 Member Posts: 49 Guru

     

    Dear all,

     

    the use case is that I want to postprocess with "remove correlated attributes". While post processing I want to assure that the attributes with the best weight remains. So it has to be first in the order.

     

    Thanks for sharing the non script process. Amazing what what powerful processes the standard operators enable. If I had known the impact of the set role operator in this scenario I would not have written the script. However, I am glad to have learned new things on Rapidminer :)

     

     

    Cheers

    Sachs

Sign In or Register to comment.