zlibsvm is an object-oriented Java binding for the LIBSVM library. It wraps the cross-compiled Java code behind a clean API that can be easily integrated via Apache Maven.
- Java 21+
- Maven 3.9.0+
<dependency>
<groupId>de.hs-heilbronn.mi</groupId>
<artifactId>zlibsvm-core</artifactId>
<version>3.0.0</version>
</dependency>A UI-based example project is available at rzo1/zlibsvm-example.
zlibsvm uses the standard LIBSVM data format:
label feature_id1:feature_value1 feature_id2:feature_value2 ...
Each feature needs a unique integer identifier. For example, given three classes 1, 2, 3 and
features a(id=1), b(id=2), c(id=3):
2 1:0.5325 3:0.523
3 2:0.7853 3:0.6326
1 1:0.53265 2:0.5422
Features not present in a data point can simply be omitted.
You need to provide implementations of SvmDocument and SvmFeature:
public class SvmDocumentImpl implements SvmDocument {
private final List<SvmFeature> features;
private final List<SvmClassLabel> classLabels = new ArrayList<>();
public SvmDocumentImpl(List<SvmFeature> features) {
this.features = features;
}
public List<SvmFeature> getSvmFeatures() {
return features;
}
public SvmClassLabel getClassLabelWithHighestProbability() {
if (classLabels.isEmpty()) {
return null;
}
return Collections.max(classLabels);
}
@Override
public List<SvmClassLabel> getAllClassLabels() {
return Collections.unmodifiableList(classLabels);
}
@Override
public void addClassLabel(SvmClassLabel classLabel) {
assert (classLabel != null);
this.classLabels.add(classLabel);
}
}public record SvmFeatureImpl(int index, double value) implements SvmFeature {
public int getIndex() {
return index;
}
public double getValue() {
return value;
}
@Override
public int compareTo(SvmFeature o) {
return Integer.compare(getIndex(), o.getIndex());
}
}Create a trainer with an SvmConfigurationImpl.Builder and train a model.
The default configuration matches LIBSVM defaults: C-SVC with RBF kernel, gamma = 0, cost = 1.
SvmTrainer trainer = new SvmTrainerImpl(
new SvmConfigurationImpl.Builder().build(), "my-model");
SvmModel model = trainer.train(documentsForTraining);Use the trained model to classify new documents:
SvmClassifier classifier = new SvmClassifierImpl(model);
List<SvmDocument> classified = classifier.classify(documentsForPrediction, true);
for (SvmDocument d : classified) {
System.out.println(d + " was classified as category: "
+ d.getClassLabelWithHighestProbability().getNumeric());
}Published under Apache License 2.0