Search in sources :

Example 11 with PMMLPortObjectSpecCreator

use of org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator 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;
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) PMMLPortObjectSpecCreator(org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)

Example 12 with PMMLPortObjectSpecCreator

use of org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator 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);
}
Also used : PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) BufferedDataTable(org.knime.core.node.BufferedDataTable) PMMLPortObjectSpecCreator(org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) Test(org.junit.Test)

Example 13 with PMMLPortObjectSpecCreator

use of org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator in project knime-core by knime.

the class LearnerTest method testPerformChdAgeData.

/**
 * 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 testPerformChdAgeData() throws Exception {
    final BufferedDataTable data = m_exec.createBufferedDataTable(new ChdAgeData(), m_exec);
    PMMLPortObjectSpecCreator specCreator = new PMMLPortObjectSpecCreator(data.getDataTableSpec());
    specCreator.setLearningColsNames(Arrays.asList(new String[] { "Age" }));
    specCreator.setTargetColName("Evidence of Coronary Heart Disease");
    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 10
    Assert.assertEquals(-53.67656, content.getEstimatedLikelihood(), 0.001);
}
Also used : PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) BufferedDataTable(org.knime.core.node.BufferedDataTable) PMMLPortObjectSpecCreator(org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) Test(org.junit.Test)

Example 14 with PMMLPortObjectSpecCreator

use of org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator in project knime-core by knime.

the class LogisticRegressionContent method createSpec.

private static PMMLPortObjectSpec createSpec(final DataTableSpec spec, final String target, final String[] learningCols) {
    PMMLPortObjectSpecCreator c = new PMMLPortObjectSpecCreator(spec);
    c.setTargetColName(target);
    c.setLearningColsNames(Arrays.asList(learningCols));
    return c.createSpec();
}
Also used : PMMLPortObjectSpecCreator(org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)

Example 15 with PMMLPortObjectSpecCreator

use of org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator 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 };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) PMMLSVMTranslator(org.knime.base.node.mine.svm.PMMLSVMTranslator) ArrayList(java.util.ArrayList) ThreadPool(org.knime.core.util.ThreadPool) SettingsModelDouble(org.knime.core.node.defaultnodesettings.SettingsModelDouble) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) Svm(org.knime.base.node.mine.svm.Svm) DataRow(org.knime.core.data.DataRow) TimerTask(java.util.TimerTask) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) BufferedDataTable(org.knime.core.node.BufferedDataTable) StringValue(org.knime.core.data.StringValue) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) ExecutionException(java.util.concurrent.ExecutionException) Kernel(org.knime.base.node.mine.svm.kernel.Kernel) PortObject(org.knime.core.node.port.PortObject) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) KNIMETimer(org.knime.core.util.KNIMETimer) BinarySvmRunnable(org.knime.base.node.mine.svm.util.BinarySvmRunnable) SettingsModelDouble(org.knime.core.node.defaultnodesettings.SettingsModelDouble) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) DoubleValue(org.knime.core.data.DoubleValue) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) Future(java.util.concurrent.Future) DoubleVector(org.knime.base.node.mine.svm.util.DoubleVector) PMMLPortObjectSpecCreator(org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)

Aggregations

PMMLPortObjectSpecCreator (org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)62 DataTableSpec (org.knime.core.data.DataTableSpec)35 PMMLPortObjectSpec (org.knime.core.node.port.pmml.PMMLPortObjectSpec)24 DataColumnSpec (org.knime.core.data.DataColumnSpec)21 PMMLPortObject (org.knime.core.node.port.pmml.PMMLPortObject)21 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)19 BufferedDataTable (org.knime.core.node.BufferedDataTable)15 PortObjectSpec (org.knime.core.node.port.PortObjectSpec)14 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)13 PortObject (org.knime.core.node.port.PortObject)13 DerivedFieldMapper (org.knime.core.node.port.pmml.preproc.DerivedFieldMapper)11 ArrayList (java.util.ArrayList)10 DoubleValue (org.knime.core.data.DoubleValue)10 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)9 LinkedList (java.util.LinkedList)6 SettingsModelFilterString (org.knime.core.node.defaultnodesettings.SettingsModelFilterString)6 HashSet (java.util.HashSet)4 LinkedHashSet (java.util.LinkedHashSet)4 DataCell (org.knime.core.data.DataCell)3 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)3