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) | |
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.
|
explicit |
Constructor with JSON parameters.
config | JSON configuration object |
svm_classifier::SVMClassifier::SVMClassifier | ( | KernelType | kernel, |
double | C = 1.0 , |
||
MulticlassStrategy | multiclass_strategy = MulticlassStrategy::ONE_VS_REST |
||
) |
Constructor with explicit parameters.
kernel | Kernel type |
C | Regularization parameter |
multiclass_strategy | Multiclass strategy |
std::vector< double > svm_classifier::SVMClassifier::cross_validate | ( | const torch::Tensor & | X, |
const torch::Tensor & | y, | ||
int | cv = 5 |
||
) |
Perform cross-validation.
X | Feature tensor |
y | Target tensor |
cv | Number of folds (default: 5) |
torch::Tensor svm_classifier::SVMClassifier::decision_function | ( | const torch::Tensor & | X | ) |
Get decision function values.
X | Feature tensor of shape (n_samples, n_features) |
std::runtime_error | if model is not fitted |
EvaluationMetrics svm_classifier::SVMClassifier::evaluate | ( | const torch::Tensor & | X, |
const torch::Tensor & | y_true | ||
) |
Calculate detailed evaluation metrics.
X | Feature tensor of shape (n_samples, n_features) |
y_true | True labels tensor of shape (n_samples,) |
TrainingMetrics svm_classifier::SVMClassifier::fit | ( | const torch::Tensor & | X, |
const torch::Tensor & | y | ||
) |
Train the SVM classifier.
X | Feature tensor of shape (n_samples, n_features) |
y | Target tensor of shape (n_samples,) with class labels |
std::invalid_argument | if input data is invalid |
std::runtime_error | if training fails |
std::vector< int > svm_classifier::SVMClassifier::get_classes | ( | ) | const |
Get unique class labels.
torch::Tensor svm_classifier::SVMClassifier::get_feature_importance | ( | ) | const |
Get feature importance (for linear kernels only)
std::runtime_error | if not supported for current kernel |
|
inline |
|
inline |
Get multiclass strategy.
Definition at line 193 of file svm_classifier.hpp.
int svm_classifier::SVMClassifier::get_n_classes | ( | ) | const |
Get the number of classes.
|
inline |
Get the number of features.
Definition at line 155 of file svm_classifier.hpp.
nlohmann::json svm_classifier::SVMClassifier::get_parameters | ( | ) | const |
Get current parameters as JSON.
|
inline |
Get SVM library being used.
Definition at line 199 of file svm_classifier.hpp.
|
inline |
Get training metrics from last fit.
Definition at line 161 of file svm_classifier.hpp.
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.
X | Feature tensor |
y | Target tensor |
param_grid | JSON object with parameter grid |
cv | Number of cross-validation folds |
|
inline |
Check if the model is fitted/trained.
Definition at line 137 of file svm_classifier.hpp.
void svm_classifier::SVMClassifier::load_model | ( | const std::string & | filename | ) |
Load model from file.
filename | Path to load the model from |
std::runtime_error | if loading fails |
torch::Tensor svm_classifier::SVMClassifier::predict | ( | const torch::Tensor & | X | ) |
Predict class labels for samples.
X | Feature tensor of shape (n_samples, n_features) |
std::runtime_error | if model is not fitted |
torch::Tensor svm_classifier::SVMClassifier::predict_proba | ( | const torch::Tensor & | X | ) |
Predict class probabilities for samples.
X | Feature tensor of shape (n_samples, n_features) |
std::runtime_error | if model is not fitted or doesn't support probabilities |
void svm_classifier::SVMClassifier::save_model | ( | const std::string & | filename | ) | const |
Save model to file.
filename | Path to save the model |
std::runtime_error | if saving fails |
double svm_classifier::SVMClassifier::score | ( | const torch::Tensor & | X, |
const torch::Tensor & | y_true | ||
) |
Calculate accuracy score on test data.
X | Feature tensor of shape (n_samples, n_features) |
y_true | True labels tensor of shape (n_samples,) |
std::runtime_error | if model is not fitted |
void svm_classifier::SVMClassifier::set_parameters | ( | const nlohmann::json & | config | ) |
Set parameters from JSON configuration.
config | JSON configuration object |
std::invalid_argument | if parameters are invalid |
bool svm_classifier::SVMClassifier::supports_probability | ( | ) | const |
Check if the current model supports probability prediction.