openGPMP
Open Source Mathematics Package
|
Support Vector Classifier (SVC) for binary classification using Stochastic Gradient Descent. More...
#include <svc.hpp>
Public Member Functions | |
SVC (double C_=1.0, double l_rate=0.01, int max_iters=1000, double tol=1e-4) | |
Constructor for SVC class. More... | |
void | fit (const std::vector< std::vector< double >> &X_train, const std::vector< int > &y_train) |
Fit the SVC model to the training data. More... | |
std::vector< int > | predict (const std::vector< std::vector< double >> &X_test) |
Predict labels for given test data. More... | |
std::vector< double > | predict_proba (const std::vector< std::vector< double >> &X_test) |
Predict class probabilities for given test data. More... | |
double | score (const std::vector< std::vector< double >> &X_test, const std::vector< int > &y_test) |
Calculate the accuracy of the model on given test data. More... | |
void | set_kernel (const std::string &k_type) |
Set the kernel type for the SVC. More... | |
void | set_kernel_parameters (double k_param) |
Set the kernel parameters for the SVC. More... | |
void | set_random_state (int seed) |
Set the random seed for reproducibility. More... | |
void | set_verbose (bool vbose) |
Enable or disable verbose output during training. More... | |
void | set_penalty (const std::string &p_type) |
Set the penalty type for regularization. More... | |
double | cross_val_score (const std::vector< std::vector< double >> &X, const std::vector< int > &y, int cv=5) |
Perform k-fold cross-validation on the model. More... | |
std::vector< double > | grid_search (const std::vector< std::vector< double >> &X, const std::vector< int > &y, const std::vector< double > &C_values, const std::vector< double > &kernel_params, int cv=5) |
Perform grid search for hyperparameter tuning. More... | |
double | hinge_loss (double prediction, int label) |
Compute the hinge loss for a given prediction and true label. More... | |
double | compute_loss (const std::vector< std::vector< double >> &X, const std::vector< int > &y) |
Compute the total loss (including regularization) for the model. More... | |
void | update_weights (const std::vector< std::vector< double >> &X, const std::vector< int > &y) |
Update weights and bias using stochastic gradient descent. More... | |
double | kernel (const std::vector< double > &x1, const std::vector< double > &x2) |
Compute the kernel function between two vectors. More... | |
double | dot_product (const std::vector< double > &x1, const std::vector< double > &x2) |
Compute the dot product between two vectors. More... | |
double | sigmoid (double z) |
Sigmoid activation function. More... | |
std::vector< int > | k_fold_indices (int num_instances, int k) |
Generate k-fold indices for cross-validation. More... | |
double | accuracy (const std::vector< int > &predictions, const std::vector< int > &labels) |
Compute the accuracy of predictions. More... | |
Public Attributes | |
double | C |
double | learning_rate |
int | max_iterations |
double | tolerance |
std::string | kernel_type |
double | kernel_param |
int | random_state |
bool | verbose |
std::string | penalty_type |
std::vector< double > | weights |
double | bias |
Support Vector Classifier (SVC) for binary classification using Stochastic Gradient Descent.
gpmp::ml::SVC::SVC | ( | double | C_ = 1.0 , |
double | l_rate = 0.01 , |
||
int | max_iters = 1000 , |
||
double | tol = 1e-4 |
||
) |
double gpmp::ml::SVC::accuracy | ( | const std::vector< int > & | predictions, |
const std::vector< int > & | labels | ||
) |
Compute the accuracy of predictions.
predictions | Predicted labels |
labels | True labels |
double gpmp::ml::SVC::compute_loss | ( | const std::vector< std::vector< double >> & | X, |
const std::vector< int > & | y | ||
) |
Compute the total loss (including regularization) for the model.
X | Input features |
y | True labels |
Definition at line 79 of file svc.cpp.
References python.linalg::C.
double gpmp::ml::SVC::cross_val_score | ( | const std::vector< std::vector< double >> & | X, |
const std::vector< int > & | y, | ||
int | cv = 5 |
||
) |
Perform k-fold cross-validation on the model.
X | Input features for cross-validation |
y | True labels for cross-validation |
cv | Number of folds (default: 5) |
Definition at line 158 of file svc.cpp.
double gpmp::ml::SVC::dot_product | ( | const std::vector< double > & | x1, |
const std::vector< double > & | x2 | ||
) |
void gpmp::ml::SVC::fit | ( | const std::vector< std::vector< double >> & | X_train, |
const std::vector< int > & | y_train | ||
) |
Fit the SVC model to the training data.
X_train | Input features for training |
y_train | Labels for training |
Definition at line 42 of file svc.cpp.
std::vector< double > gpmp::ml::SVC::grid_search | ( | const std::vector< std::vector< double >> & | X, |
const std::vector< int > & | y, | ||
const std::vector< double > & | C_values, | ||
const std::vector< double > & | kernel_params, | ||
int | cv = 5 |
||
) |
Perform grid search for hyperparameter tuning.
X | Input features for grid search |
y | True labels for grid search |
C_values | Candidate values for the regularization parameter |
kernel_params | Candidate values for the kernel parameter |
cv | Number of folds for cross-validation (default: 5) |
Definition at line 195 of file svc.cpp.
double gpmp::ml::SVC::hinge_loss | ( | double | prediction, |
int | label | ||
) |
std::vector< int > gpmp::ml::SVC::k_fold_indices | ( | int | num_instances, |
int | k | ||
) |
Generate k-fold indices for cross-validation.
num_instances | Total number of instances |
k | Number of folds |
double gpmp::ml::SVC::kernel | ( | const std::vector< double > & | x1, |
const std::vector< double > & | x2 | ||
) |
Compute the kernel function between two vectors.
x1 | First vector |
x2 | Second vector |
Definition at line 217 of file svc.cpp.
References gpmp::linalg::dot_product().
std::vector< int > gpmp::ml::SVC::predict | ( | const std::vector< std::vector< double >> & | X_test | ) |
std::vector< double > gpmp::ml::SVC::predict_proba | ( | const std::vector< std::vector< double >> & | X_test | ) |
double gpmp::ml::SVC::score | ( | const std::vector< std::vector< double >> & | X_test, |
const std::vector< int > & | y_test | ||
) |
Calculate the accuracy of the model on given test data.
X_test | Input features for evaluation |
y_test | True labels for evaluation |
Definition at line 132 of file svc.cpp.
void gpmp::ml::SVC::set_kernel | ( | const std::string & | k_type | ) |
Set the kernel type for the SVC.
k_type | Kernel type (eg, "linear") |
void gpmp::ml::SVC::set_kernel_parameters | ( | double | k_param | ) |
void gpmp::ml::SVC::set_penalty | ( | const std::string & | p_type | ) |
void gpmp::ml::SVC::set_random_state | ( | int | seed | ) |
void gpmp::ml::SVC::set_verbose | ( | bool | vbose | ) |
double gpmp::ml::SVC::sigmoid | ( | double | z | ) |
void gpmp::ml::SVC::update_weights | ( | const std::vector< std::vector< double >> & | X, |
const std::vector< int > & | y | ||
) |
Update weights and bias using stochastic gradient descent.
X | Input features |
y | True labels |
Definition at line 97 of file svc.cpp.
References python.linalg::C.
double gpmp::ml::SVC::C |
double gpmp::ml::SVC::kernel_param |
std::string gpmp::ml::SVC::kernel_type |
double gpmp::ml::SVC::learning_rate |
int gpmp::ml::SVC::random_state |