Tuesday 23 April 2013

Integrate Rapidminer wtih java application

The easiest way is to create a process in Rapidminer and use this created process in your java appilcation.
You can run this process by simply creating a instance of Process class and pass the instance of class File containing name of file as argument to the constructor of Process.


Below is the java code to run process from rapidminer to perform the clustering (K-means).

Input:-----     Excel file containing dataset(numerical).

Output----- Clustered Output labelled with cluster_0, cluster_1, cluster_2.............




import com.rapidminer.RapidMiner;
import com.rapidminer.Process;
import com.rapidminer.example.Attribute;
import com.rapidminer.example.Example;
import com.rapidminer.example.ExampleSet;
import com.rapidminer.operator.IOContainer;
import com.rapidminer.operator.IOObject;
import com.rapidminer.operator.Operator;
import com.rapidminer.operator.OperatorException;



import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import com.rapidminer.operator.io.ExcelExampleSource;
import com.rapidminer.repository.IOObjectEntry;
import com.rapidminer.repository.ProcessEntry;
import com.rapidminer.repository.RepositoryLocation;
import com.rapidminer.tools.XMLException;

public class Model {

     public static void main(String args[]) throws Exception {
        
    // this initializes RapidMiner with your repositories available
        
    RapidMiner.setExecutionMode(RapidMiner.ExecutionMode.COMMAND_LINE);
  
    RapidMiner.init();
  
    // loads the process from the repository
    Process location = new Process(new File("C:\\Documents and Settings\\MNNIT\\.RapidMiner5\\repositories\\Local Repository\\processes\\cluster.rmp"));
    //Entry entry = location.locateEntry();
    //if (entry instanceof ProcessEntry) {
        //Process process = new RepositoryProcessLocation(location).load(null);
    Operator op = location.getOperator("Read Excel");
    op.setParameter(ExcelExampleSource.PARAMETER_EXCEL_FILE, "E:\\Amit_Agrawal\\thesis\\paper\\anomaly in text\\blog\\data\\component.xls");
    //Operator outp = location.getOperator("Write Excel");
   //outp.setParameter(ExcelExampleSource.PARAMETER_EXCEL_FILE, "E:\\Amit_Agrawal\\thesis\\paper\\anomaly in text\\blog\\data\\abcd.xls");
  
    IOContainer ioResult = location.run();
        IOObject result = ioResult.getElementAt(0);
    // use the result(s) as needed, for example if your process just returns one ExampleSet, use this:
    if (ioResult.getElementAt(0) instanceof ExampleSet) {
        ExampleSet resultSet = (ExampleSet)ioResult.getElementAt(0);
        System.out.println(result);
        int i=0;
        for (Example example : resultSet) {
             Iterator<Attribute> allAtts = example.getAttributes().allAttributes();
           
           
             while(allAtts.hasNext()) {
                 Attribute a = allAtts.next();
               if(i<=3)
                 System.out.print(a.getName()+ "  ");
              
                 i++;
             }
        }
        System.out.println("\n");
     for (Example example : resultSet) {
         Iterator<Attribute> allAtts = example.getAttributes().allAttributes();
       
       
         while(allAtts.hasNext()) {
             Attribute a = allAtts.next();
           
                     if (a.isNumerical()) {
                             double value = example.getValue(a);
                             System.out.print(value+ " " );
                             //System.out.println("\n");

                     } else {
                             String value = example.getValueAsString(a);
                             System.out.print(value+ " ");
                             //System.out.println("\n");
                     }
              }
         System.out.println("\n");
  

  
   }
    
}
  
  
     }
}

for more information click here



1 comment:

  1. what packages do I need to add in my project library to run rapid miner ?

    ReplyDelete