Search in sources :

Example 1 with VectorDictionary

use of org.dmg.pmml.VectorDictionaryDocument.VectorDictionary in project knime-core by knime.

the class PMMLSVMTranslator method addVectorDictionary.

/**
 * Adds the vector dictionary to the SVM model.
 *
 * @param svmModel the SVM model to add the dictionary to
 * @param learningCol a list with the names of the learning columns
 */
private void addVectorDictionary(final SupportVectorMachineModel svmModel, final List<String> learningCol) {
    VectorDictionary dict = svmModel.addNewVectorDictionary();
    Set<DoubleVector> supportVectors = new LinkedHashSet<DoubleVector>();
    for (Svm svm : m_svms) {
        supportVectors.addAll(Arrays.asList(svm.getSupportVectors()));
    }
    dict.setNumberOfVectors(BigInteger.valueOf(supportVectors.size()));
    VectorFields vectorFields = dict.addNewVectorFields();
    vectorFields.setNumberOfFields(BigInteger.valueOf(learningCol.size()));
    for (String field : learningCol) {
        vectorFields.addNewFieldRef().setField(m_nameMapper.getDerivedFieldName(field));
    }
    for (DoubleVector vector : supportVectors) {
        VectorInstance vectorInstance = dict.addNewVectorInstance();
        vectorInstance.setId(vector.getClassValue() + CLASS_KEY_SEPARATOR + vector.getKey().getString());
        REALSparseArray pmmlRealSparseArray = vectorInstance.addNewREALSparseArray1();
        int nrValues = vector.getNumberValues();
        pmmlRealSparseArray.setN(BigInteger.valueOf(nrValues));
        // set Indices and Entries
        List<String> indicesList = new ArrayList<String>();
        List<String> entriesList = new ArrayList<String>();
        for (int i = 1; i <= nrValues; i++) {
            indicesList.add(String.valueOf(i));
            entriesList.add(String.valueOf(vector.getValue(i - 1)));
        }
        pmmlRealSparseArray.setIndices(indicesList);
        pmmlRealSparseArray.setREALEntries(entriesList);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) VectorInstance(org.dmg.pmml.VectorInstanceDocument.VectorInstance) VectorDictionary(org.dmg.pmml.VectorDictionaryDocument.VectorDictionary) REALSparseArray(org.dmg.pmml.REALSparseArrayDocument.REALSparseArray) VectorFields(org.dmg.pmml.VectorFieldsDocument.VectorFields) ArrayList(java.util.ArrayList) DoubleVector(org.knime.base.node.mine.svm.util.DoubleVector)

Aggregations

ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1 REALSparseArray (org.dmg.pmml.REALSparseArrayDocument.REALSparseArray)1 VectorDictionary (org.dmg.pmml.VectorDictionaryDocument.VectorDictionary)1 VectorFields (org.dmg.pmml.VectorFieldsDocument.VectorFields)1 VectorInstance (org.dmg.pmml.VectorInstanceDocument.VectorInstance)1 DoubleVector (org.knime.base.node.mine.svm.util.DoubleVector)1