
00001 /* 00002 * The file is based on the Random-Forest-Matlab at 00003 * https://github.com/karpathy/Random-Forest-Matlab available under BSD license 00004 00005 * Copyright (c) <2014>, <Andrej Karpathy> 00006 * All rights reserved. 00007 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions are met: 00010 00011 * 1. Redistributions of source code must retain the above copyright notice, this 00012 * list of conditions and the following disclaimer. 00013 * 2. Redistributions in binary form must reproduce the above copyright notice, 00014 * this list of conditions and the following disclaimer in the documentation 00015 * and/or other materials provided with the distribution. 00016 00017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 00018 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00019 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00020 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 00021 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00022 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00023 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00024 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00025 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00026 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 00028 * The views and conclusions contained in the software and documentation are those 00029 * of the authors and should not be interpreted as representing official policies, 00030 * purpose. 00031 */ 00032 00033 #include "RandomForestModel.h" 00034 00035 using namespace rfmodel; 00036 00037 double Model::weakTest(const DataMatrix& X) const { 00038 // X is NxD 00039 double final_yhat = 1.0; 00040 int N = X.num_rows; 00041 int D = X.num_cols; 00042 #ifdef DEBUG_RF 00043 if (classifierID == 2.0) 00044 printf("\nTesting on weakmodel r1[%d], r2[%d], w[%lf,%lf,%lf]\n", r1, r2, w[0], w[1], 00045 w[2]); 00046 #endif 00047 // Retaining pseudo code for decision stump classifier, distance based learner 00048 // as placeholders to be potentially implemented later 00049 #if 0 00050 if (classifierID == 1) { 00051 // Decision stump classifier 00052 00053 yhat = (double)X(:, model->r) < model->t; 00054 00055 int* select_cols; 00056 select_cols = new int[1]; 00057 select_cols[0] = r; 00058 00059 DataMatrix *yhat_op = MatrixOp::_subset_cols(X, num_rows, num_cols, select_cols, 00060 1); 00061 yhat = yhat_op->data; 00062 final_yhat = MatrixOp::findIndicesLT(yhat,N, t); 00063 } else 00064 #endif 00065 if (classifierID == 2) { 00066 // 2-D linear classifier stump 00067 00068 int select_cols[2]; 00069 select_cols[0] = r1 - 1; 00070 select_cols[1] = r2 - 1; 00071 00072 DataMatrix subset_arr(X.num_rows, 2); 00073 X.subset_cols(select_cols, 2, subset_arr); 00074 const DataMatrix ones_arr(N, 1, true); 00075 DataMatrix combined_arr(subset_arr.num_rows, subset_arr.num_cols + ones_arr.num_rows); 00076 combined_arr.combine((const DataMatrix)subset_arr, ones_arr); 00077 const DataMatrix w_arr(w, 3, 1); 00078 DataMatrix mm(combined_arr.num_rows, w_arr.num_cols); 00079 mm.matrix_multiply(combined_arr, w_arr); 00080 00081 double zero = 0.0; 00082 DataMatrix yhat(mm.num_rows, mm.num_cols); 00083 mm.findIndicesLT(zero, yhat); 00084 if (yhat.data.size() == 0) 00085 final_yhat = 0.0; 00086 else 00087 final_yhat = yhat.data[0] + 1; 00088 } 00089 00090 return final_yhat; 00091 }
1.5.5