00001 #ifndef _CELL_H_
00002 #define _CELL_H_
00003
00004
00005
00006 #include <math.h>
00007 #include <vector>
00008 #include <iostream>
00009 #include "Object.h"
00010
00011 namespace FD {
00012
00013 class Cell;
00014
00015 inline double entropy_funct(double x)
00016 {
00017 if (x==0) {
00018
00019 return 0;
00020 }
00021 return -x*log(x);
00022 }
00023
00024 std::ostream &operator << (std::ostream &out, const Cell &cell);
00025
00026 class MutualInformation {
00027 public:
00028 inline static float mmi (std::vector<int> Nij, int Nj, std::vector<int> Ai)
00029 {
00030 float ent = 0;
00031 for (int i = 0; i < Nij.size(); i++)
00032 {
00033 float Pc = float(Nij[i]) / Nj;
00034 float Pa = float(Ai[i]) / Nij[i];
00035 ent -= Pc * log(Pc) + Pc * ( Pa * log(Pa) + (1-Pa) * log (1-Pa) );
00036 }
00037 return ent;
00038 }
00039 };
00040
00041 class Cell : public Object {
00042 protected:
00043 int dimension;
00044 int numberClasses;
00045 bool terminal;
00046 Cell *first;
00047 Cell *second;
00048 float threshold;
00049 int splitDimension;
00050 int cellID;
00051 public:
00052 Cell(int _dimension, int _numberClasses)
00053 : dimension(_dimension)
00054 , numberClasses (_numberClasses)
00055 , terminal(true)
00056 , first(NULL)
00057 , second(NULL)
00058 , cellID (-1)
00059 {}
00060
00061 Cell(){}
00062
00063 Cell (const Cell &) {std::cerr << "don't call the Cell copy constructor\n"; exit(1);}
00064
00065 ~Cell()
00066 {
00067 if (!terminal)
00068 {
00069 delete first;
00070 delete second;
00071 }
00072 }
00073
00074 void recursiveSplit (const std::vector<std::pair<int, float *> > &data, int level = 2);
00075
00076 void split(const std::vector<std::pair<int, float *> > &data, int &bestDim, float &bestThreshold);
00077
00078 void findThreshold(const std::vector<std::pair<int, float *> > &data, int dim, float &thresh, float &score);
00079
00080 int setNumbering(int start=0);
00081
00082 int belongs(float *vect) const;
00083
00084 void calcTemplate (const std::vector<float *> &features, std::vector<int> &templ) const;
00085
00086 void printOn(std::ostream &out) const;
00087
00088 void readFrom (std::istream &in);
00089
00090 friend std::istream &operator >> (std::istream &in, Cell &cell);
00091 };
00092
00093
00094 }
00095
00096 #endif