Files
SVMClassifier/classsvm__classifier_1_1SVMClassifier.html
2025-06-22 11:25:27 +00:00

52 KiB

<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US"> <head> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="dynsections.js"></script> <script type="text/javascript" src="search/searchdata.js"></script> <script type="text/javascript" src="search/search.js"></script> </head>
SVM Classifier C++ 1.0.0
High-performance Support Vector Machine classifier with scikit-learn compatible API
<script type="text/javascript"> /* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */ var searchBox = new SearchBox("searchBox", "search/",'.html'); /* @license-end */ </script> <script type="text/javascript" src="menudata.js"></script> <script type="text/javascript" src="menu.js"></script> <script type="text/javascript"> /* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */ $(function() { initMenu('',true,false,'search.php','Search'); $(document).ready(function() { init_search(); }); }); /* @license-end */ </script>
Loading...
Searching...
No Matches
svm_classifier::SVMClassifier Class Reference

Support Vector Machine Classifier with scikit-learn compatible API. More...

#include <svm_classifier.hpp>

Public Member Functions

 SVMClassifier ()
 Default constructor with default parameters.
 
 SVMClassifier (const nlohmann::json &config)
 Constructor with JSON parameters.
 
 SVMClassifier (KernelType kernel, double C=1.0, MulticlassStrategy multiclass_strategy=MulticlassStrategy::ONE_VS_REST)
 Constructor with explicit parameters.
 
 ~SVMClassifier ()
 Destructor.
 
 SVMClassifier (const SVMClassifier &)=delete
 Copy constructor (deleted - models are not copyable)
 
SVMClassifieroperator= (const SVMClassifier &)=delete
 Copy assignment (deleted - models are not copyable)
 
 SVMClassifier (SVMClassifier &&) noexcept
 Move constructor.
 
SVMClassifieroperator= (SVMClassifier &&) noexcept
 Move assignment.
 
TrainingMetrics fit (const torch::Tensor &X, const torch::Tensor &y)
 Train the SVM classifier.
 
torch::Tensor predict (const torch::Tensor &X)
 Predict class labels for samples.
 
torch::Tensor predict_proba (const torch::Tensor &X)
 Predict class probabilities for samples.
 
torch::Tensor decision_function (const torch::Tensor &X)
 Get decision function values.
 
double score (const torch::Tensor &X, const torch::Tensor &y_true)
 Calculate accuracy score on test data.
 
EvaluationMetrics evaluate (const torch::Tensor &X, const torch::Tensor &y_true)
 Calculate detailed evaluation metrics.
 
void set_parameters (const nlohmann::json &config)
 Set parameters from JSON configuration.
 
nlohmann::json get_parameters () const
 Get current parameters as JSON.
 
bool is_fitted () const
 Check if the model is fitted/trained.
 
int get_n_classes () const
 Get the number of classes.
 
std::vector< int > get_classes () const
 Get unique class labels.
 
int get_n_features () const
 Get the number of features.
 
TrainingMetrics get_training_metrics () const
 Get training metrics from last fit.
 
bool supports_probability () const
 Check if the current model supports probability prediction.
 
void save_model (const std::string &filename) const
 Save model to file.
 
void load_model (const std::string &filename)
 Load model from file.
 
KernelType get_kernel_type () const
 Get kernel type.
 
MulticlassStrategy get_multiclass_strategy () const
 Get multiclass strategy.
 
SVMLibrary get_svm_library () const
 Get SVM library being used.
 
std::vector< double > cross_validate (const torch::Tensor &X, const torch::Tensor &y, int cv=5)
 Perform cross-validation.
 
nlohmann::json grid_search (const torch::Tensor &X, const torch::Tensor &y, const nlohmann::json &param_grid, int cv=5)
 Find optimal hyperparameters using grid search.
 
torch::Tensor get_feature_importance () const
 Get feature importance (for linear kernels only)
 
void reset ()
 Reset the classifier (clear trained model)
 

Detailed Description

Support Vector Machine Classifier with scikit-learn compatible API.

This class provides a unified interface for SVM classification using both liblinear (for linear kernels) and libsvm (for non-linear kernels). It supports multiclass classification through One-vs-Rest and One-vs-One strategies.

Definition at line 21 of file svm_classifier.hpp.

Constructor & Destructor Documentation

◆ SVMClassifier() [1/2]

svm_classifier::SVMClassifier::SVMClassifier ( const nlohmann::json &  config)
explicit

Constructor with JSON parameters.

Parameters
configJSON configuration object

◆ SVMClassifier() [2/2]

svm_classifier::SVMClassifier::SVMClassifier ( KernelType  kernel,
double  C = 1.0,
MulticlassStrategy  multiclass_strategy = MulticlassStrategy::ONE_VS_REST 
)

Constructor with explicit parameters.

Parameters
kernelKernel type
CRegularization parameter
multiclass_strategyMulticlass strategy

Member Function Documentation

◆ cross_validate()

std::vector< double > svm_classifier::SVMClassifier::cross_validate ( const torch::Tensor &  X,
const torch::Tensor &  y,
int  cv = 5 
)

Perform cross-validation.

Parameters
XFeature tensor
yTarget tensor
cvNumber of folds (default: 5)
Returns
Cross-validation scores for each fold

◆ decision_function()

torch::Tensor svm_classifier::SVMClassifier::decision_function ( const torch::Tensor &  X)

Get decision function values.

Parameters
XFeature tensor of shape (n_samples, n_features)
Returns
Tensor with decision function values
Exceptions
std::runtime_errorif model is not fitted

