Hi I have a problem with the CSVExampelSource operator. I want to read a CSVDatei and add them to the DecisionTreeLearner.
How can I add my CSVExampelSource operator attributes and label?
This is my testdata:
Play;Outlook;Temperature;Humidity;Wind
yes;sunny;85;85.0;false
yes;overcast;80;90.0;true
no;overcast;83;78.0;false
no;rain;70;96.0;false
yes;rain;68;80.0;true
no;rain;65;70.0;true
yes;overcast;64;65.0;true
no;sunny;72;95.0;false
yes;sunny;69;70.0;false
no;sunny;75;80.0;false
yes;sunny;68;70.0;true
no;overcast;72;90.0;true
yes;overcast;81;75.0;true
no;rain;71;80.0;true
My code to test:
import java.io.File;
import java.util.LinkedList;
import java.util.List;
import com.rapidminer.Process;
import com.rapidminer.RapidMiner;
import com.rapidminer.RapidMiner.ExecutionMode;
import com.rapidminer.example.Attribute;
import com.rapidminer.example.Attributes;
import com.rapidminer.example.table.AttributeFactory;
import com.rapidminer.operator.IOContainer;
import com.rapidminer.operator.OperatorException;
import com.rapidminer.operator.learner.tree.DecisionTreeLearner;
import com.rapidminer.operator.nio.CSVExampleSource;
import com.rapidminer.operator.preprocessing.filter.ChangeAttributeRole;
import com.rapidminer.tools.Ontology;
import com.rapidminer.tools.OperatorService;
import de.tu_berlin.mf.vlcu.utilityclasses.CSVReader;
public class ProcessCreator {
public static Process createProcess() {
// invoke init before using the OperatorService
RapidMiner.setExecutionMode(ExecutionMode.EMBEDDED_WITH_UI);
RapidMiner.init();
String dateipath1 = "./HLB_B3_Messung/Data_Controller/messung1/Testdaten.csv";
File file = new File(dateipath1);
CSVReader csvreader = new CSVReader(file.getAbsolutePath());
csvreader.readCSV();
String[] header = csvreader.getHeader();
String[][] data = csvreader.getData();
Process process = null;
try {
// create attribute list
List<Attribute> attributes = new LinkedList<Attribute>();
for (int a = 0; a < header.length; a++) {
if (header.equals("Play")) {
Attribute label = AttributeFactory.createAttribute("label", Ontology.NOMINAL);
attributes.add(label);
} else {
attributes.add(AttributeFactory.createAttribute("att" + a, Ontology.REAL));
}
}
// create process
process = new Process();
/* Reading Data */
CSVExampleSource csvdata = OperatorService.createOperator(CSVExampleSource.class);
// set parameters
csvdata.setParameter(CSVExampleSource.PARAMETER_CSV_FILE, file.getAbsolutePath());
csvdata.setParameter(CSVExampleSource.PARAMETER_FIRST_ROW_AS_NAMES, "true");
csvdata.setParameter(CSVExampleSource.PARAMETER_COLUMN_SEPARATORS, ";");
csvdata.setParameter(CSVExampleSource.PARAMETER_TRIM_LINES, "true");
ChangeAttributeRole changerole = OperatorService.createOperator(ChangeAttributeRole.class);
changerole.setParameter(ChangeAttributeRole.PARAMETER_NAME, "Play");
changerole.setParameter(ChangeAttributeRole.PARAMETER_TARGET_ROLE, Attributes.LABEL_NAME);
DecisionTreeLearner decisionTree = OperatorService.createOperator(DecisionTreeLearner.class);
decisionTree.setParameter(DecisionTreeLearner.PARAMETER_CRITERION, "gain_ratio");
decisionTree.setParameter(DecisionTreeLearner.PARAMETER_MINIMAL_SIZE_FOR_SPLIT, "4");
decisionTree.setParameter(DecisionTreeLearner.PARAMETER_MINIMAL_LEAF_SIZE, "2");
decisionTree.setParameter(DecisionTreeLearner.PARAMETER_MAXIMAL_DEPTH, "20");
decisionTree.setParameter(DecisionTreeLearner.PARAMETER_CONFIDENCE, "0.25");
process.getRootOperator().getSubprocess(0).addOperator(csvdata);
process.getRootOperator().getSubprocess(0).addOperator(changerole);
process.getRootOperator().getSubprocess(0).addOperator(decisionTree);
csvdata.getOutputPorts().getPortByName("output")
.connectTo(changerole.getInputPorts().getPortByName("example set input"));
changerole.getOutputPorts().getPortByName("example set output")
.connectTo(decisionTree.getInputPorts().getPortByName("training set"));
// add other operators and set parameters
// [...]
} catch (Exception e) {
e.printStackTrace();
}
return process;
}
public static void main(String[] argv) {
// create process
Process process = createProcess();
// print process setup
System.out.println(process.getRootOperator().createProcessTree(0));
try {
// perform process
IOContainer test = process.run();
System.out.println(test.toString());
// to run the process with input created by your application use
// process.run(new IOContainer(new IOObject[] { ... your objects ... });
} catch (OperatorException e) {
e.printStackTrace();
}
}
}
and this is the output:
Process[0] (Process)
subprocess 'Main Process'
+- Read CSV[0] (Read CSV)
+- Set Role[0] (Set Role)
+- Decision Tree[0] (Decision Tree)
IOContainer (0 objects):
How can I put information if nominal, binominal, real and integer to the CSV operator ....
How can I add to CSVExampelSource Metadata? that the Decision Tree is created by Rapidmineroperator over the label play.
I need urgent help, it's about my Bachelor
And prepare the prozess in rapidminer is not a alternative!!
Sorry for my bad english!!
MFG