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));
}
}
}
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;
}
Aggregations