use of org.knime.core.node.port.pmml.PMMLPortObjectSpec in project knime-core by knime.
the class LinearRegressionContent method createPortObject.
/**
* Creates a new PMML regression port object from this linear regression
* model.
* @param inPMMLPort the incoming PMMLPort object (can be null)
* @param dts the full data table spec with which the regression
* model was created.
* @param learningSpec a data table spec containing only learning columns
* @return a port object
* @throws InvalidSettingsException if the settings are invalid
*/
public PMMLPortObject createPortObject(final PMMLPortObject inPMMLPort, final DataTableSpec dts, final DataTableSpec learningSpec) throws InvalidSettingsException {
PMMLPortObjectSpec inPMMLSpec = null;
if (inPMMLPort != null) {
inPMMLSpec = inPMMLPort.getSpec();
}
PMMLPortObjectSpec spec = createPortObjectSpec(inPMMLSpec, dts, learningSpec);
PMMLPortObject outPMMLPort = new PMMLPortObject(spec, inPMMLPort);
NumericPredictor[] nps = new NumericPredictor[m_multipliers.length];
for (int i = 0; i < nps.length; i++) {
nps[i] = new NumericPredictor(m_spec.getColumnSpec(i).getName(), 1, m_multipliers[i]);
}
RegressionTable regressionTable = new RegressionTable(m_offset, nps);
/* To maintain compatibility with the previous SAX-based implementation.
* */
String targetField = "Response";
List<String> targetFields = spec.getTargetFields();
if (!targetFields.isEmpty()) {
targetField = targetFields.get(0);
}
PMMLRegressionTranslator trans = new PMMLRegressionTranslator(MODEL_NAME, ALGORITHM_NAME, regressionTable, targetField);
outPMMLPort.addModelTranslater(trans);
return outPMMLPort;
}
use of org.knime.core.node.port.pmml.PMMLPortObjectSpec in project knime-core by knime.
the class RegressionTreePMMLPredictorNodeModel method configure.
/**
* {@inheritDoc}
*/
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
PMMLPortObjectSpec pmmlSpec = (PMMLPortObjectSpec) inSpecs[0];
DataType targetType = extractTargetType(pmmlSpec);
if (!targetType.isCompatible(DoubleValue.class)) {
throw new InvalidSettingsException("This node expects a regression model.");
}
try {
AbstractTreeModelPMMLTranslator.checkPMMLSpec(pmmlSpec);
} catch (IllegalArgumentException e) {
throw new InvalidSettingsException(e.getMessage());
}
RegressionTreeModelPortObjectSpec modelSpec = translateSpec(pmmlSpec);
String targetColName = modelSpec.getTargetColumn().getName();
if (m_configuration == null) {
m_configuration = RegressionTreePredictorConfiguration.createDefault(targetColName);
} else if (!m_configuration.isChangePredictionColumnName()) {
m_configuration.setPredictionColumnName(TreeEnsemblePredictorConfiguration.getPredictColumnName(targetColName));
}
DataTableSpec dataSpec = (DataTableSpec) inSpecs[1];
final RegressionTreePredictor pred = new RegressionTreePredictor(null, modelSpec, dataSpec, m_configuration);
return new PortObjectSpec[] { pred.getPredictionRearranger().createSpec() };
}
use of org.knime.core.node.port.pmml.PMMLPortObjectSpec in project knime-core by knime.
the class LogRegLearner method execute.
/**
* Compute logistic regression model.
*
* @param portObjects The input objects.
* @param exec the execution context
* @return a {@link LogisticRegressionContent} storing computed data
* @throws Exception if computation of the logistic regression model is
* not successful or if given data is inconsistent with the settings
* defined in the constructor.
* @see LogRegLearnerNodeModel#execute(PortObjectSpec[])
*/
public LogisticRegressionContent execute(final PortObject[] portObjects, final ExecutionContext exec) throws Exception {
BufferedDataTable data = (BufferedDataTable) portObjects[0];
PMMLPortObject inPMMLPort = (PMMLPortObject) portObjects[1];
PMMLPortObjectSpec inPMMLSpec = inPMMLPort.getSpec();
init(data.getDataTableSpec(), inPMMLSpec, Collections.<String>emptySet());
// the learner typically needs five steps with two runs over the data each step, calculating
// the domain needs run over the data
double calcDomainTime = 1.0 / (5.0 * 2.0 + 1.0);
exec.setMessage("Analyzing categorical data");
BufferedDataTable dataTable = recalcDomainForTargetAndLearningFields(data, inPMMLSpec, exec.createSubExecutionContext(calcDomainTime));
checkConstantLearningFields(dataTable, inPMMLSpec);
exec.setMessage("Building logistic regression model");
return m_learner.perform(dataTable, exec.createSubExecutionContext(1.0 - calcDomainTime));
}
use of org.knime.core.node.port.pmml.PMMLPortObjectSpec in project knime-core by knime.
the class LogisticRegressionContent method load.
/**
* @param parContent the content that holds the internals
* @param spec the data table spec of the training data
* @return a instance with he loaded values
* @throws InvalidSettingsException when data are not well formed
*/
static LogisticRegressionContent load(final ModelContentRO parContent, final DataTableSpec spec) throws InvalidSettingsException {
String target = parContent.getString(CFG_TARGET);
String[] learningCols = parContent.getStringArray(CFG_LEARNING_COLS);
PMMLPortObjectSpec pmmlSpec = createSpec(spec, target, learningCols);
String[] factors = parContent.getStringArray(CFG_FACTORS);
// Since 3.0
final String[] vectorColumns = parContent.getStringArray(CFG_VECTOR_NAMES);
final int[] vectorLengths = parContent.getIntArray(CFG_VECTOR_LENGTHS);
CheckUtils.checkSetting(vectorColumns.length == vectorLengths.length, "The length of vector names and their length do not match: " + vectorColumns.length + " <> " + vectorLengths.length);
Map<String, Integer> vectorLengthMap = new LinkedHashMap<>();
for (int i = 0; i < vectorLengths.length; i++) {
vectorLengthMap.put(vectorColumns[i], vectorLengths[i]);
}
String[] covariates = parContent.getStringArray(CFG_COVARIATES);
double[] coeff = parContent.getDoubleArray(CFG_COEFFICIENTS);
double likelihood = parContent.getDouble(CFG_LOG_LIKELIHOOD);
double[] covMat = parContent.getDoubleArray(CFG_COVARIANCE_MATRIX);
int iter = parContent.getInt(CFG_ITER);
// introduced in 2.9
DataCell targetReferenceCategory = parContent.getDataCell(CFG_TARGET_REFERENCE_CATEGORY, null);
boolean sortTargetCategories = parContent.getBoolean(CFG_SORT_TARGET_CATEGORIES, true);
boolean sortFactorsCategories = parContent.getBoolean(CFG_SORT_FACTORS_CATEGORIES, true);
return new LogisticRegressionContent(pmmlSpec, Arrays.asList(factors), Arrays.asList(covariates), vectorLengthMap, targetReferenceCategory, sortTargetCategories, sortFactorsCategories, toMatrix(coeff, coeff.length), likelihood, toMatrix(covMat, coeff.length), iter);
}
use of org.knime.core.node.port.pmml.PMMLPortObjectSpec in project knime-core by knime.
the class RegressionTreeModelPortObject method createDecisionTreePMMLPortObject.
public PMMLPortObject createDecisionTreePMMLPortObject() {
final RegressionTreeModel model = getModel();
DataTableSpec attributeLearnSpec = model.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 TreeModelRegression tree = model.getTreeModel();
portObject.addModelTranslater(new RegressionTreeModelPMMLTranslator(tree, model.getMetaData(), m_spec.getLearnTableSpec()));
return portObject;
}
Aggregations