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
00027
00028
00029
00030
00031
00032
00033 #include "RandomForestModel.h"
00034
00035 using namespace rfmodel;
00036
00037
00038 void TreeModel::treeTest(const DataMatrix& X, std::vector<double>& Ysoft) const {
00039
00040 int N = X.num_rows;
00041 int D = X.num_cols;
00042
00043 int nd = (int)pow(2, DEPTH) - 1;
00044
00045 int numInternals = (nd + 1) / 2 - 1;
00046
00047
00048 std::vector<double> dataix(N * nd, 0.0);
00049
00050 for (int n = 1; n <= numInternals; n++) {
00051 DataMatrix* reld;
00052 DataMatrix* Xrel;
00053
00054
00055 if (n == 1) {
00056 reld = new DataMatrix(N, 1, true);
00057 Xrel = new DataMatrix(X.data, N, D);
00058 } else {
00059 int select_cols[1];
00060 select_cols[0] = n - 1;
00061 DataMatrix dataix_array(dataix, N, D);
00062 DataMatrix reld_resp(dataix_array.num_rows, 1);
00063 dataix_array.subset_cols(select_cols, 1, reld_resp);
00064 reld = new DataMatrix(reld_resp.num_rows, reld_resp.num_cols);
00065 reld_resp.findValue(1, *reld);
00066 int* select = new int[reld->num_rows * reld->num_cols];
00067 int c = 0;
00068 for (int i = 0; i < reld->num_rows * reld->num_cols; i++)
00069 if (reld->data[i] == 1) select[c++] = i;
00070 Xrel = new DataMatrix(c, X.num_cols);
00071 X.subset_rows(select, c, *Xrel);
00072 delete [] select;
00073 }
00074
00075 if (Xrel->num_rows * Xrel->num_cols == 0) {
00076 delete reld;
00077 delete Xrel;
00078 continue;
00079 }
00080
00081 double yhat = weakModels[n - 1].weakTest(*Xrel);
00082
00083 for (int i = 0; i < reld->num_rows * reld->num_cols; i++)
00084 dataix[(((int)reld->data[i] - 1) * nd + 2 * n) - 1] = yhat;
00085
00086 for (int i = 0; i < reld->num_rows * reld->num_cols; i++)
00087 dataix[(((int)reld->data[i] - 1) * nd + 2 * n + 1) - 1] = 1.0 - yhat;
00088
00089 delete reld;
00090 delete Xrel;
00091 }
00092
00093
00094 for (int n = ((nd + 1) / 2); n < nd; n++) {
00095 int select_cols[1];
00096 select_cols[0] = n - 1;
00097
00098 DataMatrix dataix_array(dataix, N, nd);
00099 DataMatrix dataix_subset(dataix_array.num_rows, 1);
00100 dataix_array.subset_cols(select_cols, 1, dataix_subset);
00101
00102 DataMatrix ff(dataix_subset.num_rows, dataix_subset.num_cols);
00103 dataix_subset.findIndicesNE(0.0, ff);
00104
00105 int select_rows[1];
00106 select_rows[0] = n - (nd + 1) / 2 + 1 - 1;
00107 DataMatrix leafdist_array(leafdist, l_X, l_D);
00108 DataMatrix hc(1, leafdist_array.num_cols);
00109 leafdist_array.subset_rows(select_rows, 1, hc);
00110
00111 if (ff.num_rows * ff.num_cols > 0) {
00112 DataMatrix rep_matrix(hc.num_rows * ff.num_rows * ff.num_cols, hc.num_cols);
00113 hc.repmat(ff.num_rows * ff.num_cols, 1, rep_matrix);
00114 std::copy(rep_matrix.data.begin(), rep_matrix.data.end(), Ysoft.begin());
00115 }
00116 }
00117 }