Search in sources :

Example 1 with TreeModelRegression

use of org.knime.base.node.mine.treeensemble.model.TreeModelRegression 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;
}
Also used : RegressionTreeModelPortObject(org.knime.base.node.mine.treeensemble.model.RegressionTreeModelPortObject) RegressionTreeModel(org.knime.base.node.mine.treeensemble.model.RegressionTreeModel) DoubleCell(org.knime.core.data.def.DoubleCell) PredictorRecord(org.knime.base.node.mine.treeensemble.data.PredictorRecord) DataCell(org.knime.core.data.DataCell) DataRow(org.knime.core.data.DataRow) TreeNodeRegression(org.knime.base.node.mine.treeensemble.model.TreeNodeRegression) FilterColumnRow(org.knime.base.data.filter.column.FilterColumnRow) TreeModelRegression(org.knime.base.node.mine.treeensemble.model.TreeModelRegression)

Example 2 with TreeModelRegression

use of org.knime.base.node.mine.treeensemble.model.TreeModelRegression 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);
}
Also used : TreeEnsembleLearnerConfiguration(org.knime.base.node.mine.treeensemble.node.learner.TreeEnsembleLearnerConfiguration) TreeNodeMembershipController(org.knime.base.node.mine.treeensemble.data.TreeNodeMembershipController) RegressionPriors(org.knime.base.node.mine.treeensemble.data.RegressionPriors) BitSet(java.util.BitSet) TreeTargetNumericColumnData(org.knime.base.node.mine.treeensemble.data.TreeTargetNumericColumnData) TreeData(org.knime.base.node.mine.treeensemble.data.TreeData) RowSample(org.knime.base.node.mine.treeensemble.sample.row.RowSample) TreeNodeRegression(org.knime.base.node.mine.treeensemble.model.TreeNodeRegression) TreeModelRegression(org.knime.base.node.mine.treeensemble.model.TreeModelRegression)

Example 3 with TreeModelRegression

use of org.knime.base.node.mine.treeensemble.model.TreeModelRegression 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;
}
Also used : Mean(org.apache.commons.math.stat.descriptive.moment.Mean) TreeEnsembleModel(org.knime.base.node.mine.treeensemble.model.TreeEnsembleModel) DoubleCell(org.knime.core.data.def.DoubleCell) TreeEnsemblePredictorConfiguration(org.knime.base.node.mine.treeensemble.node.predictor.TreeEnsemblePredictorConfiguration) DataRow(org.knime.core.data.DataRow) TreeNodeRegression(org.knime.base.node.mine.treeensemble.model.TreeNodeRegression) Variance(org.apache.commons.math.stat.descriptive.moment.Variance) TreeModelRegression(org.knime.base.node.mine.treeensemble.model.TreeModelRegression) IntCell(org.knime.core.data.def.IntCell) TreeEnsembleModelPortObject(org.knime.base.node.mine.treeensemble.model.TreeEnsembleModelPortObject) PredictorRecord(org.knime.base.node.mine.treeensemble.data.PredictorRecord) DataCell(org.knime.core.data.DataCell) FilterColumnRow(org.knime.base.data.filter.column.FilterColumnRow)

Example 4 with TreeModelRegression

use of org.knime.base.node.mine.treeensemble.model.TreeModelRegression in project knime-core by knime.

the class RegressionTreeLearnerNodeModel method execute.

