-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogesticRegression.cpp
More file actions
149 lines (132 loc) · 10.7 KB
/
Copy pathLogesticRegression.cpp
File metadata and controls
149 lines (132 loc) · 10.7 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
//https://github.com/yacineMahdid/artificial-intelligence-and-machine-learning/blob/master/linear_regression_in_cpp/main.cpp
//https://machinelearningmastery.com/logistic-regression-tutorial-for-machine-learning/
#include <iostream>
#include <math.h>
#include <vector>
#include <stdlib.h>
// #include "DataDictionary.cpp"
using namespace std;
bool custom_sort(double a, double b) /* this custom sort function is defined to
sort on basis of min absolute value or error*/
{
double a1 = abs(a - 0);
double b1 = abs(b - 0);
return a1 < b1;
}
const uint epoch = 4;
void train(std::vector<DataDictionary> dataDict, unordered_map<unsigned int, unsigned int[2]> &MinMax)
{
vector<double> error; // for storing the error values
double err; // for calculating error on each stage
double b0 = 0; // initializing b0
double b1 = 0; // initializing b1
double b2 = 0; // initializing b2
double b3 = 0; // initializing b3
double b4 = 0; // initializing b4
double b5 = 0; // initializing b5
double b6 = 0; // initializing b6
double b7 = 0; // initializing b7
double b8 = 0; // initializing b8
double b9 = 0; // initializing b9
double b10 = 0; // initializing b10
double b11 = 0; // initializing b11
double b12 = 0;
double alpha = 0.18; // initializing our learning rate
double e = 2.71828;
// town = _town;
// storeId = _storeId;
// managerName = _managerName;
// staffNumbers = _staffNumbers;
// space = _space;
// carParking = _carParking;
// demographicScore = _demographicScore;
// location = _location;
// populations = _populations;
// storeAge = _storeAge;
// clearenceSpace = _clearenceSpace;
// competitionNum = _competitionNum;
// competitionScore = _competitionScore;
// performance = _performance;
/*Training Phase*/
double totalPopulation = 0;
cout << dataDict.size();
for (int i = 0; i < 100 * epoch; i++)
{
//Since there are 10 values in our dataset and we want to run for 4 epochs so total for loop run 40 times
int idx = i % 100; //for accessing index after every epoch
double normDemographic = (double)(dataDict[idx].GetDemographicScore() - MinMax[DEMOGRAPHIC_SCORE][0]) / (double)(MinMax[DEMOGRAPHIC_SCORE][1] - MinMax[DEMOGRAPHIC_SCORE][0]);
double normCompetition = (double)(dataDict[idx].CompetitionScore() - MinMax[COMPETITION_SCORE][0]) / (double)(MinMax[COMPETITION_SCORE][1] - MinMax[COMPETITION_SCORE][0]);
double normSpace = (double)(dataDict[idx].GetSpace() - MinMax[SPACE][0]) / (double)(MinMax[SPACE][1] - MinMax[SPACE][0]);
double normClearence = (double)(dataDict[idx].ClearenceSpace() - MinMax[CLEARENCE_SPACE][0]) / (double)(MinMax[CLEARENCE_SPACE][1] - MinMax[CLEARENCE_SPACE][0]);
double normStaffs = (double)(dataDict[idx].GetStaffNumbers() - MinMax[STAFF_NUMBERS][0]) / (double)(MinMax[STAFF_NUMBERS][1] - MinMax[STAFF_NUMBERS][0]);
double normCompeteNum = (double)(dataDict[idx].CompetitionNumber() - MinMax[COMPETITION_NUM][0]) / (double)(MinMax[COMPETITION_NUM][1] - MinMax[COMPETITION_NUM][0]);
// double population40Avg = dataDict[idx].Population_40().population / dataDict[idx].Population_40().distanceInMins;
// double population30Avg = dataDict[idx].Population_30().population / dataDict[idx].Population_30().distanceInMins;
// double population20Avg = dataDict[idx].Population_20().population / dataDict[idx].Population_20().distanceInMins;
// double population10Avg = dataDict[idx].Population_10().population / dataDict[idx].Population_10().distanceInMins;
totalPopulation = dataDict[idx].Population_40().population + dataDict[idx].Population_30().population + dataDict[idx].Population_20().population + dataDict[idx].Population_10().population;
// TODO: Can put log here instead of normalization
double p = -(b0 + b1 * normStaffs + b2 * normSpace + b3 * dataDict[idx].GetCarParking() + b4 * normDemographic +
b6 * ((double)(dataDict[idx].Population_40().population / totalPopulation)) + b7 * ((double)(dataDict[idx].Population_30().population / totalPopulation)) + b8 * ((double)(dataDict[idx].Population_20().population / totalPopulation)) +
b9 * ((double)(dataDict[idx].Population_10().population / totalPopulation)) + b10 * normClearence + b11 * normCompetition + b12 * normCompeteNum);
// double p = -(b0 + b1 * normStaffs + b2 * normSpace + b3 * dataDict[idx].GetCarParking() + b4 * normDemographic +
// b5 * (double)log10((double)dataDict[i].GetLocation()) + b6 * ((double)(dataDict[idx].Population_40().population / totalPopulation)) +
// b7 * ((double)(dataDict[idx].Population_30().population / totalPopulation)) + b8 * ((double)(dataDict[idx].Population_20().population / totalPopulation)) +
// b9 * ((double)(dataDict[idx].Population_10().population / totalPopulation)) + b10 * normClearence + b11 * normCompetition + b12 * normCompeteNum);
//making the prediction
double pred = 1.0 / (1.0 + pow(e, p)); //calculating final prediction applying sigmoid
err = dataDict[idx].GetPerformance() - pred;
b0 = b0 - alpha * err * pred * (1 - pred) * 1.0; //updating b0 (BIAS)
b1 = b1 + alpha * err * pred * (1 - pred) * normStaffs; //updating b1
b2 = b2 + alpha * err * pred * (1 - pred) * normSpace; //updating b2
b3 = b3 + alpha * err * pred * (1 - pred) * dataDict[idx].GetCarParking(); //updating b1
b4 = b4 + alpha * err * pred * (1 - pred) * normDemographic; //updating b2 b1 = b1 + alpha * err * pred * (1 - pred) * x1[idx]; //updating b1
// b5 = b5 + alpha * err * pred * (1 - pred) * (double)log10((double)dataDict[i].GetLocation()); //updating b2 b1 = b1 + alpha * err * pred * (1 - pred) * x1[idx]; //updating b1
b6 = b6 + alpha * err * pred * (1 - pred) * (double)(dataDict[idx].Population_40().population / totalPopulation); //updating b2 b1 = b1 + alpha * err * pred * (1 - pred) * x1[idx]; //updating b1
b7 = b7 + alpha * err * pred * (1 - pred) * (double)(dataDict[idx].Population_30().population / totalPopulation); //updating b2 b1 = b1 + alpha * err * pred * (1 - pred) * x1[idx]; //updating b1
b8 = b8 + alpha * err * pred * (1 - pred) * (double)(dataDict[idx].Population_20().population / totalPopulation); //updating b2 b1 = b1 + alpha * err * pred * (1 - pred) * x1[idx]; //updating b1
b9 = b9 + alpha * err * pred * (1 - pred) * (double)(dataDict[idx].Population_10().population / totalPopulation); //updating b2 b1 = b1 + alpha * err * pred * (1 - pred) * x1[idx]; //updating b1
b10 = b10 + alpha * err * pred * (1 - pred) * normClearence; //updating b2 b1 = b1 + alpha * err * pred * (1 - pred) * x1[idx]; //updating b1
b11 = b11 + alpha * err * pred * (1 - pred) * normCompetition; //updating b2
b12 = b12 + alpha * err * pred * (1 - pred) * normCompeteNum;
// cout << "B0=" << b0 << " "
// << "B1=" << b1 << " "
// << "B2=" << b2 << " error=" << err << endl; // printing values after every step
error.push_back(err);
// std::cout << pred << std::endl;
}
sort(error.begin(), error.end(), custom_sort); //custom sort based on absolute error difference
/*Testing Phase*/
// DataDictionary test1, test2; //enter test x1 and x2
uint totalCorrect = 0;
for (int i = 0; i < dataDict.size(); i++)
{
double normDemographic = (double)(dataDict[i].GetDemographicScore() - MinMax[DEMOGRAPHIC_SCORE][0]) / (double)(MinMax[DEMOGRAPHIC_SCORE][1] - MinMax[DEMOGRAPHIC_SCORE][0]);
double normCompetition = (double)(dataDict[i].CompetitionScore() - MinMax[COMPETITION_SCORE][0]) / (double)(MinMax[COMPETITION_SCORE][1] - MinMax[COMPETITION_SCORE][0]);
double normClearence = (double)(dataDict[i].ClearenceSpace() - MinMax[CLEARENCE_SPACE][0]) / (double)(MinMax[CLEARENCE_SPACE][1] - MinMax[CLEARENCE_SPACE][0]);
double normSpace = (double)(dataDict[i].GetSpace() - MinMax[SPACE][0]) / (double)(MinMax[SPACE][1] - MinMax[SPACE][0]);
double normStaffs = (double)(dataDict[i].GetStaffNumbers() - MinMax[STAFF_NUMBERS][0]) / (double)(MinMax[STAFF_NUMBERS][1] - MinMax[STAFF_NUMBERS][0]);
double normCompeteNum = (double)(dataDict[i].CompetitionNumber() - MinMax[COMPETITION_NUM][0]) / (double)(MinMax[COMPETITION_NUM][1] - MinMax[COMPETITION_NUM][0]);
totalPopulation = dataDict[i].Population_40().population + dataDict[i].Population_30().population + dataDict[i].Population_20().population + dataDict[i].Population_10().population;
double pred = (b0 + b1 * normStaffs + b2 * normSpace + b3 * dataDict[i].GetCarParking() + b4 * normDemographic +
b6 * ((double)(dataDict[i].Population_40().population / totalPopulation)) + b7 * ((double)(dataDict[i].Population_30().population / totalPopulation)) + b8 * ((double)(dataDict[i].Population_20().population / totalPopulation)) +
b9 * ((double)(dataDict[i].Population_10().population / totalPopulation)) + b10 * normClearence + b11 * normCompetition + b12 * normCompeteNum);
// double pred = b0 + b1 * normStaffs + b2 * normSpace + b3 * dataDict[i].GetCarParking() + b4 * normDemographic +
// b5 * (double)log10((double)dataDict[i].GetLocation()) + b6 * ((double)(dataDict[i].Population_40().population / totalPopulation)) +
// b7 * ((double)(dataDict[i].Population_30().population / totalPopulation)) + b8 * ((double)(dataDict[i].Population_20().population / totalPopulation)) +
// b9 * ((double)(dataDict[i].Population_10().population / totalPopulation)) + b10 * normClearence + b11 * normCompetition + b12 * normCompeteNum;
//make prediction
// cout << "The value predicted by the model= " << pred << endl;
if (pred > 0.5)
pred = 1;
else
pred = 0;
cout << "The class predicted by the model= " << dataDict[i].GetStoreID() << " #### " << pred << endl;
if (pred == dataDict[i].GetPerformance())
totalCorrect++;
}
cout << "Accuracy = " << (totalCorrect * 100) / dataDict.size() << endl;
}
//Histogram Error
//https://stackoverflow.com/questions/11774822/matplotlib-histogram-with-errorbars
//https://github.com/Saleh-I/Histogram/blob/master/Histogram/Main.cpp