00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00028
00029
00030
00031
00032
00033
00034
00035
00036 #ifndef _ADABOOST
00037 #define _ADABOOST
00038
00039 #include "dataset.h"
00040 #include <fstream>
00041 #include <iostream>
00042 #include <string>
00043 #include <vector>
00044
00045
00046 class mlABTree;
00047
00048 class adaboost {
00049 private:
00050
00051 static double epsilon;
00052 static int verbose;
00053 static bool option_initialize_weights;
00054
00055
00056 struct wr_holder {
00057 mlABTree *rule;
00058 wr_holder *next;
00059 };
00060
00061
00062
00063 wr_holder *first;
00064 wr_holder *last;
00065 wr_holder *pcl_pointer;
00066 int nrules;
00067 int nlabels;
00068 vector<string> labels;
00069 string label_others;
00070
00071
00072 ofstream *out;
00073
00074
00075 struct {
00076 int n_rounds;
00077 int max_depth;
00078 } SC;
00079
00080
00081 int stopping_criterion(int nrounds);
00082 void initialize_weights(dataset *ds);
00083 void update_weights(mlABTree *wr, double Z, dataset *ds);
00084 void add_weak_rule(mlABTree *wr);
00085
00086
00087 adaboost(const adaboost &old_bab);
00088
00089 public:
00090
00091 adaboost(int nl);
00092 adaboost(const string &file);
00093 ~adaboost();
00094 int n_rules();
00095
00096 int get_nlabels() {return nlabels;}
00097 string get_label(int lb) {return labels[lb];}
00098 string default_class() {return label_others;}
00099
00100
00101
00102
00103 void classify(input *i, double pred[]);
00104
00105
00106 void pcl_ini_pointer();
00107 int pcl_advance_pointer(int steps);
00108
00109
00110 void pcl_classify(input *i, double *pred, int nrules);
00111
00112
00113 void learn(dataset *ds, int nrounds, int maxdepth);
00114
00115
00116
00117 void set_output(ofstream &os);
00118 void read_from_stream(ifstream &in);
00119 void read_from_file(char* file);
00120
00121 static void set_verbose(int level);
00122 static void set_epsilon(double eps);
00123 static void set_initialize_weights(bool b);
00124 };
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135 class mlABTree {
00136
00137 private:
00138
00139 int feature;
00140 mlABTree **sons;
00141 double *predictions;
00142
00143
00144 static int nlabels;
00145 static double epsilon;
00146 static int max_depth;
00147 static int *used_features;
00148 static int verbose;
00149
00150
00151 static mlABTree* learn_0(dataset *ds, double *Z, int depth);
00152 static int stopping_criterion(dataset *ds, int depth);
00153
00154 static int best_feature(dataset *ds, double *W);
00155
00156
00157 static double Zcalculus(double *W, int ndim);
00158
00159 static void Cprediction(int v, double *W, double result[]);
00160
00161
00162 mlABTree(const mlABTree &wr0);
00163
00164 public:
00165
00166 static void set_nlabels(int nl);
00167 static void set_verbose(int level);
00168 static void set_epsilon(double eps);
00169
00170
00171
00172
00173 mlABTree(double *p0);
00174 mlABTree(int f, mlABTree *wrFalse, mlABTree *wrTrue);
00175 ~mlABTree();
00176
00177
00178
00179
00180 void classify(input *i,double *pred);
00181
00182
00183 void print(char *carry);
00184 void write_to_stream(ofstream &os);
00185 static mlABTree* read_from_stream(istream &is);
00186
00187
00188 static mlABTree* learn(dataset *ds, double *Z, int max_depth0);
00189 };
00190
00191
00192
00193 #endif