// Copyright (C) 2001 Jean-Marc Valin #include "FeatureMap.h" #include #include "ObjectParser.h" #include "misc.h" #include #ifdef HAVE_VALUES_H #include #endif #ifdef HAVE_FLOAT_H #include #endif using namespace std; namespace FD { DECLARE_TYPE(FeatureMap) //@implements FeatureMap void FeatureMap::recursiveSplit (const vector &inData, const vector &outData, int level) { if (level <= 0) //if (data.size() < 50) { DYN_VEC(float, outDimension, mean); //float mean[outDimension]; for (int i=0;i firstInData; vector secondInData; vector firstOutData; vector secondOutData; for (int i=0;irecursiveSplit(firstInData, firstOutData, level-1); second->recursiveSplit(secondInData, secondOutData, level-1); } void FeatureMap::split(const vector &inData, const vector &outData, int &bestDim, float &bestThreshold) { bestDim=0; int nbEqual=0; bestThreshold=0; float bestMutual = -FLT_MAX; for (int i=0;i bestMutual) { isBest=true; nbEqual=0; } if (currentMutual == bestMutual) { nbEqual++; if (rand()%nbEqual==0) isBest=true; } if (isBest) { bestMutual=currentMutual; bestDim=i; bestThreshold=threshold; } } cerr << "bestDim: " << bestDim << " bestThreshold: " << bestThreshold << endl; //if (some condition on bestMutual) don't perform the split //splitWithThreshold(data, bestDim, bestThreshold); } static int float_less(const void *a, const void *b) { return *((float *)a) < *((float *)b); } //find threshold using split at median and mutual information void FeatureMap::findThreshold(const vector &inData, const vector &outData, int dim, float &thresh, float &score) { float sum = 0; int i,k; if (inData.size()==0) thresh=0; else { //FIXME: Allocation for sorted should be done faster, but with risk of //smashing the stack //float sorted[inData.size()]; float *sorted = new float [inData.size()]; for (i=0;i thresh) greater=true; if (inData[k][dim] == thresh) greater=rand()&1; if (greater) { for (i=0;isetNumbering(first->setNumbering(start)); } } void FeatureMap::calc(const float *in, float *out) { if (terminal) { //cerr << "found!\n"; //cerr << mapData.size() << endl; for (int i=0;icalc(in, out); else second->calc(in, out); } /* int FeatureMap::belongs(float *vect) const { if (terminal) return cellID; if (vect[splitDimension] < threshold) return first->belongs(vect); else return second->belongs(vect); } void FeatureMap::calcTemplate (const vector &features, vector &templ) const { for (vector::const_iterator feature = features.begin(); feature < features.end(); feature++) { //cerr << "(" << (*feature)[0] << "," << (*feature)[1] << "): " << belongs(*feature) << endl; templ[belongs(*feature)]++; } } */ void FeatureMap::printOn(ostream &out) const { out << "" << endl; out << "" << endl; //out << "" << endl; out << "" << endl; if (terminal) { out << "" << endl; out << "" << endl; } else { out << "" << endl; out << "" << endl; out << "" << endl;; out << "" << endl;; } out << ">\n"; } ostream &operator << (ostream &out, const FeatureMap &cell) { cell.printOn(out); return out; } void FeatureMap::readFrom (istream &in) { string tag; while (1) { char ch; in >> ch; if (ch == '>') break; in >> tag; if (tag == "inDimension") in >> inDimension; else if (tag == "outDimension") in >> outDimension; //else if (tag == "numberClasses") // in >> numberClasses; else if (tag == "terminal") in >> terminal; else if (tag == "cellID") in >> cellID; else if (tag == "threshold") in >> threshold; else if (tag == "mapData") in >> mapData; else if (tag == "splitDimension") in >> splitDimension; else if (tag == "first") { FeatureMap *tmp = new FeatureMap; in >> *tmp; first = tmp; } else if (tag == "second") { FeatureMap *tmp = new FeatureMap; in >> *tmp; second = tmp; } else throw new ParsingException ("FeatureMap::readFrom : unknown argument: " + tag); if (!in) throw new ParsingException ("FeatureMap::readFrom : Parse error trying to build " + tag); in >> tag; if (tag != ">") throw new ParsingException ("FeatureMap::readFrom : Parse error: '>' expected "); } } istream &operator >> (istream &in, FeatureMap &cell) { if (!isValidType(in, "FeatureMap")) return in; cell.readFrom(in); return in; } }//namespace FD