Search in sources :

Example 6 with DoubleVector

use of org.knime.base.node.mine.svm.util.DoubleVector in project knime-core by knime.

the class PMMLSVMTranslator method initSVMs.

/**
 * @param svmModel
 */
private void initSVMs(final SupportVectorMachineModel svmModel) {
    final Map<String, ArrayList<Double>> vectors = new LinkedHashMap<String, ArrayList<Double>>();
    for (VectorInstance vectorInstance : svmModel.getVectorDictionary().getVectorInstanceArray()) {
        REALSparseArray sparseArray = vectorInstance.getREALSparseArray1();
        ArrayList<Double> values = new ArrayList<Double>();
        for (Object realEntry : sparseArray.getREALEntries()) {
            values.add((Double) realEntry);
        }
        vectors.put(vectorInstance.getId(), values);
    }
    for (SupportVectorMachine supportVectorMachine : svmModel.getSupportVectorMachineArray()) {
        // collect support vectors
        SupportVectors svs = supportVectorMachine.getSupportVectors();
        DoubleVector[] supportVectors = new DoubleVector[svs.getNumberOfSupportVectors().intValue()];
        SupportVector[] supportVectorArray = svs.getSupportVectorArray();
        for (int i = 0; i < supportVectorArray.length; i++) {
            SupportVector supportVector = supportVectorArray[i];
            String vectorId = supportVector.getVectorId();
            String classValue = getClassValue(vectorId);
            supportVectors[i] = new DoubleVector(new RowKey(vectorId), vectors.get(vectorId), classValue);
        }
        Coefficients coef = supportVectorMachine.getCoefficients();
        double threshold = coef.getAbsoluteValue();
        // collect coefficients
        Coefficient[] coefficientArray = coef.getCoefficientArray();
        double[] alpha = new double[coefficientArray.length];
        for (int i = 0; i < coefficientArray.length; i++) {
            /**
             * The alpha in KNIME is always positive. When calculating the
             * distance it is multiplied with a target factor that is 1 or
             * -1 depending on whether we have a positive example or not.
             * (see {@link SVM#getTargetAlphas()} for details)
             */
            alpha[i] = Math.abs(coefficientArray[i].getValue());
        }
        m_svms.add(new Svm(supportVectors, alpha, supportVectorMachine.getTargetCategory(), threshold, m_kernel));
        /* The KNIME internal representation requires two SVMs for the
             * binary classification case. Therefore add a second SVM with the
             * same configuration as the first one except for the negative
             * threshold. */
        if (svmModel.getSupportVectorMachineArray().length == 1) {
            m_svms.add(new Svm(supportVectors.clone(), alpha, supportVectorMachine.getAlternateTargetCategory(), threshold * -1, m_kernel));
        }
    }
}
Also used : Coefficient(org.dmg.pmml.CoefficientDocument.Coefficient) SupportVectors(org.dmg.pmml.SupportVectorsDocument.SupportVectors) RowKey(org.knime.core.data.RowKey) REALSparseArray(org.dmg.pmml.REALSparseArrayDocument.REALSparseArray) ArrayList(java.util.ArrayList) SupportVectorMachine(org.dmg.pmml.SupportVectorMachineDocument.SupportVectorMachine) Coefficients(org.dmg.pmml.CoefficientsDocument.Coefficients) SupportVector(org.dmg.pmml.SupportVectorDocument.SupportVector) LinkedHashMap(java.util.LinkedHashMap) VectorInstance(org.dmg.pmml.VectorInstanceDocument.VectorInstance) DoubleVector(org.knime.base.node.mine.svm.util.DoubleVector)

Example 7 with DoubleVector

use of org.knime.base.node.mine.svm.util.DoubleVector in project knime-core by knime.

the class SVMLearnerNodeModel method getSVMInfos.

/**
 * @return a string containing all SVM infos in HTML for the view.
 */
String getSVMInfos() {
    if (!m_svmInfo.isEmpty()) {
        return m_svmInfo;
    }
    StringBuilder sb = new StringBuilder();
    // avoid NPE when reset is called during view update
    Svm[] svms = m_svms;
    if (svms != null) {
        sb.append("<html>\n");
        sb.append("<body>\n");
        for (int i = 0; i < svms.length; i++) {
            if (svms[i] != null) {
                sb.append("<h1> SVM " + i + " Class: " + svms[i].getPositive() + "</h1>");
                sb.append("<b> Support Vectors: </b><br>");
                DoubleVector[] supvecs = svms[i].getSupportVectors();
                for (DoubleVector vec : supvecs) {
                    for (int s = 0; s < vec.getNumberValues(); s++) {
                        sb.append(vec.getValue(s) + ", ");
                    }
                    sb.append(vec.getClassValue() + "<br>");
                }
            }
        }
        sb.append("</body>\n");
        sb.append("</html>\n");
    }
    m_svmInfo = sb.toString();
    return m_svmInfo;
}
Also used : Svm(org.knime.base.node.mine.svm.Svm) DoubleVector(org.knime.base.node.mine.svm.util.DoubleVector)

Aggregations

DoubleVector (org.knime.base.node.mine.svm.util.DoubleVector)7 ArrayList (java.util.ArrayList)3 Coefficient (org.dmg.pmml.CoefficientDocument.Coefficient)2 Coefficients (org.dmg.pmml.CoefficientsDocument.Coefficients)2 REALSparseArray (org.dmg.pmml.REALSparseArrayDocument.REALSparseArray)2 SupportVector (org.dmg.pmml.SupportVectorDocument.SupportVector)2 SupportVectorMachine (org.dmg.pmml.SupportVectorMachineDocument.SupportVectorMachine)2 SupportVectors (org.dmg.pmml.SupportVectorsDocument.SupportVectors)2 VectorInstance (org.dmg.pmml.VectorInstanceDocument.VectorInstance)2 Svm (org.knime.base.node.mine.svm.Svm)2 IOException (java.io.IOException)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 TimerTask (java.util.TimerTask)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 VectorDictionary (org.dmg.pmml.VectorDictionaryDocument.VectorDictionary)1 VectorFields (org.dmg.pmml.VectorFieldsDocument.VectorFields)1 PMMLSVMTranslator (org.knime.base.node.mine.svm.PMMLSVMTranslator)1 Kernel (org.knime.base.node.mine.svm.kernel.Kernel)1