use of org.knime.base.node.mine.treeensemble.model.TreeNodeRegression in project knime-core by knime.
the class RegressionTreePredictorCellFactory method getCells.
/**
* {@inheritDoc}
*/
@Override
public DataCell[] getCells(final DataRow row) {
RegressionTreeModelPortObject modelObject = m_predictor.getModelObject();
final RegressionTreeModel treeModel = modelObject.getModel();
int size = 1;
DataCell[] result = new DataCell[size];
DataRow filterRow = new FilterColumnRow(row, m_learnColumnInRealDataIndices);
PredictorRecord record = treeModel.createPredictorRecord(filterRow, m_learnSpec);
if (record == null) {
// missing value
Arrays.fill(result, DataType.getMissingCell());
return result;
}
TreeModelRegression tree = treeModel.getTreeModel();
TreeNodeRegression match = tree.findMatchingNode(record);
double nodeMean = match.getMean();
result[0] = new DoubleCell(nodeMean);
return result;
}
use of org.knime.base.node.mine.treeensemble.model.TreeNodeRegression in project knime-core by knime.
the class TreeEnsembleRegressionPredictorCellFactory 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();
final boolean appendModelCount = cfg.isAppendModelCount();
if (appendConfidence) {
size += 1;
}
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;
}
Mean mean = new Mean();
Variance variance = new Variance();
final int nrModels = ensembleModel.getNrModels();
for (int i = 0; i < nrModels; i++) {
if (hasOutOfBagFilter && m_predictor.isRowPartOfTrainingData(row.getKey(), i)) {
// ignore, row was used to train the model
} else {
TreeModelRegression m = ensembleModel.getTreeModelRegression(i);
TreeNodeRegression match = m.findMatchingNode(record);
double nodeMean = match.getMean();
mean.increment(nodeMean);
variance.increment(nodeMean);
}
}
int nrValidModels = (int) mean.getN();
int index = 0;
result[index++] = nrValidModels == 0 ? DataType.getMissingCell() : new DoubleCell(mean.getResult());
if (appendConfidence) {
result[index++] = nrValidModels == 0 ? DataType.getMissingCell() : new DoubleCell(variance.getResult());
}
if (appendModelCount) {
result[index++] = new IntCell(nrValidModels);
}
return result;
}
use of org.knime.base.node.mine.treeensemble.model.TreeNodeRegression in project knime-core by knime.
the class TreeLearnerRegression method learnSingleTree.
/**
* {@inheritDoc}
*/
@Override
public TreeModelRegression learnSingleTree(final ExecutionMonitor exec, final RandomData rd) throws CanceledExecutionException {
final TreeTargetNumericColumnData targetColumn = getTargetData();
final TreeData data = getData();
final RowSample rowSampling = getRowSampling();
final TreeEnsembleLearnerConfiguration config = getConfig();
double[] dataMemberships = new double[data.getNrRows()];
for (int i = 0; i < dataMemberships.length; i++) {
dataMemberships[i] = rowSampling.getCountFor(i);
}
RegressionPriors targetPriors = targetColumn.getPriors(dataMemberships, config);
BitSet forbiddenColumnSet = new BitSet(data.getNrAttributes());
// TreeNodeMembershipController rootMembershipController = new TreeNodeMembershipController(data, dataMemberships);
TreeNodeMembershipController rootMembershipController = null;
TreeNodeRegression rootNode = buildTreeNode(exec, 0, dataMemberships, TreeNodeSignature.ROOT_SIGNATURE, targetPriors, forbiddenColumnSet, rootMembershipController);
assert forbiddenColumnSet.cardinality() == 0;
rootNode.setTreeNodeCondition(TreeNodeTrueCondition.INSTANCE);
return new TreeModelRegression(rootNode);
}
use of org.knime.base.node.mine.treeensemble.model.TreeNodeRegression in project knime-core by knime.
the class TreeLearnerRegression method buildTreeNode.
private TreeNodeRegression buildTreeNode(final ExecutionMonitor exec, final int currentDepth, final double[] rowSampleWeights, final TreeNodeSignature treeNodeSignature, final RegressionPriors targetPriors, final BitSet forbiddenColumnSet, final TreeNodeMembershipController membershipController) throws CanceledExecutionException {
final TreeData data = getData();
final TreeEnsembleLearnerConfiguration config = getConfig();
exec.checkCanceled();
SplitCandidate bestSplit = findBestSplitRegression(currentDepth, rowSampleWeights, treeNodeSignature, targetPriors, forbiddenColumnSet, membershipController);
if (bestSplit == null) {
return new TreeNodeRegression(treeNodeSignature, targetPriors);
}
TreeAttributeColumnData splitColumn = bestSplit.getColumnData();
final int attributeIndex = splitColumn.getMetaData().getAttributeIndex();
boolean markAttributeAsForbidden = !bestSplit.canColumnBeSplitFurther();
forbiddenColumnSet.set(attributeIndex, markAttributeAsForbidden);
TreeNodeCondition[] childConditions = bestSplit.getChildConditions();
if (childConditions.length > Short.MAX_VALUE) {
throw new RuntimeException("Too many children when splitting " + "attribute " + bestSplit.getColumnData() + " (maximum supported: " + Short.MAX_VALUE + "): " + childConditions.length);
}
TreeNodeRegression[] childNodes = new TreeNodeRegression[childConditions.length];
final double[] dataMemberships = rowSampleWeights;
final double[] childMemberships = new double[dataMemberships.length];
final TreeTargetNumericColumnData targetColumn = (TreeTargetNumericColumnData) data.getTargetColumn();
for (int i = 0; i < childConditions.length; i++) {
System.arraycopy(dataMemberships, 0, childMemberships, 0, dataMemberships.length);
TreeNodeCondition cond = childConditions[i];
splitColumn.updateChildMemberships(cond, dataMemberships, childMemberships);
RegressionPriors childTargetPriors = targetColumn.getPriors(childMemberships, config);
TreeNodeSignature childSignature = treeNodeSignature.createChildSignature((short) i);
TreeNodeMembershipController childMembershipController = splitColumn.getChildNodeMembershipController(cond, membershipController);
childNodes[i] = buildTreeNode(exec, currentDepth + 1, childMemberships, childSignature, childTargetPriors, forbiddenColumnSet, childMembershipController);
childNodes[i].setTreeNodeCondition(cond);
}
if (markAttributeAsForbidden) {
forbiddenColumnSet.set(attributeIndex, false);
}
return new TreeNodeRegression(treeNodeSignature, targetPriors, childNodes);
}
Aggregations