openGPMP
Open Source Mathematics Package
svc.hpp
Go to the documentation of this file.
1 /*************************************************************************
2  *
3  * Project
4  * _____ _____ __ __ _____
5  * / ____| __ \| \/ | __ \
6  * ___ _ __ ___ _ __ | | __| |__) | \ / | |__) |
7  * / _ \| '_ \ / _ \ '_ \| | |_ | ___/| |\/| | ___/
8  *| (_) | |_) | __/ | | | |__| | | | | | | |
9  * \___/| .__/ \___|_| |_|\_____|_| |_| |_|_|
10  * | |
11  * |_|
12  *
13  * Copyright (C) Akiel Aries, <akiel@akiel.org>, et al.
14  *
15  * This software is licensed as described in the file LICENSE, which
16  * you should have received as part of this distribution. The terms
17  * among other details are referenced in the official documentation
18  * seen here : https://akielaries.github.io/openGPMP/ along with
19  * important files seen in this project.
20  *
21  * You may opt to use, copy, modify, merge, publish, distribute
22  * and/or sell copies of the Software, and permit persons to whom
23  * the Software is furnished to do so, under the terms of the
24  * LICENSE file. As this is an Open Source effort, all implementations
25  * must be of the same methodology.
26  *
27  *
28  *
29  * This software is distributed on an AS IS basis, WITHOUT
30  * WARRANTY OF ANY KIND, either express or implied.
31  *
32  ************************************************************************/
33 
34 #ifndef SVC_HPP
35 #define SVC_HPP
36 
37 #include <algorithm>
38 #include <random>
39 #include <vector>
40 
41 namespace gpmp {
42 
43 namespace ml {
44 
50 class SVC {
51  public:
61  SVC(double C_ = 1.0,
62  double l_rate = 0.01,
63  int max_iters = 1000,
64  double tol = 1e-4);
65 
71  void fit(const std::vector<std::vector<double>> &X_train,
72  const std::vector<int> &y_train);
73 
79  std::vector<int> predict(const std::vector<std::vector<double>> &X_test);
80 
86  std::vector<double>
87  predict_proba(const std::vector<std::vector<double>> &X_test);
88 
95  double score(const std::vector<std::vector<double>> &X_test,
96  const std::vector<int> &y_test);
97 
102  void set_kernel(const std::string &k_type);
103 
108  void set_kernel_parameters(double k_param);
109 
114  void set_random_state(int seed);
115 
120  void set_verbose(bool vbose);
121 
126  void set_penalty(const std::string &p_type);
127 
135  double cross_val_score(const std::vector<std::vector<double>> &X,
136  const std::vector<int> &y,
137  int cv = 5);
138 
148  std::vector<double> grid_search(const std::vector<std::vector<double>> &X,
149  const std::vector<int> &y,
150  const std::vector<double> &C_values,
151  const std::vector<double> &kernel_params,
152  int cv = 5);
153 
155  double C;
161  double tolerance;
163  std::string kernel_type;
165  double kernel_param;
169  bool verbose;
171  std::string penalty_type;
173  std::vector<double> weights;
175  double bias;
176 
183  double hinge_loss(double prediction, int label);
184 
191  double compute_loss(const std::vector<std::vector<double>> &X,
192  const std::vector<int> &y);
193 
199  void update_weights(const std::vector<std::vector<double>> &X,
200  const std::vector<int> &y);
201 
208  double kernel(const std::vector<double> &x1, const std::vector<double> &x2);
209 
216  double dot_product(const std::vector<double> &x1,
217  const std::vector<double> &x2);
218 
224  double sigmoid(double z);
225 
232  std::vector<int> k_fold_indices(int num_instances, int k);
233 
240  double accuracy(const std::vector<int> &predictions,
241  const std::vector<int> &labels);
242 };
243 
244 } // namespace ml
245 } // namespace gpmp
246 
247 #endif
Support Vector Classifier (SVC) for binary classification using Stochastic Gradient Descent.
Definition: svc.hpp:50
void set_random_state(int seed)
Set the random seed for reproducibility.
Definition: svc.cpp:146
double learning_rate
Definition: svc.hpp:157
double dot_product(const std::vector< double > &x1, const std::vector< double > &x2)
Compute the dot product between two vectors.
Definition: svc.cpp:227
std::string kernel_type
Definition: svc.hpp:163
double tolerance
Definition: svc.hpp:161
SVC(double C_=1.0, double l_rate=0.01, int max_iters=1000, double tol=1e-4)
Constructor for SVC class.
Definition: svc.cpp:38
void set_verbose(bool vbose)
Enable or disable verbose output during training.
Definition: svc.cpp:150
void set_kernel(const std::string &k_type)
Set the kernel type for the SVC.
Definition: svc.cpp:138
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.
Definition: svc.cpp:132
std::vector< double > predict_proba(const std::vector< std::vector< double >> &X_test)
Predict class probabilities for given test data.
Definition: svc.cpp:118
void update_weights(const std::vector< std::vector< double >> &X, const std::vector< int > &y)
Update weights and bias using stochastic gradient descent.
Definition: svc.cpp:97
std::vector< int > k_fold_indices(int num_instances, int k)
Generate k-fold indices for cross-validation.
Definition: svc.cpp:240
std::string penalty_type
Definition: svc.hpp:171
bool verbose
Definition: svc.hpp:169
std::vector< double > weights
Definition: svc.hpp:173
double bias
Definition: svc.hpp:175
double sigmoid(double z)
Sigmoid activation function.
Definition: svc.cpp:236
void fit(const std::vector< std::vector< double >> &X_train, const std::vector< int > &y_train)
Fit the SVC model to the training data.
Definition: svc.cpp:42
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.
Definition: svc.cpp:158
int max_iterations
Definition: svc.hpp:159
void set_kernel_parameters(double k_param)
Set the kernel parameters for the SVC.
Definition: svc.cpp:142
double kernel_param
Definition: svc.hpp:165
void set_penalty(const std::string &p_type)
Set the penalty type for regularization.
Definition: svc.cpp:154
double C
Definition: svc.hpp:155
double kernel(const std::vector< double > &x1, const std::vector< double > &x2)
Compute the kernel function between two vectors.
Definition: svc.cpp:217
double hinge_loss(double prediction, int label)
Compute the hinge loss for a given prediction and true label.
Definition: svc.cpp:75
double accuracy(const std::vector< int > &predictions, const std::vector< int > &labels)
Compute the accuracy of predictions.
Definition: svc.cpp:249
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.
Definition: svc.cpp:195
std::vector< int > predict(const std::vector< std::vector< double >> &X_test)
Predict labels for given test data.
Definition: svc.cpp:61
int random_state
Definition: svc.hpp:167
double compute_loss(const std::vector< std::vector< double >> &X, const std::vector< int > &y)
Compute the total loss (including regularization) for the model.
Definition: svc.cpp:79
The source C++ openGPMP namespace.