The sample code below shows how to use the MLP regression to predict the (-1, +1) numerical output of the heart-scale sample from libsvm.
The training data is loaded from a data frame connected to the "heart_scale" libsvm file (please refer to here for more example on how to create a data frame).
The sample code below shows how to use the MLP classifier to predict the labels of the heart-scale sample from libsvm.
The training data is loaded from a data frame connected to the "heart_scale" libsvm file (please refer to here for more example on how to create a data frame).
As the heart-scale data frame has (-1, +1) numerical output, the codes first coverts the (-1, +1) as categorical output label "category-label".
InputStream inputStream =newFileInputStream("heart_scale");
DataFrame dataFrame =DataQuery.libsvm().from(inputStream).build();
dataFrame.unlock();
for(int i=0; i < dataFrame.rowCount(); ++i){
DataRow row = dataFrame.row(i);
row.setCategoricalTargetCell("category-label", ""+ row.target());
}
dataFrame.lock();
MLPClassifier mlpClassifier =newMLPClassifier();
mlpClassifier.setHiddenLayers(6); // one hidden layer, to set two or more hidden layer call mlpClassifier.setHiddenLayer([layer1NeuronCunt], [layer2NeuronCunt], ...);
mlpClassifier.setEpoches(1000);
mlpClassifier.setLearningRate(0.2);
mlpClassifier.fit(dataFrame);
int correctnessCount =0;
for(int i =0; i < dataFrame.rowCount(); ++i){
DataRow row = dataFrame.row(i);
String predicted_label = mlpClassifier.classify(row);
correctnessCount += (predicted_label.equals(row.categoricalTarget()) ?1:0);
if(i <10) {
System.out.println(row);
System.out.println("predicted: "+ predicted_label +"\tactual: "+ row.categoricalTarget());
}
}
System.out.println("Prediction Accuracy: "+(correctnessCount *100/ dataFrame.rowCount()));
This package provides java implementation of various genetic programming paradigms such as linear genetic programming, tree genetic programming, gene expression programming, etc
The best program in the LGP population obtained from the training in the above step can then be used for prediction, as shown by the sample code below:
Program program = pop.getGlobalBestProgram();
logger.info("global: {}", program);
for(Observation observation : testingData) {
program.execute(observation);
double predicted = observation.getPredictedOutput(0);
double actual = observation.getOutput(0);
logger.info("predicted: {}\tactual: {}", predicted, actual);
}
Usage of Tree Genetic Programming
Here we will use the "Mexican Hat" symbolic regression introduced earlier to
Create and train the TreeGP
The sample code below shows how the TreeGP can be created and trained:
importcom.github.chen0040.gp.treegp.TreeGP;
importcom.github.chen0040.gp.commons.BasicObservation;
importcom.github.chen0040.gp.commons.Observation;
importcom.github.chen0040.gp.treegp.gp.Population;
importcom.github.chen0040.gp.treegp.program.operators.*;
TreeGP tgp =newTreeGP();
tgp.getOperatorSet().addAll(newPlus(), newMinus(), newDivide(), newMultiply(), newPower());
tgp.getOperatorSet().addIfLessThanOperator();
tgp.addConstants(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0);
tgp.setVariableCount(2); // equal to the number of input parameter in an observation
tgp.getObservations().addAll(trainingData);
tgp.setCostEvaluator((program, observations)->{
double error =0;
for(Observation observation : observations){
program.execute(observation);
error +=Math.pow(observation.getOutput(0) - observation.getPredictedOutput(0), 2.0);
}
return error;
});
long startTime =System.currentTimeMillis();
Population pop = tgp.newPopulation();
pop.initialize();
while (!pop.isTerminated())
{
pop.evolve();
logger.info("Mexican Hat Symbolic Regression Generation: {}, elapsed: {} seconds", pop.getCurrentGeneration(), (System.currentTimeMillis() - startTime) /1000);
logger.info("Global Cost: {}\tCurrent Cost: {}", pop.getGlobalBestProgram().getCost(), pop.getCostInCurrentGeneration());
}
logger.info("best solution found: {}", pop.getGlobalBestSolution().mathExpression());
The last line prints the TreeGP program found by the TreeGP evolution, a sample of which is shown below:
Test the program obtained from the TreeGP evolution
The best program in the TreeGP population obtained from the training in the above step can then be used for prediction, as shown by the sample code below:
The sample code belows takes the "result" variable from the above code and list the top 3 relevant topics of each document (which is one of the items in the "docs" list variable).