Skip to content

Instantly share code, notes, and snippets.

@dacamo76
Last active July 8, 2017 20:22
Show Gist options
  • Save dacamo76/1138546 to your computer and use it in GitHub Desktop.
Save dacamo76/1138546 to your computer and use it in GitHub Desktop.
Working example how to create an ExampleSet in RapidMiner 5.1
package com.dacamo76.librapidminer;
import com.rapidminer.example.Attribute;
import com.rapidminer.example.table.DataRow;
import com.rapidminer.example.table.DataRowFactory;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterables.toArray;
public abstract class DataRowFactory2 {
public abstract DataRow createRow(Iterable<String> inputRow);
public static DataRowFactory2 withFullStopDecimalSeparator(Iterable<Attribute> attributes) {
checkNotNull(attributes);
DataRowFactory factory = new DataRowFactory(DataRowFactory.TYPE_DOUBLE_ARRAY, '.');
return new DataRowFactoryImpl(attributes, factory);
}
public static DataRowFactory2 withCommaDecimalSeparator(Iterable<Attribute> attributes) {
checkNotNull(attributes);
DataRowFactory factory = new DataRowFactory(DataRowFactory.TYPE_DOUBLE_ARRAY, ',');
return new DataRowFactoryImpl(attributes, factory);
}
private DataRowFactory2() {}
private static final class DataRowFactoryImpl extends DataRowFactory2 {
private final DataRowFactory factory;
private final Attribute[] attributes;
DataRowFactoryImpl(Iterable<Attribute> attributes, DataRowFactory factory){
assert factory != null;
assert attributes != null;
this.attributes = toArray(attributes, Attribute.class);
this.factory = factory;
}
public DataRow createRow(Iterable<String> inputRow) {
checkNotNull(inputRow);
String[] strings = toArray(inputRow, String.class);
return factory.create(strings, attributes);
}
}
}
Copyright (c) 2011, 2012, 2013, 2014 Daniel Alberto Canas
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
SimpleExampleSet:
5 examples,
6 regular attributes,
special attributes = {
id = #0: teamID (nominal/single_value)/values=[team_0, team_1, team_2, team_3, team_4]
}
package com.dacamo76.librapidminer;
public interface Row extends Iterable<String> {
}
package com.dacamo76.librapidminer;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.rapidminer.example.Attribute;
import com.rapidminer.example.Attributes;
import com.rapidminer.example.ExampleSet;
import com.rapidminer.example.table.AttributeFactory;
import com.rapidminer.example.table.DataRow;
import com.rapidminer.example.table.ExampleTable;
import com.rapidminer.example.table.MemoryExampleTable;
import com.rapidminer.tools.Ontology;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class UsageExample {
public static void main(String[] args) {
List<Attribute> attributes = createAttributes();
Map<Attribute, String> roles = createRoles(attributes);
ExampleTable table = createExampleTable(attributes, inputRows());
ExampleSet es = table.createExampleSet(roles);
System.out.println(es);
}
static ExampleTable createExampleTable(List<Attribute> attributes, Iterable<Row> inputRows) {
MemoryExampleTable table = new MemoryExampleTable(attributes);
DataRowFactory2 factory = DataRowFactory2.withFullStopDecimalSeparator(attributes);
for(Row row : inputRows){
DataRow dataRow = factory.createRow(row);
table.addDataRow(dataRow);
}
return table;
}
/* ***********************
Static setup methods
* ***********************/
static List<Attribute> createAttributes() {
return ImmutableList.of(
AttributeFactory.createAttribute("teamID", Ontology.NOMINAL),
AttributeFactory.createAttribute("size", Ontology.INTEGER),
AttributeFactory.createAttribute("leader", Ontology.NOMINAL),
AttributeFactory.createAttribute("number of qualified employees", Ontology.INTEGER),
AttributeFactory.createAttribute("leader changed", Ontology.BINOMINAL),
AttributeFactory.createAttribute("average years of experience", Ontology.INTEGER),
AttributeFactory.createAttribute("structure", Ontology.BINOMINAL));
}
static List<String> inputData() {
return ImmutableList.of(
"team_0, 5, Mr. Miller, 4, no, 9, flat",
"team_1, 19, Mrs. Green, 3, yes, 8, flat",
"team_2, 16, Mrs. Hansc, 2, no, 3, flat",
"team_3, 9, Mr. Chang, 6, yes, 3, flat",
"team_4, 17, Mr. Chang, 5, yes, 1, hierarchical");
}
static List<Row> inputRows(){
ImmutableList.Builder<Row> data = ImmutableList.builder();
for(String line: inputData()){
data.add(newRow(line));
}
return data.build();
}
static Row newRow(final String line){
return new Row() {
public Iterator<String> iterator() {
return Splitter.on(',').split(line).iterator();
}
};
}
static Map<Attribute, String> createRoles(List<Attribute> attributes){
return ImmutableMap.of(attributes.get(0), Attributes.ID_NAME);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment