forked from BRAINSia/BRAINSStandAlone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogisticRegression.hxx
167 lines (140 loc) · 5.11 KB
/
LogisticRegression.hxx
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
156
157
158
159
160
161
162
163
164
165
166
#include "LogisticRegression.h"
#include "linear.h"
#include <map>
#include <vector>
template <typename TSampleType>
LogisticRegressionSample<TSampleType>::LogisticRegressionSample(const unsigned int featureCount)
{
this->m_sample = new std::vector<TSampleType>(featureCount+1);
this->m_label = 0;
this->m_labelSet = false;
}
template <typename TSampleType>
LogisticRegressionSample<TSampleType>::~LogisticRegressionSample()
{
delete this->m_sample;
}
template <typename TSampleType>
double LogisticRegressionSample<TSampleType>::GetLabelProbability(unsigned int const & label)
{
return this->m_predictedProbability[label];
}
template <typename TSampleType>
void LogisticRegressionSample<TSampleType>::SetLabelProbability(unsigned int const &label, double const & probability)
{
this->m_predictedProbability[label] = probability;
}
template <typename TSampleType>
std::vector<TSampleType> const * LogisticRegressionSample<TSampleType>::GetSample() const
{
return this->m_sample;
}
template <typename TSampleType>
void LogisticRegressionSample<TSampleType>::SetSample(std::vector<TSampleType> & sample)
{
*this->m_sample = sample;
}
template <typename TSampleType>
void LogisticRegressionSample<TSampleType>::SetLabel(unsigned int const & label)
{
this->m_label = label;
this->m_labelSet = true;
}
template <typename TSampleType>
LogisticRegression<TSampleType>::LogisticRegression(const unsigned int featureCount, const unsigned int totalSamples) :
m_totalSamples(totalSamples),
m_featureCount(featureCount),
m_classOneLabel(0),
m_classTwoLabel(0),
m_classOneLabelSet(false),
m_classTwoLabelSet(false)
{
this->m_sampleCount = 0;
this->m_parameters.solver_type = L1R_LR;
this->m_parameters.C = 1;
this->m_parameters.eps = 0.01;
this->m_parameters.nr_weight = 0;
this->m_parameters.weight_label = NULL;
this->m_parameters.weight = NULL;
this->m_problem.bias = 1;
this->m_problem.n = this->m_problem.bias + this->m_featureCount;
this->m_problem.l = this->m_totalSamples;
this->m_problem.y = new int [totalSamples];
this->m_problem.x = new struct feature_node * [this->m_problem.l];
this->m_featureNodes = new struct feature_node [this->m_problem.n*this->m_problem.l];
}
template <typename TSampleType>
LogisticRegression<TSampleType>::~LogisticRegression()
{
delete this->m_problem.y;
delete this->m_problem.x;
delete this->m_featureNodes;
destroy_param(&this->m_parameters);
}
template <typename TSampleType>
void LogisticRegression<TSampleType>::AddLabeledSample(LogisticRegressionSample<TSampleType> const & labeledSample)
{
assert(labeledSample.LabelIsSet());
std::vector<TSampleType> const * const samples = labeledSample.GetSample();
struct feature_node * currentNode;
this->m_problem.x[this->m_sampleCount] = &this->m_featureNodes[this->m_problem.n*this->m_sampleCount];
this->m_problem.y[this->m_sampleCount] = labeledSample.GetLabel();
for(unsigned int i=0; i<samples->size(); ++i)
{
currentNode = &this->m_featureNodes[this->m_problem.n*this->m_sampleCount + i];
currentNode->index = i+1;
currentNode->value = samples->at(i);
}
currentNode = &this->m_featureNodes[this->m_problem.n*this->m_sampleCount + samples->size()];
currentNode->index = -1;
this->m_sampleCount++;
}
template <typename TSampleType>
void LogisticRegression<TSampleType>::TrainModel()
{
assert(this->m_totalSamples >= this->m_sampleCount);
if(this->m_totalSamples > this->m_sampleCount)
{
m_problem.l = m_sampleCount;
}
this->m_model = train(&this->m_problem, &this->m_parameters);
}
template <typename TSampleType>
void LogisticRegression<TSampleType>::SetClassOneLabel(const unsigned int classLabel)
{
this->m_classOneLabel = classLabel;
this->m_classOneLabelSet = true;
}
template <typename TSampleType>
void LogisticRegression<TSampleType>::SetClassTwoLabel(const unsigned int classLabel)
{
this->m_classTwoLabel = classLabel;
this->m_classTwoLabelSet = true;
}
template <typename TSampleType>
void LogisticRegression<TSampleType>::ClassifySample(LogisticRegressionSample<TSampleType> & labeledSample)
{
//TODO Check if the class labels have been set and throw an exception if they haven't.
assert(this->m_classTwoLabelSet && this->m_classOneLabelSet);
std::vector<TSampleType> const * const samples = labeledSample.GetSample();
struct feature_node * sampleToPredict = new struct feature_node[this->m_problem.n];
for(unsigned int i=0; i<samples->size(); ++i)
{
sampleToPredict[i].index = i+1;
sampleToPredict[i].value = samples->at(i);
}
sampleToPredict[samples->size()].index = -1;
double predictedProbabilities[2];
predict_probability(this->m_model,sampleToPredict,predictedProbabilities);
if(this->m_classOneLabel > this->m_classTwoLabel)
{
labeledSample.SetLabelProbability(this->m_classOneLabel,predictedProbabilities[1]);
labeledSample.SetLabelProbability(this->m_classTwoLabel,predictedProbabilities[0]);
}
else
{
labeledSample.SetLabelProbability(this->m_classOneLabel,predictedProbabilities[0]);
labeledSample.SetLabelProbability(this->m_classTwoLabel,predictedProbabilities[1]);
}
delete [] sampleToPredict;
}