◆ evaluate()

EvaluationMetrics svm_classifier::SVMClassifier::evaluate ( const torch::Tensor &  X,
const torch::Tensor &  y_true 
)

Calculate detailed evaluation metrics.

Parameters
XFeature tensor of shape (n_samples, n_features)
y_trueTrue labels tensor of shape (n_samples,)
Returns
Evaluation metrics including precision, recall, F1-score

◆ fit()

TrainingMetrics svm_classifier::SVMClassifier::fit ( const torch::Tensor &  X,
const torch::Tensor &  y 
)

Train the SVM classifier.

Parameters
XFeature tensor of shape (n_samples, n_features)
yTarget tensor of shape (n_samples,) with class labels
Returns
Training metrics
Exceptions
std::invalid_argumentif input data is invalid
std::runtime_errorif training fails

◆ get_classes()

std::vector< int > svm_classifier::SVMClassifier::get_classes ( ) const

Get unique class labels.

Returns
Vector of unique class labels

◆ get_feature_importance()

torch::Tensor svm_classifier::SVMClassifier::get_feature_importance ( ) const

Get feature importance (for linear kernels only)

Returns
Tensor with feature weights/importance
Exceptions
std::runtime_errorif not supported for current kernel

◆ get_kernel_type()

KernelType svm_classifier::SVMClassifier::get_kernel_type ( ) const
inline

Get kernel type.

Returns
Current kernel type

Definition at line 187 of file svm_classifier.hpp.

◆ get_multiclass_strategy()

MulticlassStrategy svm_classifier::SVMClassifier::get_multiclass_strategy ( ) const
inline

Get multiclass strategy.

Returns
Current multiclass strategy

Definition at line 193 of file svm_classifier.hpp.

◆ get_n_classes()

int svm_classifier::SVMClassifier::get_n_classes ( ) const

Get the number of classes.

Returns
Number of classes (0 if not fitted)

◆ get_n_features()

int svm_classifier::SVMClassifier::get_n_features ( ) const
inline

Get the number of features.

Returns
Number of features (0 if not fitted)

Definition at line 155 of file svm_classifier.hpp.

◆ get_parameters()

nlohmann::json svm_classifier::SVMClassifier::get_parameters ( ) const

Get current parameters as JSON.

Returns
JSON object with current parameters

◆ get_svm_library()

SVMLibrary svm_classifier::SVMClassifier::get_svm_library ( ) const
inline

Get SVM library being used.

Returns
SVM library type

Definition at line 199 of file svm_classifier.hpp.

◆ get_training_metrics()

TrainingMetrics svm_classifier::SVMClassifier::get_training_metrics ( ) const
inline

Get training metrics from last fit.

Returns
Training metrics

Definition at line 161 of file svm_classifier.hpp.

◆ grid_search()

nlohmann::json svm_classifier::SVMClassifier::grid_search ( const torch::Tensor &  X,
const torch::Tensor &  y,
const nlohmann::json &  param_grid,
int  cv = 5 
)

Find optimal hyperparameters using grid search.

Parameters
XFeature tensor
yTarget tensor
param_gridJSON object with parameter grid
cvNumber of cross-validation folds
Returns
JSON object with best parameters and score

◆ is_fitted()

bool svm_classifier::SVMClassifier::is_fitted ( ) const
inline

Check if the model is fitted/trained.

Returns
True if model is fitted

Definition at line 137 of file svm_classifier.hpp.

◆ load_model()

void svm_classifier::SVMClassifier::load_model ( const std::string &  filename)

Load model from file.

Parameters
filenamePath to load the model from
Exceptions
std::runtime_errorif loading fails

◆ predict()

torch::Tensor svm_classifier::SVMClassifier::predict ( const torch::Tensor &  X)

Predict class labels for samples.

Parameters
XFeature tensor of shape (n_samples, n_features)
Returns
Tensor of predicted class labels
Exceptions
std::runtime_errorif model is not fitted

◆ predict_proba()

torch::Tensor svm_classifier::SVMClassifier::predict_proba ( const torch::Tensor &  X)

Predict class probabilities for samples.

Parameters
XFeature tensor of shape (n_samples, n_features)
Returns
Tensor of shape (n_samples, n_classes) with class probabilities
Exceptions
std::runtime_errorif model is not fitted or doesn't support probabilities

◆ save_model()

void svm_classifier::SVMClassifier::save_model ( const std::string &  filename) const

Save model to file.

Parameters
filenamePath to save the model
Exceptions
std::runtime_errorif saving fails

◆ score()

double svm_classifier::SVMClassifier::score ( const torch::Tensor &  X,
const torch::Tensor &  y_true 
)

Calculate accuracy score on test data.

Parameters
XFeature tensor of shape (n_samples, n_features)
y_trueTrue labels tensor of shape (n_samples,)
Returns
Accuracy score (fraction of correctly predicted samples)
Exceptions
std::runtime_errorif model is not fitted

◆ set_parameters()

void svm_classifier::SVMClassifier::set_parameters ( const nlohmann::json &  config)

Set parameters from JSON configuration.

Parameters
configJSON configuration object
Exceptions
std::invalid_argumentif parameters are invalid

◆ supports_probability()

bool svm_classifier::SVMClassifier::supports_probability ( ) const

Check if the current model supports probability prediction.

Returns
True if probabilities are supported

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

Generated on Sun Jun 22 2025 11:25:27 for SVM Classifier C++ by doxygen 1.9.8 </html>