PLSSVM - Parallel Least Squares Support Vector Machine  2.0.0
A Least Squares Support Vector Machine implementation using different backends.
Public Types | Public Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
plssvm::model< T, U > Class Template Reference

Implements a class encapsulating the result of a call to the SVM fit function. A model is used to predict the labels of a new data set. More...

#include <model.hpp>

Collaboration diagram for plssvm::model< T, U >:
[legend]

Public Types

using real_type = T
 The type of the data points: either float or double.
 
using label_type = U
 The type of the labels: any arithmetic type or std::string.
 
using size_type = std::size_t
 The unsigned size type.
 

Public Member Functions

 model (const std::string &filename)
 Read a previously learned model from the LIBSVM model file filename. More...
 
void save (const std::string &filename) const
 Save the model to a LIBSVM model file for later usage. More...
 
size_type num_support_vectors () const noexcept
 The number of support vectors used in this model. More...
 
size_type num_features () const noexcept
 The number of features of the support vectors used in this model. More...
 
const parameterget_params () const noexcept
 Return the SVM parameter that were used to learn this model. More...
 
const std::vector< std::vector< real_type > > & support_vectors () const noexcept
 The support vectors representing the learned model. More...
 
const std::vector< label_type > & labels () const noexcept
 Returns the labels of the support vectors. More...
 
size_type num_different_labels () const noexcept
 Returns the number of different labels in this data set. More...
 
std::vector< label_typedifferent_labels () const
 Returns the different labels of the support vectors. More...
 
const std::vector< real_type > & weights () const noexcept
 The learned weights for the support vectors. More...
 
real_type rho () const noexcept
 The bias value after learning. More...
 

Private Member Functions

 model (parameter params, data_set< real_type, label_type > data)
 Create a new model using the SVM parameter params and the data. More...
 

Private Attributes

parameter params_ {}
 The SVM parameter used to learn this model.
 
data_set< real_type, label_typedata_ {}
 The data (support vectors + respective label) used to learn this model.
 
size_type num_support_vectors_ { 0 }
 The number of support vectors representing this model.
 
size_type num_features_ { 0 }
 The number of features per support vector.
 
std::shared_ptr< std::vector< real_type > > alpha_ptr_ { nullptr }
 The learned weights for each support vector.
 
real_type rho_ { 0.0 }
 The bias after learning this model.
 
std::shared_ptr< std::vector< real_type > > w_ { std::make_shared<std::vector<real_type>>() }
 A vector used to speedup the prediction in case of the linear kernel function. More...
 

Friends

class csvm
 

Detailed Description

template<typename T, typename U = int>
class plssvm::model< T, U >

Implements a class encapsulating the result of a call to the SVM fit function. A model is used to predict the labels of a new data set.

Template Parameters
Tthe floating point type of the data (must either be float or double)
Uthe type of the used labels (must be an arithmetic type or std:string; default: int)
Examples
csvm_examples.cpp, and model_examples.cpp.

Constructor & Destructor Documentation

◆ model() [1/2]

template<typename T , typename U >
plssvm::model< T, U >::model ( const std::string &  filename)
explicit

Read a previously learned model from the LIBSVM model file filename.

Parameters
[in]filenamethe model file to read
Exceptions
plssvm::invalid_file_format_exceptionall exceptions thrown by plssvm::detail::io::parse_libsvm_model_header and plssvm::detail::io::parse_libsvm_data

◆ model() [2/2]

template<typename T , typename U >
plssvm::model< T, U >::model ( parameter  params,
data_set< real_type, label_type data 
)
private

Create a new model using the SVM parameter params and the data.

Default initializes the weights, i.e., no weights have currently been learned.

Note
This constructor may only be used in the befriended base C-SVM class!
Parameters
[in]paramsthe SVM parameters used to learn this model
[in]datathe data used to learn this model

Member Function Documentation

◆ save()

template<typename T , typename U >
void plssvm::model< T, U >::save ( const std::string &  filename) const

Save the model to a LIBSVM model file for later usage.

Parameters
[in]filenamethe file to save the model to
Examples
model_examples.cpp.

◆ num_support_vectors()

template<typename T , typename U = int>
size_type plssvm::model< T, U >::num_support_vectors ( ) const
inlinenoexcept

The number of support vectors used in this model.

Returns
the number of support vectors ([[nodiscard]])

◆ num_features()

template<typename T , typename U = int>
size_type plssvm::model< T, U >::num_features ( ) const
inlinenoexcept

The number of features of the support vectors used in this model.

Returns
the number of features ([[nodiscard]])

◆ get_params()

template<typename T , typename U = int>
const parameter& plssvm::model< T, U >::get_params ( ) const
inlinenoexcept

Return the SVM parameter that were used to learn this model.

Returns
the SVM parameter ([[nodiscard]])

◆ support_vectors()

template<typename T , typename U = int>
const std::vector<std::vector<real_type> >& plssvm::model< T, U >::support_vectors ( ) const
inlinenoexcept

The support vectors representing the learned model.

The support vectors are of dimension num_support_vectors() x num_features().

Returns
the support vectors ([[nodiscard]])

◆ labels()

template<typename T , typename U = int>
const std::vector<label_type>& plssvm::model< T, U >::labels ( ) const
inlinenoexcept

Returns the labels of the support vectors.

If the labels are present, they can be retrieved as std::vector using: dataset.labels()->get().

Returns
the labels ([[nodiscard]])

◆ num_different_labels()

template<typename T , typename U = int>
size_type plssvm::model< T, U >::num_different_labels ( ) const
inlinenoexcept

Returns the number of different labels in this data set.

If the data set contains the labels std::vector<int>{ -1, 1, 1, -1, -1, 1 }, this function returns 2. It is the same as: model.different_labels().size()

Returns
the number of different labels ([[nodiscard]])

◆ different_labels()

template<typename T , typename U = int>
std::vector<label_type> plssvm::model< T, U >::different_labels ( ) const
inline

Returns the different labels of the support vectors.

If the support vectors contain the labels std::vector<int>{ -1, 1, 1, -1, -1, 1 }, this function returns the labels { -1, 1 }.

Returns
all different labels ([[nodiscard]])

◆ weights()

template<typename T , typename U = int>
const std::vector<real_type>& plssvm::model< T, U >::weights ( ) const
inlinenoexcept

The learned weights for the support vectors.

It is of size num_support_vectors().

Returns
the weights ([[nodiscard]])

◆ rho()

template<typename T , typename U = int>
real_type plssvm::model< T, U >::rho ( ) const
inlinenoexcept

The bias value after learning.

Returns
the bias rho ([[nodiscard]])

Member Data Documentation

◆ w_

template<typename T , typename U = int>
std::shared_ptr<std::vector<real_type> > plssvm::model< T, U >::w_ { std::make_shared<std::vector<real_type>>() }
private

A vector used to speedup the prediction in case of the linear kernel function.

Will be reused by subsequent calls to plssvm::csvm::fit/plssvm::csvm::score with the same plssvm::model.

Note
Must be initialized to an empty vector instead of a nullptr in order to be passable as const reference.

The documentation for this class was generated from the following file: