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]);
}
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]);
}
Aggregations