#include "RF.h" #include #include #include struct DT{ int height; long* featureId; DT* left = nullptr; DT* right = nullptr; // split info bool terminate; double dpoint; long feature; long result; // Sparse data record double** sortedData; // for each feature, sorted data long** sortedResult; // Dense data record long*** count = nullptr;// for each feature, number of data belongs to each class and dense value double** record = nullptr;// for each feature, record each dense data long* max = nullptr;// number of dense value of each feature //long* T; // number of data in each class in this node double** dataRecord = nullptr;// Record the data long* resultRecord = nullptr;// Record the result long size = 0;// Size of the dataset }; RandomForest::RandomForest(long mTree, long actTree, long rTime, int h, long feature, int* s, double forg, long maxF, long noC, Evaluation eval, long r, long rb){ srand((long)clock()); Rebuild = rb; if(actTree<1)actTree=1; noTree = actTree; activeTree = actTree; treePointer = 0; if(mTreefit(newData, newResult, size); } timer++; } long* RandomForest::fitThenPredict(double** trainData, long* trainResult, long trainSize, double** testData, long testSize){ fit(trainData, trainResult, trainSize); long* testResult = (long*)malloc(testSize*sizeof(long)); for(long i=0; iFree(); delete DTrees[(treePointer+activeTree)%maxTree]; }else{ noTree++; } DTrees[(treePointer+activeTree)%maxTree] = new DecisionTree(height, f, sparse, forget, maxFeature, noClasses, e, retain, Rebuild); long size = DTrees[(treePointer+activeTree-1)%maxTree]->DTree->size; double** newData = new double*[size]; long* newResult = new long[size]; for(long j = 0; jDTree->dataRecord[j][k]; } newResult[j] = DTrees[(treePointer+activeTree-1)%maxTree]->DTree->resultRecord[j]; } DTrees[(treePointer+activeTree)%maxTree]->fit(newData, newResult, size); DTrees[treePointer]->Stablelize(); if(++treePointer==maxTree)treePointer=0; } long RandomForest::Test(double* data){ long i; long predict[noClasses]; for(i=0; iTest(data, DTrees[i]->DTree)]++; } long ret = 0; for(i=1; ipredict[ret])ret = i; } return ret; }