How to Create Example Sets Using Groovy Script


Sometimes you want to create custom data sets which are beyond the options of the Generate Data operator.
One way to do this is to use the Execute Script operator which leverages groovy script. Here is a groovy script to start off with. It generates an example set with two attributes. One is numerical and has different Poissoin distributed values in. The other is nominal and has two different class values in.
import com.rapidminer.example.utils.ExampleSetBuilder;
import com.rapidminer.example.utils.ExampleSets;
import com.rapidminer.tools.Ontology;
import org.apache.commons.math3.distribution.PoissonDistribution;
List<Attribute> attributes = new ArrayList<Attribute>();
attributes.add(AttributeFactory.createAttribute("data",Ontology.REAL));
attributes.add(AttributeFactory.createAttribute("class",Ontology.STRING));
ExampleSetBuilder examplesetBuilder = ExampleSets.from(attributes);
PoissonDistribution pos = new PoissonDistribution(20)
double[] row = new double[2];
for(int i = 0; i < 10000; ++i){
row[0] = pos.sample();
row[1] = attributes.get(1).getMapping().mapString("Class1")
examplesetBuilder.addRow(row)
}
PoissonDistribution pos2 = new PoissonDistribution(40)
for(int i = 0; i < 10000; ++i){
row[0] = pos2.sample();
row[1] = attributes.get(1).getMapping().mapString("Class2")
examplesetBuilder.addRow(row)
}
// This line returns the first input as the first output
return examplesetBuilder.build();
- Sr. Director Data Solutions, Altair RapidMiner -
Dortmund, Germany
Dortmund, Germany
Tagged:
3