7 #include "FeatureSelection.hpp"
11 namespace featselect {
12 template<
typename T =
double>
17 struct fisher_select_score {
22 static int fisher_select_compare_score_greater(
const fisher_select_score &a,
const fisher_select_score &b);
34 const Fisher::fisher_select_score &b) {
35 return a.score < b.score;
40 this->samples = mltk::make_data<T>(samples);
41 this->classifier = classifier;
42 this->final_dim = final_dim;
43 this->number = final_dim;
49 size_t num_pos = 0, num_neg = 0, svs = 0, dim = this->samples->
dim(), size = this->samples->size();
52 std::vector<int> remove(dim - number, -1), fnames;
53 std::vector<double> avg_neg(dim), avg_pos(dim), sd_neg(dim), sd_pos(dim), w;
54 std::vector<fisher_select_score> scores(dim);
55 std::shared_ptr<Data<T> > stmp(std::make_shared<
Data<T> >()), stmp_partial(std::make_shared<
Data<T> >());
59 for (i = 0; i < dim; ++i) {
64 for (j = 0; j < size; ++j) {
65 if ((*this->samples)[j]->Y() == -1) {
66 avg_neg[i] += (*this->samples)[j]->X()[i];
69 avg_pos[i] += (*this->samples)[j]->X()[i];
73 avg_neg[i] /= num_neg;
74 avg_pos[i] /= num_pos;
78 for (i = 0; i < dim; ++i) {
81 for (j = 0; j < size; ++j) {
82 if ((*this->samples)[j]->Y() == -1)
83 sd_neg[i] += std::pow((*this->samples)[j]->X()[i] - avg_neg[i], 2);
84 else sd_pos[i] += std::pow((*this->samples)[j]->X()[i] - avg_pos[i], 2);
86 sd_neg[i] = sqrt(sd_neg[i] / (num_neg - 1));
87 sd_pos[i] = sqrt(sd_pos[i] / (num_pos - 1));
90 fnames = this->samples->getFeaturesNames();
93 for (i = 0; i < dim; ++i) {
94 scores[i].score = std::pow(avg_pos[i] - avg_neg[i], 2) / (sd_pos[i] + sd_neg[i]);
95 scores[i].fname = fnames[i];
97 std::cout <<
"Score: " << scores[i].score <<
", Fname: " << scores[i].fname << std::endl;
99 if (this->verbose) std::cout <<
"----------------------------\n";
101 if (this->verbose) std::cout <<
"Dim: " << dim <<
" -- ";
104 this->classifier->setVerbose(0);
105 this->classifier->setGamma(margin);
106 this->classifier->setSamples(this->samples);
107 if (!this->classifier->train()) {
108 w.erase(w.begin(), w.end());
109 if (this->verbose) std::cout <<
"Training failed!\n";
112 sol = this->classifier->getSolution();
113 std::cout <<
"Training sucessful...\n";
114 std::cout <<
"Margin = " << sol.
margin <<
", Support Vectors = " << sol.
svs <<
"\n";
115 std::cout <<
"----------------------------\n";
118 std::sort(scores.begin(), scores.end(), fisher_select_compare_score_greater);
120 stmp_partial->copy(*this->samples);
121 stmp->copy(*this->samples);
123 for (i = 0; i < (dim - this->number); ++i) {
124 if (this->verbose) std::cout <<
"Score: " << scores[i].score <<
", Fname: " << scores[i].fname <<
"\n";
125 remove[i] = scores[i].fname;
126 stmp->removeFeatures(remove);
129 std::cout <<
"Dim: " << dim - i - 1 <<
" -- ";
132 w.erase(w.begin(), w.end());
133 this->classifier->setGamma(margin);
134 this->classifier->setSamples(stmp);
135 if (!this->classifier->train()) {
136 if (this->verbose) std::cout <<
"Training failed!\n";
140 sol = this->classifier->getSolution();
141 std::cout <<
"Training sucessful...\n";
142 std::cout <<
"Margin = " << sol.
margin <<
", Support Vectors = " << sol.
svs <<
"\n";
143 std::cout <<
"----------------------------\n";
145 stmp_partial.reset();
146 stmp_partial = std::make_shared<Data<T> >();
147 stmp_partial->copy(*stmp);
152 return *stmp_partial;
154 stmp_partial.reset();
size_t dim() const
Returns the dimension of the dataset.
Definition: Data.hpp:213
Definition: Solution.hpp:13
unsigned int svs
Number of support Vectors.
Definition: Solution.hpp:31
double margin
Margin generated from the classifier that generated the solution.
Definition: Solution.hpp:27
Definition: classifier/Classifier.hpp:17
Definition: featselect/FeatureSelection.hpp:17
classifier::Classifier< double > * classifier
Classifier used by the method.
Definition: featselect/FeatureSelection.hpp:23
std::shared_ptr< Data< double > > samples
Attributes.
Definition: featselect/FeatureSelection.hpp:21
Definition: Fisher.hpp:13
Data< T > selectFeatures() override
Function that executes the feature selection phase.
Definition: Fisher.hpp:47
UFJF-MLTK main namespace for core functionalities.
Definition: classifier/Classifier.hpp:11