52 KiB
SVM Classifier C++ 1.0.0
High-performance Support Vector Machine classifier with scikit-learn compatible API
|
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) | |
SVMClassifier & | operator= (const SVMClassifier &)=delete |
Copy assignment (deleted - models are not copyable) | |
SVMClassifier (SVMClassifier &&) noexcept | |
Move constructor. | |
SVMClassifier & | operator= (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 ¶m_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]
|
explicit |
Constructor with JSON parameters.
- Parameters
-
config JSON 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
-
kernel Kernel type C Regularization parameter multiclass_strategy Multiclass 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
-
X Feature tensor y Target tensor cv Number 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
-
X Feature tensor of shape (n_samples, n_features)
- Returns
- Tensor with decision function values
- Exceptions
-
std::runtime_error if model is not fitted
◆ evaluate()
EvaluationMetrics svm_classifier::SVMClassifier::evaluate | ( | const torch::Tensor & | X, |
const torch::Tensor & | y_true | ||
) |
Calculate detailed evaluation metrics.
- Parameters
-
X Feature tensor of shape (n_samples, n_features) y_true True 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
-
X Feature tensor of shape (n_samples, n_features) y Target tensor of shape (n_samples,) with class labels
- Returns
- Training metrics
- Exceptions
-
std::invalid_argument if input data is invalid std::runtime_error if 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_error if not supported for current kernel
◆ get_kernel_type()
|
inline |
◆ get_multiclass_strategy()
|
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()
|
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()
|
inline |
Get SVM library being used.
- Returns
- SVM library type
Definition at line 199 of file svm_classifier.hpp.
◆ get_training_metrics()
|
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
-
X Feature tensor y Target tensor param_grid JSON object with parameter grid cv Number of cross-validation folds
- Returns
- JSON object with best parameters and score
◆ is_fitted()
|
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
-
filename Path to load the model from
- Exceptions
-
std::runtime_error if loading fails
◆ predict()
torch::Tensor svm_classifier::SVMClassifier::predict | ( | const torch::Tensor & | X | ) |
Predict class labels for samples.
- Parameters
-
X Feature tensor of shape (n_samples, n_features)
- Returns
- Tensor of predicted class labels
- Exceptions
-
std::runtime_error if model is not fitted
◆ predict_proba()
torch::Tensor svm_classifier::SVMClassifier::predict_proba | ( | const torch::Tensor & | X | ) |
Predict class probabilities for samples.
- Parameters
-
X Feature tensor of shape (n_samples, n_features)
- Returns
- Tensor of shape (n_samples, n_classes) with class probabilities
- Exceptions
-
std::runtime_error if 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
-
filename Path to save the model
- Exceptions
-
std::runtime_error if saving fails
◆ score()
double svm_classifier::SVMClassifier::score | ( | const torch::Tensor & | X, |
const torch::Tensor & | y_true | ||
) |
Calculate accuracy score on test data.
- Parameters
-
X Feature tensor of shape (n_samples, n_features) y_true True labels tensor of shape (n_samples,)
- Returns
- Accuracy score (fraction of correctly predicted samples)
- Exceptions
-
std::runtime_error if model is not fitted
◆ set_parameters()
void svm_classifier::SVMClassifier::set_parameters | ( | const nlohmann::json & | config | ) |
Set parameters from JSON configuration.
- Parameters
-
config JSON configuration object
- Exceptions
-
std::invalid_argument if 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:
- include/svm_classifier/svm_classifier.hpp
Generated on Sun Jun 22 2025 11:25:27 for SVM Classifier C++ by