use of org.knime.base.node.mine.util.PredictorHelper in project knime-core by knime.
the class DecTreePredictorNodeModel method createOutTableSpec.
private DataTableSpec createOutTableSpec(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
List<DataCell> predValues = null;
if (m_showDistribution.getBooleanValue()) {
predValues = getPredictionValues((PMMLPortObjectSpec) inSpecs[INMODELPORT]);
if (predValues == null) {
// no out spec can be determined
return null;
}
}
int numCols = (predValues == null ? 0 : predValues.size()) + 1;
DataTableSpec inSpec = (DataTableSpec) inSpecs[INDATAPORT];
DataColumnSpec[] newCols = new DataColumnSpec[numCols];
/* Set bar renderer and domain [0,1] as default for the double cells
* containing the distribution */
// DataColumnProperties propsRendering = new DataColumnProperties(
// Collections.singletonMap(
// DataValueRenderer.PROPERTY_PREFERRED_RENDERER,
// DoubleBarRenderer.DESCRIPTION));
DataColumnDomain domain = new DataColumnDomainCreator(new DoubleCell(0.0), new DoubleCell(1.0)).createDomain();
PredictorHelper predictorHelper = PredictorHelper.getInstance();
String trainingColumnName = ((PMMLPortObjectSpec) inSpecs[INMODELPORT]).getTargetFields().iterator().next();
// add all distribution columns
for (int i = 0; i < numCols - 1; i++) {
assert predValues != null;
DataColumnSpecCreator colSpecCreator = new DataColumnSpecCreator(predictorHelper.probabilityColumnName(trainingColumnName, predValues.get(i).toString(), m_probabilitySuffix.getStringValue()), DoubleCell.TYPE);
// colSpecCreator.setProperties(propsRendering);
colSpecCreator.setDomain(domain);
newCols[i] = colSpecCreator.createSpec();
}
// add the prediction column
String predictionColumnName = predictorHelper.computePredictionColumnName(m_predictionColumn.getStringValue(), m_overridePrediction.getBooleanValue(), trainingColumnName);
newCols[numCols - 1] = new DataColumnSpecCreator(predictionColumnName, StringCell.TYPE).createSpec();
DataTableSpec newColSpec = new DataTableSpec(newCols);
return new DataTableSpec(inSpec, newColSpec);
}
use of org.knime.base.node.mine.util.PredictorHelper in project knime-core by knime.
the class BasisFunctionPredictor2CellFactory method predict.
/**
* Predicts an unknown row to the given model.
* @param row the row to predict
* @param model a list of rules
* @return mapping class label to array of assigned class degrees
*/
protected DataCell[] predict(final DataRow row, final Map<DataCell, List<BasisFunctionPredictorRow>> model) {
// maps class to activation
Map<DataCell, Double> map = new LinkedHashMap<DataCell, Double>();
// overall basisfunctions in the model
for (DataCell key : model.keySet()) {
for (BasisFunctionPredictorRow bf : model.get(key)) {
DataCell classInfo = bf.getClassLabel();
double act;
if (map.containsKey(classInfo)) {
act = bf.compose(row, map.get(classInfo));
} else {
act = bf.compose(row, 0.0);
}
map.put(classInfo, act);
}
}
// hash column specs
DataTableSpec hash = new DataTableSpec(m_specs);
// find best class activation index
DataCell best = DataType.getMissingCell();
// set default highest activation, not yet set
double hact = -1.0;
double sumAct = 0.0;
Double[] act = new Double[m_specs.length];
PredictorHelper predictorHelper = PredictorHelper.getInstance();
for (DataCell cell : map.keySet()) {
Double d = map.get(cell);
if (d > hact || (d == hact && best.isMissing())) {
hact = d;
best = cell;
}
String colName = predictorHelper.probabilityColumnName(m_trainingColumnName, cell.toString(), m_suffix);
int idx = hash.findColumnIndex(colName);
if (idx >= 0) {
act[idx] = d;
sumAct += d;
}
}
// all class values
DataCell[] res = new DataCell[act.length];
// skip last column which is the winner
for (int i = 0; i < res.length - 1; i++) {
if (act[i] == null) {
res[i] = new DoubleCell(0.0);
} else {
if (m_normClass && sumAct > 0) {
res[i] = new DoubleCell(act[i] / sumAct);
} else {
res[i] = new DoubleCell(act[i]);
}
}
}
// insert class label
if (hact == 0.0 || hact < m_dontKnowClass) {
res[res.length - 1] = DataType.getMissingCell();
} else {
res[res.length - 1] = best;
}
return res;
}
use of org.knime.base.node.mine.util.PredictorHelper in project knime-core by knime.
the class BasisFunctionPredictor2NodeModel method createSpec.
/**
* Creates the output model spec.
* @param dataSpec input data spec
* @param modelSpec input model spec
* @param modelClassIdx index with reflects the class column
* @return the new model spec
*/
public final DataColumnSpec[] createSpec(final DataTableSpec dataSpec, final DataTableSpec modelSpec, final int modelClassIdx) {
DataColumnSpec trainingClass = modelSpec.getColumnSpec(modelClassIdx);
Set<DataCell> possClasses = trainingClass.getDomain().getValues();
final PredictorHelper predictorHelper = PredictorHelper.getInstance();
String trainingClassName = trainingClass.getName();
final DataColumnSpec[] specs;
if (possClasses == null) {
if (m_appendClassProps) {
return null;
} else {
specs = new DataColumnSpec[1];
}
} else {
specs = new DataColumnSpec[possClasses.size() + 1];
Iterator<DataCell> it = possClasses.iterator();
for (int i = 0; i < possClasses.size(); i++) {
String colName = predictorHelper.probabilityColumnName(trainingClassName, it.next().toString(), m_suffix.getStringValue());
specs[i] = new DataColumnSpecCreator(colName, DoubleCell.TYPE).createSpec();
}
}
DataColumnSpecCreator newTargetSpec = new DataColumnSpecCreator(trainingClass);
String applyColumn = predictorHelper.computePredictionColumnName(m_predictionColumn.getStringValue(), m_overridePrediction.getBooleanValue(), trainingClassName);
newTargetSpec.setName(applyColumn);
specs[specs.length - 1] = newTargetSpec.createSpec();
return specs;
}
Aggregations