openGPMP
Open Source Mathematics Package
Public Member Functions | Public Attributes | List of all members
gpmp::ml::LogReg Class Reference

Represents a Logistic Regression classifier. More...

#include <logreg.hpp>

Public Member Functions

 LogReg (double l_rate=001, int num_iters=1000, double lda=001)
 Constructor for the LogReg class. More...
 
 ~LogReg ()
 Destructor for the LogReg class. More...
 
void train (const std::vector< std::vector< double >> &X_train, const std::vector< int > &y_train)
 Trains the logistic regression model on the given training data. More...
 
std::vector< int > classify (const std::vector< std::vector< double >> &X)
 Predicts the class labels for the given test data. More...
 
std::vector< double > predict (const std::vector< std::vector< double >> &X_test)
 Computes the predicted probabilities for the given test data. More...
 
double accuracy (const std::vector< std::vector< double >> &X_test, const std::vector< int > &y_test)
 Computes the accuracy of the model on the given test data. More...
 
void feature_scaling (std::vector< std::vector< double >> &X)
 Performs feature scaling on the input feature matrix. More...
 
double sigmoid (double z)
 Computes the sigmoid function value for the given input. More...
 
double cost_function (const std::vector< std::vector< double >> &X, const std::vector< int > &y)
 Computes the cost function value for the given input data and labels. More...
 

Public Attributes

double learning_rate
 
int num_iterations
 
double lambda
 
std::vector< double > weights
 

Detailed Description

Represents a Logistic Regression classifier.

Definition at line 53 of file logreg.hpp.

Constructor & Destructor Documentation

◆ LogReg()

gpmp::ml::LogReg::LogReg ( double  l_rate = 001,
int  num_iters = 1000,
double  lda = 001 
)

Constructor for the LogReg class.

Parameters
l_rateThe learning rate for gradient descent optimization (default: 001)
num_itersThe number of iterations for gradient descent (default: 1000)
ldaThe regularization parameter lambda (default: 001)

Definition at line 40 of file logreg.cpp.

41  : learning_rate(l_rate), num_iterations(num_iters), lambda(lda) {
42 }
double learning_rate
Definition: logreg.hpp:107

◆ ~LogReg()

gpmp::ml::LogReg::~LogReg ( )

Destructor for the LogReg class.

Definition at line 44 of file logreg.cpp.

44  {
45 }

Member Function Documentation

◆ accuracy()

double gpmp::ml::LogReg::accuracy ( const std::vector< std::vector< double >> &  X_test,
const std::vector< int > &  y_test 
)

Computes the accuracy of the model on the given test data.

Parameters
X_testThe feature matrix of the test data
y_testThe true labels of the test data
Returns
The accuracy of the model

Definition at line 99 of file logreg.cpp.

100  {
101  std::vector<double> predictions = predict(X_test);
102  int correct = 0;
103  for (size_t i = 0; i < predictions.size(); ++i) {
104  if ((predictions[i] >= 0.5 && y_test[i] == 1) ||
105  (predictions[i] < 0.5 && y_test[i] == 0)) {
106  correct++;
107  }
108  }
109  return static_cast<double>(correct) / y_test.size();
110 }
std::vector< double > predict(const std::vector< std::vector< double >> &X_test)
Computes the predicted probabilities for the given test data.
Definition: logreg.cpp:81

◆ classify()

std::vector< int > gpmp::ml::LogReg::classify ( const std::vector< std::vector< double >> &  X)

Predicts the class labels for the given test data.

Parameters
X_testThe feature matrix of the test data
Returns
A vector of predicted class labels

Definition at line 164 of file logreg.cpp.

164  {
165  std::vector<int> classifications;
166  for (size_t i = 0; i < X.size(); ++i) {
167  // Add bias term to input
168  std::vector<double> input = {1.0};
169  input.insert(input.end(), X[i].begin(), X[i].end());
170 
171  // Compute the predicted value
172  double predicted = sigmoid(std::inner_product(input.begin(),
173  input.end(),
174  weights.begin(),
175  0.0));
176  int classification = predicted >= 0.5 ? 1 : 0;
177  classifications.push_back(classification);
178  }
179  return classifications;
180 }
std::vector< double > weights
Definition: logreg.hpp:112
double sigmoid(double z)
Computes the sigmoid function value for the given input.
Definition: logreg.cpp:112

◆ cost_function()

double gpmp::ml::LogReg::cost_function ( const std::vector< std::vector< double >> &  X,
const std::vector< int > &  y 
)

Computes the cost function value for the given input data and labels.

Parameters
XThe feature matrix of the data
yThe labels of the data
Returns
The value of the cost function

Definition at line 145 of file logreg.cpp.

