Due to recent updates, all users are required to create an Altair One account to login to the RapidMiner community. Click the Register button to create your account using the same email that you have previously used to login to the RapidMiner community. This will ensure that any previously created content will be synced to your Altair One account. Once you login, you will be asked to provide a username that identifies you to other Community users. Email us at Community with questions.

How to iterate over all the attributes in an ExampleSet

radoneradone RapidMiner Certified Expert, Member Posts: 74 Guru
edited November 2018 in Help
How can I iterate over all attributes in an ExampleSet, including special attributes and label?

public class AttributeTest {
public static void main(String[] args) {
// construct attribute set
Attribute[] attributes = new Attribute[3];
attributes[0] = AttributeFactory.createAttribute(
"FileName", Ontology.STRING);
attributes[1] = AttributeFactory.createAttribute(
"Xposition", Ontology.INTEGER);
attributes[2] = AttributeFactory.createAttribute(
"Yposition", Ontology.INTEGER);

MemoryExampleTable table = new MemoryExampleTable(attributes);
char decimalSeperator = '.';
DataRowFactory ROW_FACTORY = new DataRowFactory(0, decimalSeperator);

String[] strings = new String[3];

strings[0] = "FILE";
strings[1] = Integer.toString(11);
strings[2] = Integer.toString(22);

// make and add row
DataRow row = ROW_FACTORY.create(strings, attributes);
table.addDataRow(row);

ExampleSet es = table.createExampleSet();

es.getAttributes().setSpecialAttribute(attributes[0],
"IGNORED1");
es.getAttributes().setSpecialAttribute(attributes[1],
"IGNORED2");

for (Attribute a : es.getAttributes()) {
System.out.println(a.getName());
}

System.out.println(es.getAttributes().getSpecial("clusterFNATT"));
}
}
With the setSpecial() method I want to mark these attributes to be not used for training.
This code will print only the thirth attribute (which was not made to be special). How can I print also values of the special attributes?

Thanks in advance.
Radim

Tagged:

Answers

  • fischerfischer Member Posts: 439 Maven
    Hi,

    exampleSet.getAttributes().allAttributes();

    returns the iterator you are looking for,

    Cheers,
    Simon
  • radoneradone RapidMiner Certified Expert, Member Posts: 74 Guru
    Thank you again Simon.

    For anyone dealing with the problem, let me summarize the content of this thread:

    To iterate only over regular attributes:
                           for (Attribute a : ef.getAttributes().allAttributes()) {
    Example e = ef.getExample(0);
    values = e.getValueAsString(a);
    i++;
    }
    To iterate over all attributes (including special attributes, label, etc.):
                            Iterator<Attribute> it = ef.getAttributes().allAttributes();
    while(it.hasNext()) {
    Example e = ef.getExample(0);
    values = e.getValueAsString(it.next());
    i++;
    }
    where ef is of type ExampleSet.
Sign In or Register to comment.