Removing attributes with script fails on second attribute
Dear all,
Could anyone give my a hint please why this script fails? I works fine to remove the first attribute (having "break" loop active) but on the second one it throws an error. No idea why...
Best regards
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="series:windowing" compatibility="7.4.000" expanded="true" height="82" name="Windowing" width="90" x="179" y="34">
<parameter key="window_size" value="5"/>
</operator>
<operator activated="true" class="execute_script" compatibility="7.5.001" expanded="true" height="82" name="Execute Script (2)" width="90" x="313" y="34">
<parameter key="script" value="import com.rapidminer.tools.Ontology; ExampleSet inputTable = input[0]; for ( Attribute att : inputTable.getAttributes() ) { 		inputTable.getAttributes().remove(att); //		break; } return inputTable;"/>
</operator>
<connect from_op="Generate Data" from_port="output" to_op="Windowing" to_port="example set input"/>
<connect from_op="Windowing" from_port="example set output" to_op="Execute Script (2)" to_port="input 1"/>
<connect from_op="Execute Script (2)" 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>
Answers
-
Hi Sachs,
it does not fail on the second attribute. It fails at the end of the execution when it tries to get the next attribute but the ExampleSet is empty.
I.e. you need to adapt your for condition.
Nevertheless, are there any restrictions why you do not use the Select Attributes Operator in the first place?
With RegEx as select criteria can do very powerful stuff
Best,
Edin
0 -
Hi Edin,
Thank you for taking the time to help me with this issue.
The reason why I do not use the select attributes operator is that my posted script is just a simplified excerpt of the whole script.
Actually, I have a group of attributes with the same prefix and want to remove all but the first occurance.
(Example: att1-0, att1-1, att1-2, att2-0, att2-1, att2-2 --> keep only att1-0 and att2-0 because they are the first occurance of this prefix.)
Referring to the script: I also had this error when trying to remove not all but just a few attributes. My original script looked like this:import com.rapidminer.tools.Ontology;
import java.util.ArrayList;
ExampleSet inputTable = input[0];
ArrayList<String> name_list = new ArrayList<String>();
for ( Attribute att : inputTable.getAttributes() ) // For each attribute "att" in example set do
{
String name = att.getName().substring( 1, att.getName().indexOf('-') ); // Get prefix of "att"
int flag = 0;
for ( int i = 0 ; i < name_list.size() ; i++ ) // Compare if this prefix is already in list
{
if ( name_list[i] == name ) // If prefix found in list, then set flag to 1 and break loop
{
flag = 1;
break;
}
}
if ( flag == 0 ) // If name was not found then add name to list
name_list.add(name);
else // If name was found then remove "att" from example set
inputTable.getAttributes().remove(att);
}
return inputTable;
Best
Sachs0 -
Hi,
Actually, I have a group of attributes with the same prefix and want to remove all but the first occurance.
(Example: att1-0, att1-1, att1-2, att2-0, att2-1, att2-2 --> keep only att1-0 and att2-0 because they are the first occurance of this prefix.)set the one to a special role and use Select Attribute with regular expression?
~Martin
0 -
Hi Martin,
Thank you very much for your feedback as well. The thing is that I have multiple prefixes. So I would have to loop through them. While this would still be possible the next hurdle is that the prefixes change all the time depending on my input data. This is why I tried to get a solution using a script.
I tested another version and it still seems to me that the error is related to the second removal and not that the example set is empty at the end.
In this version I introduced i to make sure that the removal is only processed two times max. Still the error occurs...
import com.rapidminer.tools.Ontology;
ExampleSet inputTable = input[0];
int i = 0;
for ( Attribute att : inputTable.getAttributes() )
{
inputTable.getAttributes().remove(att);
i++;
if ( i == 2 )
break;
}
return inputTable;Looking forward to any suggestions...
Best
Sachs
0