The C++ API of bob.learn.libsvm allows users to leverage from automatic converters for classes in bob.learn.libsvm. To use the C API, clients should first, include the header file <bob.learn.libsvm/api.h> on their compilation units and then, make sure to call once import_bob_learn_libsvm() at their module instantiation, as explained at the Python manual.
Here is a dummy C example showing how to include the header and where to call the import function:
#include <bob.blitz/capi.h>
#include <bob.io/api.h>
#include <bob.learn.libsvm/api.h>
PyMODINIT_FUNC initclient(void) {
PyObject* m Py_InitModule("client", ClientMethods);
if (!m) return 0;
if (import_bob_blitz() < 0) return 0;
if (import_bob_io() < 0) return 0;
if (import_bob_learn_libsvm() < 0) return 0;
return m;
}
Note
The include directory can be discovered using bob.learn.libsvm.get_include().
The pythonic object representation for a bob::learn::libsvm::File object.
typedef struct {
PyObject_HEAD
bob::learn::libsvm::File* cxx;
} PyBobLearnLibsvmFileObject
A pointer to the C++ file implementation.
Checks if the input object o is a PyBobLearnLibsvmFileObject. Returns 1 if it is, and 0 otherwise.
The pythonic object representation for a bob::learn::libsvm::Machine object.
typedef struct {
PyObject_HEAD
bob::learn::libsvm::Machine* cxx;
} PyBobLearnLibsvmMachineObject
A pointer to the C++ machine implementation.
Checks if the input object o is a PyBobLearnLibsvmMachineObject. Returns 1 if it is, and 0 otherwise.
Builds a new Python object from an existing Machine. The machine object m is stolen from the user, which should not delete it anymore.
The pythonic object representation for a bob::learn::libsvm::Trainer object.
typedef struct {
PyObject_HEAD
bob::learn::libsvm::Trainer* cxx;
} PyBobLearnLibsvmTrainerObject
A pointer to the C++ trainer implementation.
Checks if the input object o is a PyBobLearnLibsvmTrainerObject. Returns 1 if it is, and 0 otherwise.
Returns a Python string representing given a machine type. Returns NULL and sets an RuntimeError if the enumeration provided is not supported.
This function will return a proper PyStringObject on Python 2.x and a PyUnicodeObject on Python 3.x.
Decodes the machine type enumeration from a pythonic string. Works with any string type or subtype. A RuntimeError is set if the string cannot be encoded as one of the available enumerations. You must check for PyErr_Occurred() after a call to this function to make sure that the conversion was correctly performed.
This function works the same as PyBobLearnLibsvm_StringAsMachineType(), but accepts a C-style string instead of a Python object as input. A RuntimeError is set if the string cannot be encoded as one of the available enumerations. You must check for PyErr_Occurred() after a call to this function to make sure that the conversion was correctly performed.
Returns a Python string representing given a kernel type. Returns NULL and sets an RuntimeError if the enumeration provided is not supported.
This function will return a proper PyStringObject on Python 2.x and a PyUnicodeObject on Python 3.x.
Decodes the kernel type enumeration from a pythonic string. Works with any string type or subtype. A RuntimeError is set if the string cannot be encoded as one of the available enumerations. You must check for PyErr_Occurred() after a call to this function to make sure that the conversion was correctly performed.
This function works the same as PyBobLearnLibsvm_StringAsKernelType(), but accepts a C-style string instead of a Python object as input. A RuntimeError is set if the string cannot be encoded as one of the available enumerations. You must check for PyErr_Occurred() after a call to this function to make sure that the conversion was correctly performed.
As explained above, each PyObject produced by this library contains a pointer to a pure C++ implementation of a similar object. The C++ of such objects is described in this section.
Enumeration defining the types of SVM’s available within this implementation. The following are legal values:
Enumeration defining the types of kernels available within this implementation. The following are legal values:
Loads a given libsvm data file. The data file format, as defined on the library README is like this:
[label] [index1]:[value1] [index2]:[value2] ... [label] [index1]:[value1] [index2]:[value2] ... [label] [index1]:[value1] [index2]:[value2] ...
The labels are integer values, so are the indexes, starting from “1” (and not from zero as a C-programmer would expect. The values are floating point.
Zero values are suppressed - this is a sparse format.
Constructor, initializes the file readout.
Virtualized destructor
Returns the size of each entry in the file, in number of floats
Returns the number of samples in the file.
Resets the file, going back to the beginning.
Reads the next entry. Values are organized according to the indexed labels at the file. Returns ‘false’ if the file is over or something goes wrong.
Reads the next entry on the file, but without checking. Returns ‘false’ if the file is over or something goes wrong reading the file.
Returns the name of the file being read.
Tests if the file is still good to go.
Interface to svm_model, from LIBSVM. Incorporates prediction.
Builds a new Support Vector Machine from a LIBSVM model file.
When you load using the libsvm model loader, note that the scaling parameters will be set to defaults (subtraction of 0.0 and division by 1.0). If you need scaling to be applied, set it individually using the appropriate methods bellow.
Builds a new Support Vector Machine from an HDF5 file containing the configuration for this machine. Scaling parameters are also loaded from the file. Using this constructor assures a 100% state recovery from previous sessions.
Builds a new SVM model from a trained model. Scaling parameters will be neutral (subtraction := 0.0, division := 1.0).
Note
This method is typically only used by the respective :cpp:class`bob::learn::libsvm::Trainer` as it requires the creation of the object svm_model. You can still make use of it if you decide to implement the model instantiation yourself.
Virtual d’tor
Tells the input size this machine expects
The number of outputs depends on the number of classes the machine has to deal with. If the problem is a regression problem, the number of outputs is fixed to 1. The same happens in a binary classification problem. Otherwise, the output size is the same as the number of classes being discriminated.
Tells the number of classes the problem has.
Returns the class label (as stored inside the svm_model object) for a given class ‘i’.
The SVM type
Kernel type
Polinomial degree, if kernel is POLY
factor, for POLY, RBF or SIGMOID kernels
Coefficient 0 for POLY and SIGMOID kernels
Tells if this model supports probability output.
Returns the input subtraction factor
Sets the current input subtraction factor. We will check that the number of inputs (first dimension of weights) matches the number of values currently set and will raise an exception if that is not the case.
Sets all input subtraction values to a specific value.
Returns the input division factor
Sets the current input division factor. We will check that the number of inputs (first dimension of weights) matches the number of values currently set and will raise an exception if that is not the case.
Sets all input division values to a specific value.
Predict, output classes only. Note that the number of labels in the output “labels” array should be the same as the number of input.
Predict, output classes only. Note that the number of labels in the output “labels” array should be the same as the number of input.
This does the same as predictClass(), but does not check the input.
Predicts class and scores output for each class on this SVM,
Note
The output array must be lying on contiguous memory. This is also checked.
Predicts output class and scores. Same as above, but does not check
Predict, output class and probabilities for each class on this SVM, but only if the model supports it. Otherwise, throws a run-time exception.
Note
The output array must be lying on contiguous memory. This is also checked.
Predicts, output class and probability, but only if the model supports it. Same as above, but does not check
Saves the current model state to a file. With this variant, the model is saved on simpler libsvm model file that does not include the scaling parameters set on this machine.
Saves the whole machine into a configuration file. This allows for a single instruction parameter loading, which includes both the model and the scaling parameters.
This class emulates the behavior of the command line utility called svm-train, from LIBSVM. These bindings do not support:
Fell free to implement those and remove these remarks.
Todo
Support for weight cost in multi-class classification?
Builds a new trainer setting the default parameters as defined in the command line application svm-train.
Destructor virtualisation
Trains a new machine for multi-class classification. If the number of classes in data is 2, then the assigned labels will be -1 and +1. If the number of classes is greater than 2, labels are picked starting from 1 (i.e., 1, 2, 3, 4, etc.). If what you want is regression, the size of the input data array should be 1.
Returns a new object you must deallocate yourself.
This version accepts scaling parameters that will be applied column-wise to the input data.
Returns a new object you must deallocate yourself.