2 //////////////////////////////////////////////////////////////////////////////////
3 // This program is free software: you can redistribute it and/or modify //
4 // it under the terms of the version 3 of the GNU General Public License //
5 // as published by the Free Software Foundation. //
7 // This program is distributed in the hope that it will be useful, but //
8 // WITHOUT ANY WARRANTY; without even the implied warranty of //
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU //
10 // General Public License for more details. //
12 // You should have received a copy of the GNU General Public License //
13 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
15 // Written by Francois Fleuret //
16 // Copyright (C) Ecole Polytechnique Federale de Lausanne //
17 // Contact <francois.fleuret@epfl.ch> for comments & bug reports //
18 //////////////////////////////////////////////////////////////////////////////////
20 // $Id: classifier.h,v 1.3 2007-08-23 08:36:50 fleuret Exp $
31 #include "fastentropy.h"
33 class FeatureSelector;
36 // This keeps track of the number of references
42 int nb_samples, nb_features;
45 uint32_t *y_va, **x_va;
47 DataSet(int nb_samples, int nb_features);
48 DataSet(const DataSet &ds);
49 DataSet(const DataSet &ds, const FeatureSelector &fs);
50 DataSet(const DataSet &ds, bool *selected_samples);
51 DataSet &operator = (const DataSet &ds);
53 void copy(const DataSet &ds);
54 void save_ascii(ostream &os);
57 //////////////////////////////////////////////////////////////////////
58 // The classifier ////////////////////////////////////////////////////
59 //////////////////////////////////////////////////////////////////////
64 static Classifier *load(istream &is);
65 virtual ~Classifier();
66 virtual void predict(const DataSet &ds, float *result) = 0;
67 virtual void save(ostream &out) = 0;
68 virtual void inner_load(istream &in) = 0;
71 class FeatureSelector {
73 int nb_selected_features;
76 // Those remains from the feature selection process. They can be
77 // used as-is in the case of adaboost
80 FeatureSelector(istream &is);
81 FeatureSelector(int nb_selected_features);
84 // Selects features according to the Conditional Mutual Information Maximisation
85 void cmim(const DataSet &ds);
87 // Selects features according to the Mutual Information Maximisation
88 void mim(const DataSet &ds);
90 // Selects random features
91 void random(const DataSet &ds);
93 void save(ostream &os);
96 class LinearClassifier : public Classifier {
102 LinearClassifier(int nb_features);
103 virtual ~LinearClassifier();
105 void compute_bayesian_weights(int nb_samples, uint32_t *y_va, uint32_t **x_va);
106 void compute_bias(int nb_samples, uint32_t *y_va, uint32_t **x_va, bool balanced_error);
108 void learn_bayesian(const DataSet &ds, bool balanced_error);
109 void learn_perceptron(const DataSet &ds, bool balanced_error);
111 virtual void predict(const DataSet &ds, float *result);
112 virtual void save(ostream &out);
113 virtual void inner_load(istream &is);
116 void compute_error_rates(FeatureSelector *selector, Classifier *classifier,
117 const DataSet &testing_set, int &n00, int &n01, int &n10, int &n11, float *result);