Cpp ML Library  1.0.0
A library of Machine Learning Algorithmns seen from the Udemy course Machine Learning A to Z.
LogisticRegression.hpp
Go to the documentation of this file.
1 #ifndef LOGISTIC_REGRESSION_HPP
2 #define LOGISTIC_REGRESSION_HPP
3 
4 #include <vector>
5 #include <cmath>
6 #include <stdexcept>
7 #include <numeric>
8 #include <algorithm>
9 
19 public:
26  LogisticRegression(double learningRate = 0.01, int iterations = 1000, bool useBias = true)
27  : learningRate_(learningRate), iterations_(iterations), useBias_(useBias) {}
28 
34  void train(const std::vector<std::vector<double>>& features, const std::vector<int>& labels) {
35  if (features.empty() || labels.empty()) {
36  throw std::invalid_argument("Features and labels must not be empty.");
37  }
38  if (features.size() != labels.size()) {
39  throw std::invalid_argument("The number of feature vectors must match the number of labels.");
40  }
41 
42  size_t numSamples = features.size();
43  size_t numFeatures = features[0].size();
44 
45  // Validate that all feature vectors have the same size
46  for (const auto& feature : features) {
47  if (feature.size() != numFeatures) {
48  throw std::invalid_argument("All feature vectors must have the same number of elements.");
49  }
50  }
51 
52  // Initialize weights if they haven't been initialized yet
53  if (weights_.empty()) {
54  weights_ = std::vector<double>(numFeatures, 0.0);
55  if (useBias_) {
56  bias_ = 0.0;
57  }
58  }
59 
60  for (int iter = 0; iter < iterations_; ++iter) {
61  std::vector<double> gradients(numFeatures, 0.0);
62  double biasGradient = 0.0;
63 
64  for (size_t i = 0; i < numSamples; ++i) {
65  double prediction = predictProbability(features[i]);
66  double error = prediction - labels[i];
67 
68  for (size_t j = 0; j < numFeatures; ++j) {
69  gradients[j] += error * features[i][j];
70  }
71 
72  if (useBias_) {
73  biasGradient += error;
74  }
75  }
76 
77  // Update weights and bias
78  for (size_t j = 0; j < numFeatures; ++j) {
79  weights_[j] -= learningRate_ * (gradients[j] / numSamples);
80  }
81 
82  if (useBias_) {
83  bias_ -= learningRate_ * (biasGradient / numSamples);
84  }
85  }
86  }
87 
93  int predict(const std::vector<double>& features) const {
94  return predictProbability(features) >= 0.5 ? 1 : 0;
95  }
96 
102  double predictProbability(const std::vector<double>& features) const {
103  if (features.size() != weights_.size()) {
104  throw std::invalid_argument("Feature vector size does not match the number of weights.");
105  }
106 
107  double z = std::inner_product(features.begin(), features.end(), weights_.begin(), 0.0);
108  if (useBias_) {
109  z += bias_;
110  }
111  return sigmoid(z);
112  }
113 
114 private:
115  double learningRate_;
116  int iterations_;
117  std::vector<double> weights_;
118  double bias_ = 0.0;
119  bool useBias_;
120 
126  double sigmoid(double z) const {
127  if (z >= 0) {
128  double exp_neg_z = std::exp(-z);
129  return 1.0 / (1.0 + exp_neg_z);
130  } else {
131  double exp_z = std::exp(z);
132  return exp_z / (1.0 + exp_z);
133  }
134  }
135 };
136 
137 #endif // LOGISTIC_REGRESSION_HPP
Logistic Regression model for binary classification tasks.
Definition: LogisticRegression.hpp:18
void train(const std::vector< std::vector< double >> &features, const std::vector< int > &labels)
Train the model using features and labels.
Definition: LogisticRegression.hpp:34
int predict(const std::vector< double > &features) const
Predicts the class label for a given input.
Definition: LogisticRegression.hpp:93
double predictProbability(const std::vector< double > &features) const
Predicts the probability of class 1 for a given input.
Definition: LogisticRegression.hpp:102
LogisticRegression(double learningRate=0.01, int iterations=1000, bool useBias=true)
Constructor initializing the learning rate and iteration count.
Definition: LogisticRegression.hpp:26