use of org.knime.base.node.mine.treeensemble2.model.TreeNodeClassification in project knime-core by knime.
the class RandomForestClassificationTreeNodeWidget method createNodeLabelPanel.
/**
* The panel at the top displaying the node label.
* @param scale
* @return A label, e.g. "Iris-versicolor (45/46)"
*/
private JPanel createNodeLabelPanel(final float scale) {
int gap = Math.round(5 * scale);
JPanel p = new JPanel(new FlowLayout(FlowLayout.CENTER, gap, gap));
StringBuilder label = new StringBuilder();
TreeNodeClassification node = (TreeNodeClassification) getUserObject();
String majorityClass = node.getMajorityClassName();
final float[] targetDistribution = node.getTargetDistribution();
double majorityClassCount = targetDistribution[node.getMajorityClassIndex()];
boolean displayTargetDistribution = false;
double totalClassCount = 0;
for (double classCount : targetDistribution) {
totalClassCount += classCount;
if (!displayTargetDistribution && classCount != 0.0 && classCount != 1.0) {
displayTargetDistribution = true;
}
}
label.append(majorityClass);
// display target distribution only if available (not 1/1)
if (displayTargetDistribution) {
label.append(" (").append(convertCount(majorityClassCount));
label.append("/").append(convertCount(totalClassCount));
label.append(")");
}
p.add(scaledLabel(label.toString(), scale));
return p;
}
use of org.knime.base.node.mine.treeensemble2.model.TreeNodeClassification in project knime-core by knime.
the class RandomForestClassificationTreeNodeWidget method getConnectorLabelBelow.
/**
* {@inheritDoc}
*/
@Override
public String getConnectorLabelBelow() {
TreeNodeClassification node = (TreeNodeClassification) getUserObject();
if (node.getNrChildren() != 0) {
TreeNodeClassification child = node.getChild(0);
TreeNodeCondition childCondition = child.getCondition();
if (childCondition instanceof TreeNodeColumnCondition) {
return ((TreeNodeColumnCondition) childCondition).getAttributeName();
} else if (childCondition instanceof TreeNodeSurrogateCondition) {
TreeNodeSurrogateCondition surrogateCondition = (TreeNodeSurrogateCondition) childCondition;
TreeNodeCondition headCondition = surrogateCondition.getFirstCondition();
if (headCondition instanceof TreeNodeColumnCondition) {
return ((TreeNodeColumnCondition) headCondition).getAttributeName();
}
}
}
return null;
}
use of org.knime.base.node.mine.treeensemble2.model.TreeNodeClassification in project knime-core by knime.
the class TreeEnsembleClassificationPredictorCellFactory method getCells.
/**
* {@inheritDoc}
*/
@Override
public DataCell[] getCells(final DataRow row) {
TreeEnsembleModelPortObject modelObject = m_predictor.getModelObject();
TreeEnsemblePredictorConfiguration cfg = m_predictor.getConfiguration();
final TreeEnsembleModel ensembleModel = modelObject.getEnsembleModel();
int size = 1;
final boolean appendConfidence = cfg.isAppendPredictionConfidence();
if (appendConfidence) {
size += 1;
}
final boolean appendClassConfidences = cfg.isAppendClassConfidences();
if (appendClassConfidences) {
size += m_targetValueMap.size();
}
final boolean appendModelCount = cfg.isAppendModelCount();
if (appendModelCount) {
size += 1;
}
final boolean hasOutOfBagFilter = m_predictor.hasOutOfBagFilter();
DataCell[] result = new DataCell[size];
DataRow filterRow = new FilterColumnRow(row, m_learnColumnInRealDataIndices);
PredictorRecord record = ensembleModel.createPredictorRecord(filterRow, m_learnSpec);
if (record == null) {
// missing value
Arrays.fill(result, DataType.getMissingCell());
return result;
}
final Voting voting = m_votingFactory.createVoting();
final int nrModels = ensembleModel.getNrModels();
int nrValidModels = 0;
for (int i = 0; i < nrModels; i++) {
if (hasOutOfBagFilter && m_predictor.isRowPartOfTrainingData(row.getKey(), i)) {
// ignore, row was used to train the model
} else {
TreeModelClassification m = ensembleModel.getTreeModelClassification(i);
TreeNodeClassification match = m.findMatchingNode(record);
voting.addVote(match);
nrValidModels += 1;
}
}
final NominalValueRepresentation[] targetVals = ((TreeTargetNominalColumnMetaData) ensembleModel.getMetaData().getTargetMetaData()).getValues();
String majorityClass = voting.getMajorityClass();
int index = 0;
if (majorityClass == null) {
assert nrValidModels == 0;
Arrays.fill(result, DataType.getMissingCell());
index = size - 1;
} else {
result[index++] = m_targetValueMap.get(majorityClass);
// final float[] distribution = voting.getClassProbabilities();
if (appendConfidence) {
result[index++] = new DoubleCell(voting.getClassProbabilityForClass(majorityClass));
}
if (appendClassConfidences) {
for (String targetValue : m_targetValueMap.keySet()) {
result[index++] = new DoubleCell(voting.getClassProbabilityForClass(targetValue));
}
}
}
if (appendModelCount) {
result[index++] = new IntCell(voting.getNrVotes());
}
return result;
}
use of org.knime.base.node.mine.treeensemble2.model.TreeNodeClassification in project knime-core by knime.
the class TreeEnsembleClassificationPredictorCellFactory2 method getCells.
/**
* {@inheritDoc}
*/
@Override
public DataCell[] getCells(final DataRow row) {
TreeEnsembleModelPortObject modelObject = m_predictor.getModelObject();
TreeEnsemblePredictorConfiguration cfg = m_predictor.getConfiguration();
final TreeEnsembleModel ensembleModel = modelObject.getEnsembleModel();
int size = 1;
final boolean appendConfidence = cfg.isAppendPredictionConfidence();
if (appendConfidence) {
size += 1;
}
final boolean appendClassConfidences = cfg.isAppendClassConfidences();
if (appendClassConfidences) {
size += m_targetValueMap.size();
}
final boolean appendModelCount = cfg.isAppendModelCount();
if (appendModelCount) {
size += 1;
}
final boolean hasOutOfBagFilter = m_predictor.hasOutOfBagFilter();
DataCell[] result = new DataCell[size];
DataRow filterRow = new FilterColumnRow(row, m_learnColumnInRealDataIndices);
PredictorRecord record = ensembleModel.createPredictorRecord(filterRow, m_learnSpec);
if (record == null) {
// missing value
Arrays.fill(result, DataType.getMissingCell());
return result;
}
OccurrenceCounter<String> counter = new OccurrenceCounter<String>();
final int nrModels = ensembleModel.getNrModels();
TreeTargetNominalColumnMetaData targetMeta = (TreeTargetNominalColumnMetaData) ensembleModel.getMetaData().getTargetMetaData();
final double[] classProbabilities = new double[targetMeta.getValues().length];
int nrValidModels = 0;
for (int i = 0; i < nrModels; i++) {
if (hasOutOfBagFilter && m_predictor.isRowPartOfTrainingData(row.getKey(), i)) {
// ignore, row was used to train the model
} else {
TreeModelClassification m = ensembleModel.getTreeModelClassification(i);
TreeNodeClassification match = m.findMatchingNode(record);
String majorityClassName = match.getMajorityClassName();
final float[] nodeClassProbs = match.getTargetDistribution();
double instancesInNode = 0;
for (int c = 0; c < nodeClassProbs.length; c++) {
instancesInNode += nodeClassProbs[c];
}
for (int c = 0; c < classProbabilities.length; c++) {
classProbabilities[c] += nodeClassProbs[c] / instancesInNode;
}
counter.add(majorityClassName);
nrValidModels += 1;
}
}
String bestValue = counter.getMostFrequent();
int index = 0;
if (bestValue == null) {
assert nrValidModels == 0;
Arrays.fill(result, DataType.getMissingCell());
index = size - 1;
} else {
// result[index++] = m_targetValueMap.get(bestValue);
int indexBest = -1;
double probBest = -1;
for (int c = 0; c < classProbabilities.length; c++) {
double prob = classProbabilities[c];
if (prob > probBest) {
probBest = prob;
indexBest = c;
}
}
result[index++] = new StringCell(targetMeta.getValues()[indexBest].getNominalValue());
if (appendConfidence) {
// final int freqValue = counter.getFrequency(bestValue);
// result[index++] = new DoubleCell(freqValue / (double)nrValidModels);
result[index++] = new DoubleCell(probBest);
}
if (appendClassConfidences) {
for (NominalValueRepresentation nomVal : targetMeta.getValues()) {
double prob = classProbabilities[nomVal.getAssignedInteger()] / nrValidModels;
result[index++] = new DoubleCell(prob);
}
}
}
if (appendModelCount) {
result[index++] = new IntCell(nrValidModels);
}
return result;
}
use of org.knime.base.node.mine.treeensemble2.model.TreeNodeClassification in project knime-core by knime.
the class ClassificationTreeModelExporter method addNodeContent.
/**
* {@inheritDoc}
*/
@Override
protected void addNodeContent(final int nodeId, final Node pmmlNode, final TreeNodeClassification node) {
pmmlNode.setScore(node.getMajorityClassName());
float[] targetDistribution = node.getTargetDistribution();
NominalValueRepresentation[] targetVals = node.getTargetMetaData().getValues();
double sum = 0.0;
for (float v : targetDistribution) {
sum += v;
}
pmmlNode.setRecordCount(sum);
// adding score distribution (class counts)
for (int i = 0; i < targetDistribution.length; i++) {
String className = targetVals[i].getNominalValue();
double freq = targetDistribution[i];
ScoreDistribution pmmlScoreDist = pmmlNode.addNewScoreDistribution();
pmmlScoreDist.setValue(className);
pmmlScoreDist.setRecordCount(freq);
}
}
Aggregations