我之前用随机森林进行分类任务,使用这个例子作为指南来设置参数,它工作得很好。但现在我想解决一个回归问题。
我有点想法认为这与var_type Mat有关,该方法定义了随机森林训练方法中数据的类型,但是我不太确定每个标志代表什么。
对于分类任务,代码看起来像这样(从上面的链接复制):
我的问题是如何设置OpenCV随机森林,使其作为回归器运行。我已经搜索了很多,但还没有找到答案。我得到的最接近的解释是在这个答案中。然而,它仍然没有任何意义。
我正在寻找一个简单的答案,解释回归的var_type和params参数。
我有点想法认为这与var_type Mat有关,该方法定义了随机森林训练方法中数据的类型,但是我不太确定每个标志代表什么。
对于分类任务,代码看起来像这样(从上面的链接复制):
// define all the attributes as numerical
// alternatives are CV_VAR_CATEGORICAL or CV_VAR_ORDERED(=CV_VAR_NUMERICAL)
// that can be assigned on a per attribute basis
Mat var_type = Mat(ATTRIBUTES_PER_SAMPLE + 1, 1, CV_8U );
var_type.setTo(Scalar(CV_VAR_NUMERICAL) ); // all inputs are numerical
// this is a classification problem (i.e. predict a discrete number of class
// outputs) so reset the last (+1) output var_type element to CV_VAR_CATEGORICAL
var_type.at<uchar>(ATTRIBUTES_PER_SAMPLE, 0) = CV_VAR_CATEGORICAL;
参数设置:
float priors[] = {1,1,1,1,1,1,1,1,1,1}; // weights of each classification for classes
// (all equal as equal samples of each digit)
CvRTParams params = CvRTParams(25, // max depth
5, // min sample count
0, // regression accuracy: N/A here
false, // compute surrogate split, no missing data
15, // max number of categories (use sub-optimal algorithm for larger numbers)
priors, // the array of priors
false, // calculate variable importance
4, // number of variables randomly selected at node and used to find the best split(s).
100, // max number of trees in the forest
0.01f, // forrest accuracy
CV_TERMCRIT_ITER | CV_TERMCRIT_EPS // termination cirteria
);
训练使用var_type和params如下:
CvRTrees* rtree = new CvRTrees;
rtree->train(training_data, CV_ROW_SAMPLE, training_classifications,
Mat(), Mat(), var_type, Mat(), params);
我的问题是如何设置OpenCV随机森林,使其作为回归器运行。我已经搜索了很多,但还没有找到答案。我得到的最接近的解释是在这个答案中。然而,它仍然没有任何意义。
我正在寻找一个简单的答案,解释回归的var_type和params参数。