Search in sources :

Example 1 with TreeMetaData

use of org.knime.base.node.mine.treeensemble2.data.TreeMetaData in project knime-core by knime.

the class RegressionTreeModel method load.

/**
 * Loads and returns new ensemble model, input is NOT closed afterwards.
 *
 * @param in ...
 * @param exec ...
 * @return ...
 * @throws IOException ...
 * @throws CanceledExecutionException ...
 */
public static RegressionTreeModel load(final InputStream in, final ExecutionMonitor exec, final TreeBuildingInterner treeBuildingInterner) throws IOException, CanceledExecutionException {
    // wrapping the argument (zip input) stream in a buffered stream
    // reduces read operation from, e.g. 42s to 2s
    TreeModelDataInputStream input = new TreeModelDataInputStream(new BufferedInputStream(new NonClosableInputStream(in)));
    int version = input.readInt();
    if (version > 20140201) {
        throw new IOException("Tree Ensemble version " + version + " not supported");
    }
    TreeType type = TreeType.load(input);
    TreeMetaData metaData = TreeMetaData.load(input);
    boolean isRegression = metaData.isRegression();
    TreeModelRegression model;
    try {
        model = TreeModelRegression.load(input, metaData, treeBuildingInterner);
        if (input.readByte() != 0) {
            throw new IOException("Model not terminated by 0 byte");
        }
    } catch (IOException e) {
        throw new IOException("Can't read tree model. " + e.getMessage(), e);
    }
    // does not close the method argument stream!!
    input.close();
    return new RegressionTreeModel(metaData, model, type);
}
Also used : BufferedInputStream(java.io.BufferedInputStream) NonClosableInputStream(org.knime.core.data.util.NonClosableInputStream) TreeMetaData(org.knime.base.node.mine.treeensemble2.data.TreeMetaData) IOException(java.io.IOException)

Example 2 with TreeMetaData

use of org.knime.base.node.mine.treeensemble2.data.TreeMetaData in project knime-core by knime.

the class TreeEnsembleModel method createDecisionTree.

public DecisionTree createDecisionTree(final int modelIndex, final DataTable sampleForHiliting) {
    final DecisionTree result;
    final TreeMetaData metaData = getMetaData();
    if (metaData.isRegression()) {
        TreeModelRegression treeModel = getTreeModelRegression(modelIndex);
        result = treeModel.createDecisionTree(metaData);
    } else {
        TreeModelClassification treeModel = getTreeModelClassification(modelIndex);
        result = treeModel.createDecisionTree(metaData);
    }
    if (sampleForHiliting != null) {
        final DataTableSpec dataSpec = sampleForHiliting.getDataTableSpec();
        final DataTableSpec spec = getLearnAttributeSpec(dataSpec);
        for (DataRow r : sampleForHiliting) {
            try {
                DataRow fullAttributeRow = createLearnAttributeRow(r, spec);
                result.addCoveredPattern(fullAttributeRow, spec);
            } catch (Exception e) {
                // dunno what to do with that
                NodeLogger.getLogger(getClass()).error("Error updating hilite info in tree view", e);
                break;
            }
        }
    }
    return result;
}
Also used : DecisionTree(org.knime.base.node.mine.decisiontree2.model.DecisionTree) DataTableSpec(org.knime.core.data.DataTableSpec) TreeMetaData(org.knime.base.node.mine.treeensemble2.data.TreeMetaData) DataRow(org.knime.core.data.DataRow) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException)

Example 3 with TreeMetaData

use of org.knime.base.node.mine.treeensemble2.data.TreeMetaData in project knime-core by knime.

the class TreeEnsembleModel method load.

public static TreeEnsembleModel load(final InputStream in) throws IOException {
    // wrapping the argument (zip input) stream in a buffered stream
    // reduces read operation from, e.g. 42s to 2s
    TreeModelDataInputStream input = new TreeModelDataInputStream(new BufferedInputStream(new NonClosableInputStream(in)));
    int version = input.readInt();
    if (version > 20160114) {
        throw new IOException("Tree Ensemble version " + version + " not supported");
    }
    byte ensembleType;
    if (version == 20160114) {
        ensembleType = input.readByte();
    } else {
        ensembleType = 'r';
    }
    TreeType type = TreeType.load(input);
    TreeMetaData metaData = TreeMetaData.load(input);
    int nrModels = input.readInt();
    boolean containsClassDistribution;
    if (version == 20121019) {
        containsClassDistribution = true;
    } else {
        containsClassDistribution = input.readBoolean();
    }
    input.setContainsClassDistribution(containsClassDistribution);
    AbstractTreeModel[] models = new AbstractTreeModel[nrModels];
    boolean isRegression = metaData.isRegression();
    if (ensembleType != 'r') {
        isRegression = true;
    }
    final TreeBuildingInterner treeBuildingInterner = new TreeBuildingInterner();
    for (int i = 0; i < nrModels; i++) {
        AbstractTreeModel singleModel;
        try {
            singleModel = isRegression ? TreeModelRegression.load(input, metaData, treeBuildingInterner) : TreeModelClassification.load(input, metaData, treeBuildingInterner);
            if (input.readByte() != 0) {
                throw new IOException("Model not terminated by 0 byte");
            }
        } catch (IOException e) {
            throw new IOException("Can't read tree model " + (i + 1) + "/" + nrModels + ": " + e.getMessage(), e);
        }
        models[i] = singleModel;
    }
    TreeEnsembleModel result;
    switch(ensembleType) {
        case 'r':
            result = new TreeEnsembleModel(metaData, models, type, containsClassDistribution);
            break;
        case 'g':
            result = new GradientBoostingModel(metaData, models, type, containsClassDistribution);
            break;
        case 't':
            result = new GradientBoostedTreesModel(metaData, models, type, containsClassDistribution);
            break;
        case 'm':
            result = new MultiClassGradientBoostedTreesModel(metaData, models, type, containsClassDistribution);
            break;
        default:
            throw new IllegalStateException("Unknown ensemble type: '" + (char) ensembleType + "'");
    }
    result.loadData(input);
    // does not close the method argument stream!!
    input.close();
    return result;
}
Also used : IOException(java.io.IOException) BufferedInputStream(java.io.BufferedInputStream) NonClosableInputStream(org.knime.core.data.util.NonClosableInputStream) TreeMetaData(org.knime.base.node.mine.treeensemble2.data.TreeMetaData)