// /**
// * @param ensembleSpec
// * @param ensembleModel
// * @param inSpec
// * @return
// * @throws InvalidSettingsException
// */
// private TreeEnsemblePredictor createOutOfBagPredictor(final TreeEnsembleModelPortObjectSpec ensembleSpec,
// final TreeEnsembleModelPortObject ensembleModel, final DataTableSpec inSpec) throws InvalidSettingsException {
// TreeEnsemblePredictorConfiguration ooBConfig = new TreeEnsemblePredictorConfiguration(true);
// String targetColumn = m_configuration.getTargetColumn();
// String append = targetColumn + " (Out-of-bag)";
// ooBConfig.setPredictionColumnName(append);
// ooBConfig.setAppendPredictionConfidence(true);
// ooBConfig.setAppendClassConfidences(true);
// ooBConfig.setAppendModelCount(true);
// return new TreeEnsemblePredictor(ensembleSpec, ensembleModel, inSpec, ooBConfig);
// }
/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inObjects, final ExecutionContext exec) throws Exception {
    BufferedDataTable t = (BufferedDataTable) inObjects[0];
    DataTableSpec spec = t.getDataTableSpec();
    final FilterLearnColumnRearranger learnRearranger = m_configuration.filterLearnColumns(spec);
    String warn = learnRearranger.getWarning();
    BufferedDataTable learnTable = exec.createColumnRearrangeTable(t, learnRearranger, exec.createSubProgress(0.0));
    DataTableSpec learnSpec = learnTable.getDataTableSpec();
    TreeEnsembleModelPortObjectSpec ensembleSpec = m_configuration.createPortObjectSpec(learnSpec);
    ExecutionMonitor readInExec = exec.createSubProgress(0.1);
    ExecutionMonitor learnExec = exec.createSubProgress(0.9);
    TreeDataCreator dataCreator = new TreeDataCreator(m_configuration, learnSpec, learnTable.getRowCount());
    exec.setProgress("Reading data into memory");
    TreeData data = dataCreator.readData(learnTable, m_configuration, readInExec);
    m_hiliteRowSample = dataCreator.getDataRowsForHilite();
    m_viewMessage = dataCreator.getViewMessage();
    String dataCreationWarning = dataCreator.getAndClearWarningMessage();
    if (dataCreationWarning != null) {
        if (warn == null) {
            warn = dataCreationWarning;
        } else {
            warn = warn + "\n" + dataCreationWarning;
        }
    }
    readInExec.setProgress(1.0);
    exec.setMessage("Learning tree");
    // TreeEnsembleLearner learner = new TreeEnsembleLearner(m_configuration, data);
    // TreeEnsembleModel model;
    // try {
    // model = learner.learnEnsemble(learnExec);
    // } catch (ExecutionException e) {
    // Throwable cause = e.getCause();
    // if (cause instanceof Exception) {
    // throw (Exception)cause;
    // }
    // throw e;
    // }
    RandomData rd = m_configuration.createRandomData();
    TreeLearnerRegression treeLearner = new TreeLearnerRegression(m_configuration, data, rd);
    TreeModelRegression regTree = treeLearner.learnSingleTree(learnExec, rd);
    RegressionTreeModel model = new RegressionTreeModel(m_configuration, data.getMetaData(), regTree, data.getTreeType());
    RegressionTreeModelPortObjectSpec treePortObjectSpec = new RegressionTreeModelPortObjectSpec(learnSpec);
    RegressionTreeModelPortObject treePortObject = new RegressionTreeModelPortObject(model, treePortObjectSpec);
    learnExec.setProgress(1.0);
    m_treeModelPortObject = treePortObject;
    if (warn != null) {
        setWarningMessage(warn);
    }
    return new PortObject[] { treePortObject };
}
Also used : RegressionTreeModelPortObject(org.knime.base.node.mine.treeensemble.model.RegressionTreeModelPortObject) DataTableSpec(org.knime.core.data.DataTableSpec) RandomData(org.apache.commons.math.random.RandomData) TreeEnsembleModelPortObjectSpec(org.knime.base.node.mine.treeensemble.model.TreeEnsembleModelPortObjectSpec) RegressionTreeModel(org.knime.base.node.mine.treeensemble.model.RegressionTreeModel) RegressionTreeModelPortObjectSpec(org.knime.base.node.mine.treeensemble.model.RegressionTreeModelPortObjectSpec) TreeModelRegression(org.knime.base.node.mine.treeensemble.model.TreeModelRegression) BufferedDataTable(org.knime.core.node.BufferedDataTable) FilterLearnColumnRearranger(org.knime.base.node.mine.treeensemble.node.learner.TreeEnsembleLearnerConfiguration.FilterLearnColumnRearranger) TreeData(org.knime.base.node.mine.treeensemble.data.TreeData) TreeLearnerRegression(org.knime.base.node.mine.treeensemble.learner.TreeLearnerRegression) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) TreeDataCreator(org.knime.base.node.mine.treeensemble.data.TreeDataCreator) RegressionTreeModelPortObject(org.knime.base.node.mine.treeensemble.model.RegressionTreeModelPortObject) PortObject(org.knime.core.node.port.PortObject)

Aggregations

TreeModelRegression (org.knime.base.node.mine.treeensemble.model.TreeModelRegression)4 TreeNodeRegression (org.knime.base.node.mine.treeensemble.model.TreeNodeRegression)3 FilterColumnRow (org.knime.base.data.filter.column.FilterColumnRow)2 PredictorRecord (org.knime.base.node.mine.treeensemble.data.PredictorRecord)2 TreeData (org.knime.base.node.mine.treeensemble.data.TreeData)2 RegressionTreeModel (org.knime.base.node.mine.treeensemble.model.RegressionTreeModel)2 RegressionTreeModelPortObject (org.knime.base.node.mine.treeensemble.model.RegressionTreeModelPortObject)2 DataCell (org.knime.core.data.DataCell)2 DataRow (org.knime.core.data.DataRow)2 DoubleCell (org.knime.core.data.def.DoubleCell)2 BitSet (java.util.BitSet)1 RandomData (org.apache.commons.math.random.RandomData)1 Mean (org.apache.commons.math.stat.descriptive.moment.Mean)1 Variance (org.apache.commons.math.stat.descriptive.moment.Variance)1 RegressionPriors (org.knime.base.node.mine.treeensemble.data.RegressionPriors)1 TreeDataCreator (org.knime.base.node.mine.treeensemble.data.TreeDataCreator)1 TreeNodeMembershipController (org.knime.base.node.mine.treeensemble.data.TreeNodeMembershipController)1 TreeTargetNumericColumnData (org.knime.base.node.mine.treeensemble.data.TreeTargetNumericColumnData)1 TreeLearnerRegression (org.knime.base.node.mine.treeensemble.learner.TreeLearnerRegression)1 RegressionTreeModelPortObjectSpec (org.knime.base.node.mine.treeensemble.model.RegressionTreeModelPortObjectSpec)1