146  {
147  double cost = 0.0;
148  for (size_t i = 0; i < X.size(); ++i) {
149  // Add bias term to input
150  std::vector<double> input = {1.0};
151  input.insert(input.end(), X[i].begin(), X[i].end());
152 
153  double predicted = sigmoid(std::inner_product(input.begin(),
154  input.end(),
155  weights.begin(),
156  0.0));
157  cost += -y[i] * log(predicted) - (1 - y[i]) * log(1 - predicted);
158  }
159  cost /= X.size();
160  return cost;
161 }

◆ feature_scaling()

void gpmp::ml::LogReg::feature_scaling ( std::vector< std::vector< double >> &  X)

Performs feature scaling on the input feature matrix.

Parameters
XThe feature matrix to be scaled

Definition at line 116 of file logreg.cpp.

116  {
117  if (X.empty()) {
118  throw std::invalid_argument("Input feature matrix is empty.");
119  }
120 
121  size_t num_features = X[0].size();
122  for (size_t j = 0; j < num_features; ++j) {
123  double min_val = X[0][j], max_val = X[0][j];
124  for (size_t i = 1; i < X.size(); ++i) {
125  if (X[i][j] < min_val) {
126  min_val = X[i][j];
127  }
128  if (X[i][j] > max_val) {
129  max_val = X[i][j];
130  }
131  }
132 
133  if (fabs(min_val - max_val) < std::numeric_limits<double>::epsilon()) {
134  continue; // Skip if all values are the same
135  }
136 
137  double range = max_val - min_val;
138  for (size_t i = 0; i < X.size(); ++i) {
139  X[i][j] = (X[i][j] - min_val) / range;
140  }
141  }
142 }

◆ predict()

std::vector< double > gpmp::ml::LogReg::predict ( const std::vector< std::vector< double >> &  X_test)

Computes the predicted probabilities for the given test data.

Parameters
X_testThe feature matrix of the test data
Returns
A vector of predicted probabilities

Definition at line 81 of file logreg.cpp.

81  {
82  std::vector<double> predictions;
83  for (size_t i = 0; i < X_test.size(); ++i) {
84  // Add bias term to input
85  std::vector<double> input = {1.0};
86  input.insert(input.end(), X_test[i].begin(), X_test[i].end());
87 
88  // Compute the predicted value
89  double predicted = sigmoid(std::inner_product(input.begin(),
90  input.end(),
91  weights.begin(),
92  0.0));
93  predictions.push_back(predicted);
94  }
95  return predictions;
96 }

◆ sigmoid()

double gpmp::ml::LogReg::sigmoid ( double  z)

Computes the sigmoid function value for the given input.

Parameters
zThe input value
Returns
The sigmoid of z

Definition at line 112 of file logreg.cpp.

112  {
113  return 1.0 / (1.0 + exp(-z));
114 }

◆ train()

void gpmp::ml::LogReg::train ( const std::vector< std::vector< double >> &  X_train,
const std::vector< int > &  y_train 
)

Trains the logistic regression model on the given training data.

Parameters
X_trainThe feature matrix of the training data
y_trainThe labels of the training data

Definition at line 47 of file logreg.cpp.

48  {
49  // Initialize weights to zeros
50  weights.assign(X_train[0].size() + 1, 0.0);
51 
52  for (int iter = 0; iter < num_iterations; ++iter) {
53  std::vector<double> gradient(X_train[0].size() + 1, 0.0);
54 
55  for (size_t i = 0; i < X_train.size(); ++i) {
56  // Add bias term to input
57  std::vector<double> input = {1.0};
58  input.insert(input.end(), X_train[i].begin(), X_train[i].end());
59 
60  // Compute the predicted value
61  double predicted = sigmoid(std::inner_product(input.begin(),
62  input.end(),
63  weights.begin(),
64  0.0));
65 
66  // Compute gradient for each weight
67  for (size_t j = 0; j < gradient.size(); ++j) {
68  gradient[j] += (predicted - y_train[i]) * input[j];
69  }
70  }
71 
72  // Update weights using gradient descent
73  for (size_t j = 0; j < weights.size(); ++j) {
74  weights[j] -= learning_rate *
75  (gradient[j] / X_train.size() + lambda * weights[j]);
76  }
77  }
78 }

Member Data Documentation

◆ lambda

double gpmp::ml::LogReg::lambda

The regularization parameter lambda

Definition at line 110 of file logreg.hpp.

◆ learning_rate

double gpmp::ml::LogReg::learning_rate

The learning rate for gradient descent optimization

Definition at line 107 of file logreg.hpp.

◆ num_iterations

int gpmp::ml::LogReg::num_iterations

The number of iterations for gradient descent

Definition at line 109 of file logreg.hpp.

◆ weights

std::vector<double> gpmp::ml::LogReg::weights

The weights learned by the logistic regression model

Definition at line 112 of file logreg.hpp.


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