Search in sources :

Example 1 with NominalValue

use of org.knime.core.data.NominalValue in project knime-core by knime.

the class RegressionPredictorCellFactory method createColumnSpec.

/**
 * Creates the spec of the output if possible.
 *
 * @param portSpec the spec of the pmml input port
 * @param tableSpec the spec of the data input port
 * @param settings settings for the predictor node
 * @return The spec of the output or null
 * @throws InvalidSettingsException when tableSpec and portSpec do not match
 */
public static DataColumnSpec[] createColumnSpec(final PMMLPortObjectSpec portSpec, final DataTableSpec tableSpec, final RegressionPredictorSettings settings) throws InvalidSettingsException {
    // Assertions
    if (portSpec.getTargetCols().isEmpty()) {
        throw new InvalidSettingsException("The general regression model" + " does not specify a target column.");
    }
    for (DataColumnSpec learningColSpec : portSpec.getLearningCols()) {
        String learningCol = learningColSpec.getName();
        if (tableSpec.containsName(learningCol)) {
            DataColumnSpec colSpec = tableSpec.getColumnSpec(learningCol);
            if (learningColSpec.getType().isCompatible(NominalValue.class)) {
                if (!colSpec.getType().isCompatible(BitVectorValue.class) && !colSpec.getType().isCompatible(ByteVectorValue.class) && !colSpec.getType().isCompatible(NominalValue.class)) {
                    throw new InvalidSettingsException("The column \"" + learningCol + "\" in the table of prediction " + "is expected to be  compatible with " + "\"NominalValue\".");
                }
            } else if (learningColSpec.getType().isCompatible(DoubleValue.class) && !colSpec.getType().isCompatible(DoubleValue.class)) {
                throw new InvalidSettingsException("The column \"" + learningCol + "\" in the table of prediction " + "is expected to be numeric.");
            }
        } else {
            throw new InvalidSettingsException("The table for prediction " + "does not contain the column \"" + learningCol + "\".");
        }
    }
    // The list of added columns
    List<DataColumnSpec> newColsSpec = new ArrayList<DataColumnSpec>();
    String targetCol = portSpec.getTargetFields().get(0);
    DataColumnSpec targetColSpec = portSpec.getDataTableSpec().getColumnSpec(targetCol);
    if (settings.getIncludeProbabilities() && targetColSpec.getType().isCompatible(NominalValue.class)) {
        if (!targetColSpec.getDomain().hasValues()) {
            return null;
        }
        List<DataCell> targetCategories = new ArrayList<DataCell>();
        targetCategories.addAll(targetColSpec.getDomain().getValues());
        for (DataCell value : targetCategories) {
            String name = "P (" + targetCol + "=" + value.toString() + ")" + settings.getPropColumnSuffix();
            String newColName = DataTableSpec.getUniqueColumnName(tableSpec, name);
            DataColumnSpecCreator colSpecCreator = new DataColumnSpecCreator(newColName, DoubleCell.TYPE);
            DataColumnDomainCreator domainCreator = new DataColumnDomainCreator(new DoubleCell(0.0), new DoubleCell(1.0));
            colSpecCreator.setDomain(domainCreator.createDomain());
            newColsSpec.add(colSpecCreator.createSpec());
        }
    }
    String targetColName = settings.getHasCustomPredictionName() ? settings.getCustomPredictionName() : "Prediction (" + targetCol + ")";
    String uniqueTargetColName = DataTableSpec.getUniqueColumnName(tableSpec, targetColName);
    DataType targetType = targetColSpec.getType().isCompatible(NominalValue.class) ? targetColSpec.getType() : DoubleCell.TYPE;
    DataColumnSpecCreator targetColSpecCreator = new DataColumnSpecCreator(uniqueTargetColName, targetType);
    if (targetColSpec.getType().isCompatible(NominalValue.class)) {
        DataColumnDomainCreator targetDomainCreator = new DataColumnDomainCreator(targetColSpec.getDomain());
        targetColSpecCreator.setDomain(targetDomainCreator.createDomain());
    }
    newColsSpec.add(targetColSpecCreator.createSpec());
    return newColsSpec.toArray(new DataColumnSpec[0]);
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) NominalValue(org.knime.core.data.NominalValue) DoubleCell(org.knime.core.data.def.DoubleCell) ArrayList(java.util.ArrayList) DataColumnDomainCreator(org.knime.core.data.DataColumnDomainCreator) ByteVectorValue(org.knime.core.data.vector.bytevector.ByteVectorValue) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DoubleValue(org.knime.core.data.DoubleValue) DataCell(org.knime.core.data.DataCell) DataType(org.knime.core.data.DataType) BitVectorValue(org.knime.core.data.vector.bitvector.BitVectorValue)

Example 2 with NominalValue

use of org.knime.core.data.NominalValue in project knime-core by knime.

the class RegressionPredictorCellFactory method createColumnSpec.

/**
 * Creates the spec of the output if possible.
 *
 * @param portSpec the spec of the pmml input port
 * @param tableSpec the spec of the data input port
 * @param settings settings for the predictor node
 * @return The spec of the output or null
 * @throws InvalidSettingsException when tableSpec and portSpec do not match
 */
