Getting the type of Attribute

seshadotcomseshadotcom Member Posts: 33 Contributor II
edited November 2018 in Help
Hi Guys,

I want to write a generic workflow which can discover the type of attributes and change them accordingly on a case by case basis so that I deliver one big chunk of dataset of binomial values to my FP Growth. I did see an operator called GuessTypes but how do I get the related type of an attribute? It seems not to be delivering this in the output port.

Any Idea?

Answers

  • MariusHelfMariusHelf RapidMiner Certified Expert, Member Posts: 1,869 Unicorn
    That is not possible out of the box. You can probably get the types via the scripting interface in the Execute Script operator.

    Best regards,
    Marius
  • Marco_BoeckMarco_Boeck Administrator, Moderator, Employee, Member, University Professor Posts: 1,993 RM Engineering
    Hi,

    you can use the "Execute Script" operator for that. I created an example process for you which you can extend and modifiy. For further information on how to convert various attributes, have a look at the RM sourcecode of the Type Conversion operators like com.rapidminer.operator.preprocessing.filter.NominalToBinominal.

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <process version="5.3.009">
     <context>
       <input/>
       <output/>
       <macros/>
     </context>
     <operator activated="true" class="process" compatibility="5.3.009" expanded="true" name="Process">
       <process expanded="true">
         <operator activated="true" class="generate_data" compatibility="5.3.009" expanded="true" height="60" name="Generate Data" width="90" x="45" y="30">
           <parameter key="number_examples" value="1"/>
           <parameter key="number_of_attributes" value="1"/>
         </operator>
         <operator activated="true" class="execute_script" compatibility="5.3.009" expanded="true" height="76" name="Execute Script" width="90" x="179" y="30">
           <parameter key="script" value="import java.util.Map;&#10;import java.util.Map.Entry;&#10;import java.util.Iterator;&#10;import java.util.LinkedHashMap;&#10;&#10;import com.rapidminer.example.Attribute;&#10;import com.rapidminer.example.Example;&#10;import com.rapidminer.example.ExampleSet;&#10;import com.rapidminer.example.table.AttributeFactory;&#10;import com.rapidminer.tools.Ontology;&#10;&#10;ExampleSet exampleSet = input[0];&#10;Map&lt;Attribute, Attribute&gt; translationMap = new LinkedHashMap&lt;Attribute, Attribute&gt;();&#10;Iterator&lt;Attribute&gt; iterator = exampleSet.getAttributes().iterator();&#10;while (iterator.hasNext()) {&#10;&#9;Attribute oldAtt = iterator.next();&#10;&#9;if (Ontology.ATTRIBUTE_VALUE_TYPE.isA(oldAtt.getValueType(), Ontology.NUMERICAL)) {&#10;&#9;&#9;Attribute newAtt = AttributeFactory.createAttribute(Ontology.BINOMINAL);&#10;&#9;&#9;translationMap.put(oldAtt, newAtt);&#10;&#9;}&#10;&#9;// your other conditions here&#10;}&#10;// adding to table and exampleSet&#10;for (Entry&lt;Attribute, Attribute&gt; replacement: translationMap.entrySet()) {&#10;&#9;Attribute newAttribute = replacement.getValue();&#10;&#9;exampleSet.getExampleTable().addAttribute(newAttribute);&#10;&#9;exampleSet.getAttributes().addRegular(newAttribute);&#10;}&#10;&#9;&#9;&#10;// over all examples change attribute values&#10;for(Example example : exampleSet) {&#10;&#9;for(Entry&lt;Attribute, Attribute&gt; replacement: translationMap.entrySet()) {&#10;&#9;&#9;Attribute oldAttribute = replacement.getKey();&#10;&#9;&#9;Attribute newAttribute = replacement.getValue();&#10;&#9;&#9;double oldValue = example.getValue(oldAttribute);&#10;&#9;&#9;if (Double.isNaN(oldValue)) {&#10;&#9;&#9;&#9;example.setValue(newAttribute, Double.NaN);&#10;&#9;&#9;} else { // your conditions here&#10;&#9;&#9;&#9;example.setValue(newAttribute, newAttribute.getMapping().mapString(&quot;yourString&quot;));&#10;&#9;&#9;}&#10;&#9;}&#10;}&#10;&#9;&#9;&#10;// removing old attributes&#10;for (Map.Entry&lt;Attribute,Attribute&gt; entry : translationMap.entrySet()) {&#10;&#9;Attribute originalAttribute = entry.getKey();&#10;&#9;exampleSet.getAttributes().remove(originalAttribute);&#10;&#9;entry.getValue().setName(originalAttribute.getName());&#10;}&#10;&#10;return exampleSet;"/>
         </operator>
         <connect from_op="Generate Data" 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>
    Regards,
    Marco
Sign In or Register to comment.