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 #include <cassert> 00035 00036 using namespace rfmodel; 00037 00038 int ForestModel::forestTest(std::vector<double>& X, int num_rows, int num_cols) { 00039 // X is NxD, where rows are data points 00040 00041 int numTrees = NUM_TREES; 00042 std::vector<double> Ysoft(LB_CLASSES); 00043 00044 std::vector<double> model_Ysoft(LB_CLASSES); 00045 DataMatrix Xarray(X, num_rows, num_cols); 00046 00047 // Assuming initialization to 0s 00048 for (int i = 0; i < numTrees; i++) { 00049 treeModels[i].treeTest(Xarray, model_Ysoft); 00050 00051 for (int ii = 0; ii < LB_CLASSES; ii++) Ysoft[ii] += model_Ysoft[ii]; 00052 } 00053 return classes[DataMatrix(Ysoft, LB_CLASSES, 1).maxIndex()]; 00054 }