public static DataColumnSpec[] createColumnSpec(final PMMLPortObjectSpec portSpec, final DataTableSpec tableSpec, final RegressionPredictorSettings settings) throws InvalidSettingsException {
    // Assertions
    if (portSpec.getTargetCols().isEmpty()) {
        throw new InvalidSettingsException("The general regression model" + " does not specify a target column.");
    }
    for (DataColumnSpec learningColSpec : portSpec.getLearningCols()) {
        String learningCol = learningColSpec.getName();
        if (tableSpec.containsName(learningCol)) {
            DataColumnSpec colSpec = tableSpec.getColumnSpec(learningCol);
            if (learningColSpec.getType().isCompatible(NominalValue.class)) {
                if (!colSpec.getType().isCompatible(BitVectorValue.class) && !colSpec.getType().isCompatible(ByteVectorValue.class) && !colSpec.getType().isCompatible(NominalValue.class)) {
                    throw new InvalidSettingsException("The column \"" + learningCol + "\" in the table of prediction " + "is expected to be  compatible with " + "\"NominalValue\".");
                }
            } else if (learningColSpec.getType().isCompatible(DoubleValue.class) && !colSpec.getType().isCompatible(DoubleValue.class)) {
                throw new InvalidSettingsException("The column \"" + learningCol + "\" in the table of prediction " + "is expected to be numeric.");
            }
        } else {
            throw new InvalidSettingsException("The table for prediction " + "does not contain the column \"" + learningCol + "\".");
        }
    }
    // The list of added columns
    List<DataColumnSpec> newColsSpec = new ArrayList<DataColumnSpec>();
    String targetCol = portSpec.getTargetFields().get(0);
    DataColumnSpec targetColSpec = portSpec.getDataTableSpec().getColumnSpec(targetCol);
    if (settings.getIncludeProbabilities() && targetColSpec.getType().isCompatible(NominalValue.class)) {
        if (!targetColSpec.getDomain().hasValues()) {
            return null;
        }
        List<DataCell> targetCategories = new ArrayList<DataCell>();
        targetCategories.addAll(targetColSpec.getDomain().getValues());
        for (DataCell value : targetCategories) {
            String name = "P (" + targetCol + "=" + value.toString() + ")" + settings.getPropColumnSuffix();
            String newColName = DataTableSpec.getUniqueColumnName(tableSpec, name);
            DataColumnSpecCreator colSpecCreator = new DataColumnSpecCreator(newColName, DoubleCell.TYPE);
            DataColumnDomainCreator domainCreator = new DataColumnDomainCreator(new DoubleCell(0.0), new DoubleCell(1.0));
            colSpecCreator.setDomain(domainCreator.createDomain());
            newColsSpec.add(colSpecCreator.createSpec());
        }
    }
    String targetColName = settings.getHasCustomPredictionName() ? settings.getCustomPredictionName() : "Prediction (" + targetCol + ")";
    String uniqueTargetColName = DataTableSpec.getUniqueColumnName(tableSpec, targetColName);
    DataType targetType = targetColSpec.getType().isCompatible(NominalValue.class) ? targetColSpec.getType() : DoubleCell.TYPE;
    DataColumnSpecCreator targetColSpecCreator = new DataColumnSpecCreator(uniqueTargetColName, targetType);
    if (targetColSpec.getType().isCompatible(NominalValue.class)) {
        DataColumnDomainCreator targetDomainCreator = new DataColumnDomainCreator(targetColSpec.getDomain());
        targetColSpecCreator.setDomain(targetDomainCreator.createDomain());
    }
    newColsSpec.add(targetColSpecCreator.createSpec());
    return newColsSpec.toArray(new DataColumnSpec[0]);
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) NominalValue(org.knime.core.data.NominalValue) DoubleCell(org.knime.core.data.def.DoubleCell) ArrayList(java.util.ArrayList) DataColumnDomainCreator(org.knime.core.data.DataColumnDomainCreator) ByteVectorValue(org.knime.core.data.vector.bytevector.ByteVectorValue) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DoubleValue(org.knime.core.data.DoubleValue) DataCell(org.knime.core.data.DataCell) DataType(org.knime.core.data.DataType) BitVectorValue(org.knime.core.data.vector.bitvector.BitVectorValue)

Aggregations

ArrayList (java.util.ArrayList)2 DataCell (org.knime.core.data.DataCell)2 DataColumnDomainCreator (org.knime.core.data.DataColumnDomainCreator)2 DataColumnSpec (org.knime.core.data.DataColumnSpec)2 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)2 DataType (org.knime.core.data.DataType)2 DoubleValue (org.knime.core.data.DoubleValue)2 NominalValue (org.knime.core.data.NominalValue)2 DoubleCell (org.knime.core.data.def.DoubleCell)2 BitVectorValue (org.knime.core.data.vector.bitvector.BitVectorValue)2 ByteVectorValue (org.knime.core.data.vector.bytevector.ByteVectorValue)2 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)2