我正在尝试使用随机森林的最简单示例。训练数据为2点{0,0},标签为0,{1,1}为标签1.预测样本为{2,2}。 OpenCV返回0而不是1.这是C ++中的OpenCV代码(main.cpp
):
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/ml/ml.hpp>
using namespace std;
using namespace cv;
int main(int argc, char const *argv[]) {
cout << " hi \n";
float trainingData[2][2] = { {0.0, 0.0}, {1.0, 1.0}};
Mat training_data(2, 2, CV_32FC1, trainingData);
float trainingClass[2] = {0.0,1.0};
Mat training_class(2, 1, CV_32FC1, trainingClass);
CvRTrees rtree;
rtree.train(training_data, CV_ROW_SAMPLE, training_class);
float sampleData[2] = {2.0, 2.0};
Mat sample_data(2, 1, CV_32FC1, sampleData);
cout << rtree.predict(sample_data) << " <-- predict\n";
return 0;
}
cmake文件:
cmake_minimum_required(VERSION 2.8)
project( main )
find_package( OpenCV REQUIRED )
add_executable( main main.cpp )
target_link_libraries( main ${OpenCV_LIBS} )
运行:
> cmake .;make;./main
hi
0 <-- predict
比较一下,这是一个python的sklearn代码(rfc.py
):
from sklearn.ensemble import RandomForestClassifier
X = [[0, 0], [1, 1]]
Y = [0, 1]
clf = RandomForestClassifier(n_estimators=10)
clf = clf.fit(X, Y)
print clf.predict([[2., 2.]])
运行:
> python rfc.py
[1]
答案 0 :(得分:1)
训练点数太少。如果我将它改为3,一切正常。
将min_sample_count
更改为2也有效。