use of org.knime.core.node.port.pmml.PMMLPortObject in project knime-core by knime.
the class TreeEnsembleShrinkerNodeModel method execute.
@Override
protected PortObject[] execute(final PortObject[] inObjects, final ExecutionContext exec) throws Exception {
TreeEnsembleModel treeEnsemble = ((TreeEnsembleModelPortObject) inObjects[0]).getEnsembleModel();
TreeEnsembleModelPortObject resultEnsemble;
int resultSize = m_config.getResultSize(treeEnsemble.getNrModels());
boolean shrink = true;
if (!m_config.isResultSizeAutomatic()) {
// Check if result size is valid
if (resultSize < 1) {
// Result size is to small, use 1
setWarningMessage("The configured result size is smaller than 1, defaulting to 1");
resultSize = 1;
} else if (resultSize > treeEnsemble.getNrModels()) {
// Result size is to big, just keep current ensemble
setWarningMessage("The configured result size is bigger than the size of the input ensemble, defaulting to the input ensembles size");
shrink = false;
} else if (resultSize == treeEnsemble.getNrModels()) {
// Result size is ensemble size -> we don't need to shrink
shrink = false;
}
}
// If our result size is not smaller than the current ensemble we don't have to do the following and therefore can save time
if (shrink) {
BufferedDataTable inData = (BufferedDataTable) inObjects[1];
// Create shrinker
TreeEnsembleShrinker shrinker = new TreeEnsembleShrinker(treeEnsemble, inData, m_config.getTargetColumn(), exec);
// Shrink ensemble
if (m_config.isResultSizeAutomatic()) {
shrinker.autoShrink();
} else {
shrinker.shrinkTo(resultSize);
}
// Get shrunk ensemble
TreeEnsembleModel newEnsemble = shrinker.getModel();
// Push flow variable with archived accuracy
pushFlowVariableDouble("Tree Ensemble Shrinker Prediction Accuracy", shrinker.getAccuracy());
// Create port object for tree ensemble
resultEnsemble = new TreeEnsembleModelPortObject(((TreeEnsembleModelPortObject) inObjects[0]).getSpec(), newEnsemble);
} else {
// We did not need to shrink just use input tree ensemble port object
resultEnsemble = (TreeEnsembleModelPortObject) inObjects[0];
}
// Convert tree ensemble port object to PMML
PMMLPortObject pmmlEnsemble = convertToPmmlEnsemble(resultEnsemble, exec);
return new PortObject[] { pmmlEnsemble };
}
use of org.knime.core.node.port.pmml.PMMLPortObject in project knime-core by knime.
the class LogRegLearnerNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inObjects, final ExecutionContext exec) throws Exception {
final BufferedDataTable data = (BufferedDataTable) inObjects[0];
final DataTableSpec tableSpec = data.getDataTableSpec();
final LogRegCoordinator coordinator = new LogRegCoordinator(tableSpec, m_settings);
m_content = coordinator.learn(data, exec);
String warn = coordinator.getWarningMessage();
if (warn != null) {
setWarningMessage(warn);
}
PMMLPortObject outPMMLPort = new PMMLPortObject((PMMLPortObjectSpec) coordinator.getOutputSpecs()[0], null, tableSpec);
PMMLGeneralRegressionTranslator trans = new PMMLGeneralRegressionTranslator(m_content.createGeneralRegressionContent());
outPMMLPort.addModelTranslater(trans);
return new PortObject[] { outPMMLPort, m_content.createCoeffStatisticsTablePortObject(exec), m_content.createModelStatisticsTable(exec) };
}
use of org.knime.core.node.port.pmml.PMMLPortObject in project knime-core by knime.
the class SVMLearnerNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
BufferedDataTable inTable = (BufferedDataTable) inData[0];
DataTableSpec inSpec = inTable.getDataTableSpec();
LearnColumnsAndColumnRearrangerTuple tuple = createTrainTableColumnRearranger(inSpec);
// no progress needed as constant operation (column removal only)
BufferedDataTable trainTable = exec.createColumnRearrangeTable(inTable, tuple.getTrainingRearranger(), exec.createSubProgress(0.0));
DataTableSpec trainSpec = trainTable.getDataTableSpec();
int classpos = trainSpec.findColumnIndex(m_classcol.getStringValue());
CheckUtils.checkArgument(classpos >= 0, "Selected class column not found: " + m_classcol.getStringValue());
// convert input data
ArrayList<DoubleVector> inputData = new ArrayList<DoubleVector>();
List<String> categories = new ArrayList<String>();
StringValue classvalue = null;
for (DataRow row : trainTable) {
exec.checkCanceled();
ArrayList<Double> values = new ArrayList<Double>();
boolean add = true;
for (int i = 0; i < row.getNumCells(); i++) {
if (row.getCell(i).isMissing()) {
add = false;
break;
}
if (i != classpos) {
DoubleValue cell = (DoubleValue) row.getCell(i);
values.add(cell.getDoubleValue());
} else {
classvalue = (StringValue) row.getCell(classpos);
if (!categories.contains(classvalue.getStringValue())) {
categories.add(classvalue.getStringValue());
}
}
}
if (add) {
@SuppressWarnings("null") final String nonNullClassValue = classvalue.getStringValue();
inputData.add(new DoubleVector(row.getKey(), values, nonNullClassValue));
}
}
if (categories.isEmpty()) {
throw new Exception("No categories found to train SVM. " + "Possibly an empty input table was provided.");
}
DoubleVector[] inputDataArr = new DoubleVector[inputData.size()];
inputDataArr = inputData.toArray(inputDataArr);
Kernel kernel = KernelFactory.getKernel(m_kernelType);
Vector<SettingsModelDouble> kernelparams = m_kernelParameters.get(m_kernelType);
for (int i = 0; i < kernel.getNumberParameters(); ++i) {
kernel.setParameter(i, kernelparams.get(i).getDoubleValue());
}
final Svm[] svms = new Svm[categories.size()];
exec.setMessage("Training SVM");
final BinarySvmRunnable[] bst = new BinarySvmRunnable[categories.size()];
for (int i = 0; i < categories.size(); i++) {
bst[i] = new BinarySvmRunnable(inputDataArr, categories.get(i), kernel, m_paramC.getDoubleValue(), exec.createSubProgress((1.0 / categories.size())));
}
ThreadPool pool = KNIMEConstants.GLOBAL_THREAD_POOL;
final Future<?>[] fut = new Future<?>[bst.length];
KNIMETimer timer = KNIMETimer.getInstance();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
try {
exec.checkCanceled();
} catch (final CanceledExecutionException ce) {
for (int i = 0; i < fut.length; i++) {
if (fut[i] != null) {
fut[i].cancel(true);
}
}
super.cancel();
}
}
};
timer.scheduleAtFixedRate(timerTask, 0, 3000);
for (int i = 0; i < bst.length; i++) {
fut[i] = pool.enqueue(bst[i]);
}
try {
pool.runInvisible(new Callable<Void>() {
@Override
public Void call() throws Exception {
for (int i = 0; i < fut.length; ++i) {
fut[i].get();
bst[i].ok();
if (bst[i].getWarning() != null) {
setWarningMessage(bst[i].getWarning());
}
svms[i] = bst[i].getSvm();
}
return null;
}
});
} catch (Exception ex) {
exec.checkCanceled();
Throwable t = ex;
if (ex instanceof ExecutionException) {
t = ex.getCause();
}
if (t instanceof Exception) {
throw (Exception) t;
} else {
throw new Exception(t);
}
} finally {
for (int i = 0; i < fut.length; i++) {
fut[i].cancel(true);
}
timerTask.cancel();
}
// the optional PMML input (can be null)
PMMLPortObject inPMMLPort = m_pmmlInEnabled ? (PMMLPortObject) inData[1] : null;
// create the outgoing PMML spec
PMMLPortObjectSpecCreator specCreator = new PMMLPortObjectSpecCreator(inPMMLPort, inSpec);
specCreator.setLearningCols(trainSpec);
specCreator.setTargetCol(trainSpec.getColumnSpec(m_classcol.getStringValue()));
// create the outgoing PMML port object
PMMLPortObject outPMMLPort = new PMMLPortObject(specCreator.createSpec(), inPMMLPort, inSpec);
outPMMLPort.addModelTranslater(new PMMLSVMTranslator(categories, Arrays.asList(svms), kernel));
m_svms = svms;
return new PortObject[] { outPMMLPort };
}
use of org.knime.core.node.port.pmml.PMMLPortObject in project knime-core by knime.
the class RegressionPredictorNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
public PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
PMMLPortObject port = (PMMLPortObject) inData[0];
List<Node> models = port.getPMMLValue().getModels(PMMLModelType.GeneralRegressionModel);
if (models.isEmpty()) {
// KNIME currently (Sep '17) still uses the 'Regression' Model Type in the PolyReg learner
@SuppressWarnings("deprecation") org.knime.base.node.mine.regression.predict.RegressionPredictorNodeModel regrPredictor = new org.knime.base.node.mine.regression.predict.RegressionPredictorNodeModel();
@SuppressWarnings("deprecation") PortObject[] regrPredOut = regrPredictor.execute(inData, exec);
if (regrPredOut.length > 0 && regrPredOut[0] instanceof BufferedDataTable) {
BufferedDataTable regrPredOutTable = (BufferedDataTable) regrPredOut[0];
// replace name of prediction column (the last column of regrPredOutTable)
return new PortObject[] { adjustSpecOfRegressionPredictorTable(regrPredOutTable, inData, exec) };
} else {
return regrPredOut;
}
}
PMMLGeneralRegressionTranslator trans = new PMMLGeneralRegressionTranslator();
port.initializeModelTranslator(trans);
BufferedDataTable data = (BufferedDataTable) inData[1];
DataTableSpec spec = data.getDataTableSpec();
ColumnRearranger c = createRearranger(trans.getContent(), port.getSpec(), spec);
BufferedDataTable out = exec.createColumnRearrangeTable(data, c, exec);
return new BufferedDataTable[] { out };
}
use of org.knime.core.node.port.pmml.PMMLPortObject in project knime-core by knime.
the class NaiveBayesLearnerNodeModel2 method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws CanceledExecutionException, InvalidSettingsException {
LOGGER.debug("Entering execute of " + NaiveBayesLearnerNodeModel2.class.getName());
assert (inData != null && ((inData.length == 2 && m_pmmlInEnabled) || (inData.length == 1 && !m_pmmlInEnabled)) && inData[TRAINING_DATA_PORT] != null);
final PortObject inObject = inData[TRAINING_DATA_PORT];
if (!(inObject instanceof BufferedDataTable)) {
throw new IllegalArgumentException("Invalid input data");
}
final BufferedDataTable trainingTable = (BufferedDataTable) inObject;
final boolean ignoreMissingVals = m_ignoreMissingVals.getBooleanValue();
final boolean pmmlCompatible = m_pmmlCompatible.getBooleanValue();
final int maxNoOfNomVals = m_maxNoOfNominalVals.getIntValue();
m_model = new NaiveBayesModel(trainingTable, m_classifyColumnName.getStringValue(), exec, maxNoOfNomVals, ignoreMissingVals, pmmlCompatible, m_threshold.getDoubleValue());
final List<String> missingModels = m_model.getAttributesWithMissingVals();
if (missingModels.size() > 0) {
final StringBuilder buf = new StringBuilder();
buf.append("The following attributes contain missing values: ");
for (int i = 0, length = missingModels.size(); i < length; i++) {
if (i != 0) {
buf.append(", ");
}
if (i > 3) {
buf.append("...(see View)");
break;
}
buf.append(missingModels.get(i));
}
setWarningMessage(buf.toString());
}
if (m_model.containsSkippedAttributes()) {
setWarningMessage(m_model.getSkippedAttributesString(3));
}
LOGGER.debug("Exiting execute of " + NaiveBayesLearnerNodeModel2.class.getName());
// handle the optional PMML input
final PMMLPortObject inPMMLPort = m_pmmlInEnabled ? (PMMLPortObject) inData[MODEL_INPORT] : null;
final DataTableSpec tableSpec = trainingTable.getSpec();
final PMMLPortObjectSpec outPortSpec = createPMMLSpec(tableSpec, inPMMLPort == null ? null : inPMMLPort.getSpec(), m_model.getPMMLLearningCols(), m_model.getClassColumnName());
final PMMLPortObject outPMMLPort = new PMMLPortObject(outPortSpec, inPMMLPort, tableSpec);
outPMMLPort.addModelTranslater(new PMMLNaiveBayesModelTranslator(m_model));
return new PortObject[] { outPMMLPort, m_model.getStatisticsTable() };
}
Aggregations