-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogisticregression.cpp
More file actions
155 lines (136 loc) · 4.27 KB
/
logisticregression.cpp
File metadata and controls
155 lines (136 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <numeric>
using std::cout, std::endl, std::string, std::vector, std::inner_product, std::cin, std::stod;
static vector<vector<double>> X;
static vector<double> Y;
static vector<double> theta;
double g(double z) { // Sigmoid function
return 1/(1 + exp(-1 * z));
}
double dot(vector<double>& v1, vector<double>& v2) { // Dot product
return inner_product(v1.begin(), v1.end(), v2.begin(), 0);
}
double likely(int num_of_training_ex) { // Log likelyhood function
double sum = 0;
for (int i = 0; i < num_of_training_ex; ++i) {
double h = g(dot(theta, X[i]));
sum += Y[i] * log(h) + (1-Y[i]) * log(1-h);
}
return sum;
}
vector<string> split_string(string str, char delimiter){
vector<string> result;
string current = "";
for(int i = 0; i < str.size(); ++i){
if(str[i] == delimiter){
if(current != ""){
result.push_back(current);
current = "";
}
continue;
}
current += str[i];
}
if(current.size() != 0){
result.push_back(current);
}
return result;
}
void get_training_data() {
int num_of_training_ex = 0;
string x_values = "";
string y_value = "";
while (true) {
num_of_training_ex++;
cout << "Enter training example x-value(s) #" << num_of_training_ex << ", seperated by a comma (Type 'Q' to finish): ";
cin >> x_values;
if (x_values == "Q") {
break;
}
vector<string> x_values_vec = split_string(x_values, ',');
vector<double> x_values_to_push = {1};
for (int i = 0; i < x_values_vec.size(); ++i) {
x_values_to_push.push_back(stod(x_values_vec[i]));
}
X.push_back(x_values_to_push);
cout << endl;
do {
cout << "Enter training example y-value {0, 1} #" << num_of_training_ex << ": ";
cin >> y_value;
} while (y_value != "0" && y_value != "1");
Y.push_back(stod(y_value));
}
for (int i = 0; i < X[0].size(); ++i) { //setting thetas to 0's
theta.push_back(0);
}
}
void find_optimal_theta_parameters() {
double learning_rate = 0.01;
bool is_converged = false;
int iters = 0;
double epsilon = 0.01;
while (!is_converged) {
vector<double> temp(theta.size(), 0);
for (int j = 0; j < theta.size(); ++j) {
double result = 0;
for (int i = 0; i < X.size(); ++i) {
double h = g(dot(theta, X[i]));
result += (Y[i] - h) * X[i][j];
}
temp[j] = theta[j] + learning_rate * result;
}
theta = temp;
iters++;
if (iters == 100000 || fabs(likely(X.size())) <= epsilon) {
is_converged = true;
}
}
}
void print_equation() {
cout << "Predicted decision boundary equation: Y = ";
for (int i = 0; i < theta.size(); ++i) {
if (i == 0) {
cout << theta[i] << " + ";
}else if (i == theta.size() - 1){
cout << theta[i] << "x" << i << endl;;
}else{
cout << theta[i] << "x" << i << " + ";
}
}
}
void print_probability() {
cout << "Probability of predictions accurate: " << exp(likely(X.size())) << endl;
}
void predict() { // Asks for inputs and displays coressponding prediction
string x_values;
while (true) {
cout << "Enter your x-values that you want to predict for (comma is delimiter, Q to quit): ";
cin >> x_values;
if (x_values == "Q") {
break;
}
vector<string> x_values_string_vec = split_string(x_values, ',');
vector<double> x_values_vec = {1};
for (int i = 0; i < x_values_string_vec.size(); ++i) {
x_values_vec.push_back(stod(x_values_string_vec[i]));
}
double h = g(dot(theta, x_values_vec));
cout << "Model's prediction: ";
if (h >= 0.5) { // if probablity >= 0.5
cout << 1 << endl;
}else{
cout << 0 << endl;
}
}
}
int main() {
get_training_data();
find_optimal_theta_parameters();
print_equation();
print_probability();
predict();
return 0;
}