The Altair Community is migrating to a new platform to provide a better experience for you. In preparation for the migration, the Altair Community is on read-only mode from October 28 - November 6, 2024. Technical support via cases will continue to work as is. For any urgent requests from Students/Faculty members, please submit the form linked here

How to incapsulate data in IOContainer

lenoleno Member Posts: 13 Contributor II
edited November 2018 in Help
Hi i have extract a model file from a process.
I want load the model and apply this model to data that i calculate a runtime.
The data are all integer value.
My code is

        RapidMiner.init();
    //create the operator for apply model
      Operator testSource = OperatorService.createOperator(ModelApplier.class);
        //create the operator for load the model
      Operator loadModel= OperatorService.createOperator(ModelLoader.class);

then i have to load the model and this is not a problem.
My problem is
" How i can incapsulate a set of number in an IOContainer for using this as input of the Model?"

Thank you very much
Tagged:

Answers

  • Marco_BoeckMarco_Boeck Administrator, Moderator, Employee-RapidMiner, Member, University Professor Posts: 1,996 RM Engineering
    Hi,

    just set up your ExampleSet and put it in the IOContainer. See the following examples.

    Create ExampleSet:

    List<Attribute> listOfAttributes;
    listOfAttributes.add(AttributeFactory.createAttribute("helloWorldAttribute", Ontology.NUMERICAL);
    // setup your attribute list
    MemoryExampleTable table = new MemoryExampleTable(listOfAttributes);
    // setup your data rows
    double[] doubleArray = new double[] { 1.0d, 2.0d, 3.0d };
    table.addDataRow(new DoubleArrayDataRow(doubleArray));
    ExampleSet exSet = table.createExampleSet();
    // if you need special roles:
    Attribute att = listOfAttributes.get(0);
    exSet.getAttributes().setSpecialAttribute(att, "label");
    Put ExampleSet into IOContainer:

    IOContainer ioInput = new IOContainer(new IOObject[]{ exSet });
    IOContainer ioResult = process.run(ioInput);
    ExampleSet resultSet;
    if (ioResult.getElementAt(0) instanceof ExampleSet) {
    resultSet = (ExampleSet)ioResult.getElementAt(0);
           // do stuff
    }
    Just make sure to use the correct input order in your process design.

    Regards,
    Marco
  • lenoleno Member Posts: 13 Contributor II
    But the resultSet contain only the predicted label? And another doubt but if the apply method of the class ModelApplier is deprecated, How i can apply a model that i have in a file?
    I have
            RapidMiner.init();
            // loading the test set (plus adding the model to result container)
            Operator testSource = OperatorService.createOperator(ModelApplier.class);
            Operator loadModel= OperatorService.createOperator(ModelLoader.class);
           
          File modelFile=new File("neuralX.mod");
          loadModel.setParameter(ModelLoader.PARAMETER_MODEL_FILE, modelFile.getAbsolutePath());
         
    Some idea? Very much thanks for the help
  • Marco_BoeckMarco_Boeck Administrator, Moderator, Employee-RapidMiner, Member, University Professor Posts: 1,996 RM Engineering
    Hi,

    I strongly suggest using RapidMiner to design your process(es) beforehand and then using them via the code I posted in my first response. You can connect the input ports on the left border of the procecss design window to your operators, and then deliver the data at these ports at runtime to your process via:

    IOContainer ioInput = new IOContainer(new IOObject[]{ exSet });
    IOContainer ioResult = process.run(ioInput);
    This way, you can do anything you can do from RapidMiner, so the result(s) from the process are the same as in RapidMiner. Trying to create the process "by hand" and not using a process xml is very, very error-prone (and a lot more work).

    Regards,
    Marco
  • lenoleno Member Posts: 13 Contributor II
    Ok more problem is done, now i have to extract the predicted label by the ExampleSet i do
    resultSet = (ExampleSet)ioResult.getElementAt(0);
                System.out.println("resultset");
                Example e=resultSet.getExample(0);
                predetto=e.getPredictedLabel();
                System.out.println("predetto="+predetto);
     

    It is correct? And then the process.run is in a while loop , have i to destroy the table and the example map at every iterate? Thank you very much Marco i have problem to understand the index that i insert in the call of function.

    Happy data miner :)
  • Marco_BoeckMarco_Boeck Administrator, Moderator, Employee-RapidMiner, Member, University Professor Posts: 1,996 RM Engineering
    Hi,

    I suggest using something along these lines:

    // change attribute name to whatever you need
    Attribute targetAtt = exampleSet.getAttributes().get("attributeName");
    double result;
    for (Example example: exampleSet) {
    result = example.getValue(targetAtt);
            // or if nominal attribute
          String resultString = example.getValueAsString(targetAtt);
          // insert custom code here
    }
    This will iterate over all examples of the specified attribute.

    If you want to run your process execution in a while loop you can do that, there is no need to explicitly destroy something.

    Regards,
    Marco
  • lenoleno Member Posts: 13 Contributor II
    But i need to get the last resultSet and only the predicted Label not an attribute, this is my doubt.
    Then i have to use getPredictedLabel() not getAttribute right? Then is there an order of the ExampleSet? The last example Set is the first of the table?
    Finally a question if i have two output port for my process what i have to do for get a specific port?

    You are very nice thank you very much and i'm sorry for the great number of question that i do
  • Marco_BoeckMarco_Boeck Administrator, Moderator, Employee-RapidMiner, Member, University Professor Posts: 1,996 RM Engineering
    Hi,

    1) Predictions are also attributes. They have a special role, but my code above should work anyway.
    2) The order you get when iterating over the example set as indicated above should match the order displayed in RapidMiner.
    3) ioResult.getElementAt(0) will get you the first output IOObject, ioResult.getElementAt(1) the second, and so on.

    Regards,
    Marco
  • lenoleno Member Posts: 13 Contributor II
    ioResult.getElementAt(0) is the first output port ok very well.
    My problem is that i have to insert a new row in the data table and create an exampleSet of only this new row data, maybe i don't have understand something.
  • lenoleno Member Posts: 13 Contributor II
    Ok i have done

      table.addDataRow(new DoubleArrayDataRow(doubleArray));
           exSet =table.createExampleSet();
           ioInput = new IOContainer(new IOObject[]{ exSet });
           ioResult = process.run(ioInput);

           if (ioResult.getElementAt(0) instanceof ExampleSet) {
               resultSet = (ExampleSet)ioResult.getElementAt(0);
               e =resultSet.getExample(0);
               predetto=e.getPredictedLabel();
               System.out.println("predettoX="+predetto);
           }
           if (ioResult.getElementAt(1) instanceof ExampleSet) {
               resultSet = (ExampleSet)ioResult.getElementAt(1);
               e =resultSet.getExample(0);
               predetto=e.getPredictedLabel();
               System.out.println("predettoY="+predetto);
           }
         

               Attribute attribute = table.getAttribute(table.getNumberOfAttributes()-1);

              System.out.println("Nome del 11 "+attribute.getName());
           

              table.removeAttribute(table.getNumberOfAttributes()-1);

              attribute = table.getAttribute(table.getNumberOfAttributes()-2);

              System.out.println("Nome del 12 "+attribute.getName());


              table.removeAttribute(table.getNumberOfAttributes()-2);

           removeDataRow = table.removeDataRow(0);
           table.clear();
    It play and no problem of major number of attribute i have,  but if i print the numer of Attributes it say 12, but must be 10.
    Everyone have an idea?
    My goal is at every loop i have to give it a new exampleSet, run my process that give me two ResultSet, and then prepare for the next iterate(then eliminate the prediction attribute that Rapidminer add to my exampleSet).

    Thank you very much
Sign In or Register to comment.