use of org.knime.core.node.port.pmml.PMMLPortObjectSpec in project knime-core by knime.
the class PolyRegLearnerNodeModel method configure.
/**
* {@inheritDoc}
*/
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
DataTableSpec tableSpec = (DataTableSpec) inSpecs[0];
PMMLPortObjectSpec pmmlSpec = (PMMLPortObjectSpec) inSpecs[1];
String[] selectedCols = computeSelectedColumns(tableSpec);
for (String colName : selectedCols) {
DataColumnSpec dcs = tableSpec.getColumnSpec(colName);
if (dcs == null) {
throw new InvalidSettingsException("Selected column '" + colName + "' does not exist in input table");
}
if (!dcs.getType().isCompatible(DoubleValue.class)) {
throw new InvalidSettingsException("Selected column '" + dcs.getName() + "' from the input table is not a numeric column.");
}
}
if (m_settings.getTargetColumn() == null) {
throw new InvalidSettingsException("No target column selected");
}
if (tableSpec.findColumnIndex(m_settings.getTargetColumn()) == -1) {
throw new InvalidSettingsException("Target column '" + m_settings.getTargetColumn() + "' does not exist.");
}
DataColumnSpecCreator crea = new DataColumnSpecCreator("PolyReg prediction", DoubleCell.TYPE);
DataColumnSpec col1 = crea.createSpec();
crea = new DataColumnSpecCreator("Prediction Error", DoubleCell.TYPE);
DataColumnSpec col2 = crea.createSpec();
return new PortObjectSpec[] { AppendedColumnTable.getTableSpec(tableSpec, col1, col2), createModelSpec(pmmlSpec, tableSpec) };
}
use of org.knime.core.node.port.pmml.PMMLPortObjectSpec in project knime-core by knime.
the class PolyRegLearnerNodeModel method createPMMLModel.
private PMMLPortObject createPMMLModel(final PMMLPortObject inPMMLPort, final DataTableSpec inSpec) throws InvalidSettingsException, SAXException {
NumericPredictor[] preds = new NumericPredictor[m_betas.length - 1];
int deg = m_settings.getDegree();
for (int i = 0; i < m_columnNames.length; i++) {
for (int k = 0; k < deg; k++) {
preds[i * deg + k] = new NumericPredictor(m_columnNames[i], k + 1, m_betas[i * deg + k + 1]);
}
}
RegressionTable tab = new RegressionTable(m_betas[0], preds);
PMMLPortObjectSpec pmmlSpec = null;
if (inPMMLPort != null) {
pmmlSpec = inPMMLPort.getSpec();
}
PMMLPortObjectSpec spec = createModelSpec(pmmlSpec, inSpec);
/* To maintain compatibility with the previous SAX-based implementation.
* */
String targetField = "Response";
List<String> targetFields = spec.getTargetFields();
if (!targetFields.isEmpty()) {
targetField = targetFields.get(0);
}
PMMLPortObject outPMMLPort = new PMMLPortObject(spec, inPMMLPort, inSpec);
PMMLRegressionTranslator trans = new PMMLRegressionTranslator("KNIME Polynomial Regression", "PolynomialRegression", tab, targetField);
outPMMLPort.addModelTranslater(trans);
return outPMMLPort;
}
use of org.knime.core.node.port.pmml.PMMLPortObjectSpec in project knime-core by knime.
the class RegressionPredictorNodeModel method configure.
/**
* {@inheritDoc}
*/
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
PMMLPortObjectSpec regModelSpec = (PMMLPortObjectSpec) inSpecs[0];
DataTableSpec dataSpec = (DataTableSpec) inSpecs[1];
if (dataSpec == null || regModelSpec == null) {
throw new InvalidSettingsException("No input specification available");
}
for (String learnColName : regModelSpec.getLearningFields()) {
if (!dataSpec.containsName(learnColName)) {
throw new InvalidSettingsException("Learning column \"" + learnColName + "\" not found in input data to be predicted");
}
}
ColumnRearranger rearranger = createRearranger(dataSpec, regModelSpec, null);
DataTableSpec outSpec = rearranger.createSpec();
return new DataTableSpec[] { outSpec };
}
use of org.knime.core.node.port.pmml.PMMLPortObjectSpec in project knime-core by knime.
the class TreeEnsembleModelPortObject method createDecisionTreePMMLPortObject.
public PMMLPortObject createDecisionTreePMMLPortObject(final int modelIndex) {
final TreeEnsembleModel ensembleModel = getEnsembleModel();
DataTableSpec attributeLearnSpec = ensembleModel.getLearnAttributeSpec(m_spec.getLearnTableSpec());
DataColumnSpec targetSpec = m_spec.getTargetColumn();
PMMLPortObjectSpecCreator pmmlSpecCreator = new PMMLPortObjectSpecCreator(new DataTableSpec(attributeLearnSpec, new DataTableSpec(targetSpec)));
try {
pmmlSpecCreator.setLearningCols(attributeLearnSpec);
} catch (InvalidSettingsException e) {
// (as of KNIME v2.5.1)
throw new IllegalStateException(e);
}
pmmlSpecCreator.setTargetCol(targetSpec);
PMMLPortObjectSpec pmmlSpec = pmmlSpecCreator.createSpec();
PMMLPortObject portObject = new PMMLPortObject(pmmlSpec);
final AbstractTreeModel<?> model = ensembleModel.getTreeModel(modelIndex);
portObject.addModelTranslater(new TreeModelPMMLTranslator(model));
return portObject;
}
use of org.knime.core.node.port.pmml.PMMLPortObjectSpec in project knime-core by knime.
the class LearnerTest method testPerformLowBirthWeightData.
/**
* Test method for {@link org.knime.base.node.mine.regression.logistic.learner.Learner#perform(BufferedDataTable, org.knime.core.node.ExecutionContext)}.
* @throws CanceledExecutionException
*/
@Test
public final void testPerformLowBirthWeightData() throws Exception {
final BufferedDataTable data = m_exec.createBufferedDataTable(new LowBirthWeightData(), m_exec);
PMMLPortObjectSpecCreator specCreator = new PMMLPortObjectSpecCreator(data.getDataTableSpec());
specCreator.setLearningColsNames(Arrays.asList(new String[] { "AGE", "LWT", "RACE", "FTV" }));
specCreator.setTargetColName("LOW");
final PMMLPortObjectSpec spec = specCreator.createSpec();
// done in KNIME thread pool, expected by code
Future<LogisticRegressionContent> callable = KNIMEConstants.GLOBAL_THREAD_POOL.enqueue(new Callable<LogisticRegressionContent>() {
@Override
public LogisticRegressionContent call() throws Exception {
final Learner learner = new Learner(spec, null, true, true);
return learner.perform(data, m_exec);
}
});
LogisticRegressionContent content = callable.get();
// Reference results are published in the book:
// Applied Logistic Regression,
// David W. Hosmer and Stanley Lemeshow
// Wiley, 2000 (2nd. ed)
// The table of results are found on page 36
Assert.assertEquals(-111.286, content.getEstimatedLikelihood(), 0.001);
}
Aggregations