Example 4 with TreeMetaData

use of org.knime.base.node.mine.treeensemble2.data.TreeMetaData in project knime-core by knime.

the class TreeNodeClassification method createDecisionTreeNode.

/**
 * Creates DecisionTreeNode model that is used in Decision Tree of KNIME
 *
 * @param idGenerator
 * @param metaData
 * @return a DecisionTreeNode
 */
public DecisionTreeNode createDecisionTreeNode(final MutableInteger idGenerator, final TreeMetaData metaData) {
    DataCell majorityCell = new StringCell(getMajorityClassName());
    final float[] targetDistribution = getTargetDistribution();
    int initSize = (int) (targetDistribution.length / 0.75 + 1.0);
    LinkedHashMap<DataCell, Double> scoreDistributionMap = new LinkedHashMap<DataCell, Double>(initSize);
    NominalValueRepresentation[] targets = getTargetMetaData().getValues();
    for (int i = 0; i < targetDistribution.length; i++) {
        String cl = targets[i].getNominalValue();
        double d = targetDistribution[i];
        scoreDistributionMap.put(new StringCell(cl), d);
    }
    final int nrChildren = getNrChildren();
    if (nrChildren == 0) {
        return new DecisionTreeNodeLeaf(idGenerator.inc(), majorityCell, scoreDistributionMap);
    } else {
        int id = idGenerator.inc();
        DecisionTreeNode[] childNodes = new DecisionTreeNode[nrChildren];
        int splitAttributeIndex = getSplitAttributeIndex();
        assert splitAttributeIndex >= 0 : "non-leaf node has no split";
        String splitAttribute = metaData.getAttributeMetaData(splitAttributeIndex).getAttributeName();
        PMMLPredicate[] childPredicates = new PMMLPredicate[nrChildren];
        for (int i = 0; i < nrChildren; i++) {
            final TreeNodeClassification treeNode = getChild(i);
            TreeNodeCondition cond = treeNode.getCondition();
            childPredicates[i] = cond.toPMMLPredicate();
            childNodes[i] = treeNode.createDecisionTreeNode(idGenerator, metaData);
        }
        return new DecisionTreeNodeSplitPMML(id, majorityCell, scoreDistributionMap, splitAttribute, childPredicates, childNodes);
    }
}
Also used : NominalValueRepresentation(org.knime.base.node.mine.treeensemble2.data.NominalValueRepresentation) PMMLPredicate(org.knime.base.node.mine.decisiontree2.PMMLPredicate) LinkedHashMap(java.util.LinkedHashMap) DecisionTreeNodeLeaf(org.knime.base.node.mine.decisiontree2.model.DecisionTreeNodeLeaf) StringCell(org.knime.core.data.def.StringCell) DecisionTreeNodeSplitPMML(org.knime.base.node.mine.decisiontree2.model.DecisionTreeNodeSplitPMML) DataCell(org.knime.core.data.DataCell) DecisionTreeNode(org.knime.base.node.mine.decisiontree2.model.DecisionTreeNode)

Aggregations

IOException (java.io.IOException)3 TreeMetaData (org.knime.base.node.mine.treeensemble2.data.TreeMetaData)3 BufferedInputStream (java.io.BufferedInputStream)2 NonClosableInputStream (org.knime.core.data.util.NonClosableInputStream)2 LinkedHashMap (java.util.LinkedHashMap)1 PMMLPredicate (org.knime.base.node.mine.decisiontree2.PMMLPredicate)1 DecisionTree (org.knime.base.node.mine.decisiontree2.model.DecisionTree)1 DecisionTreeNode (org.knime.base.node.mine.decisiontree2.model.DecisionTreeNode)1 DecisionTreeNodeLeaf (org.knime.base.node.mine.decisiontree2.model.DecisionTreeNodeLeaf)1 DecisionTreeNodeSplitPMML (org.knime.base.node.mine.decisiontree2.model.DecisionTreeNodeSplitPMML)1 NominalValueRepresentation (org.knime.base.node.mine.treeensemble2.data.NominalValueRepresentation)1 DataCell (org.knime.core.data.DataCell)1 DataRow (org.knime.core.data.DataRow)1 DataTableSpec (org.knime.core.data.DataTableSpec)1 StringCell (org.knime.core.data.def.StringCell)1 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)1