use of org.dmg.pmml.VectorFieldsDocument.VectorFields 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);
}
}
Aggregations