http://blog.csdn.net/NNNNNNNNNNNNY/article/details/50973583用他的来对我自己的数据集分类 traindata是328X1000的矩阵 trainlabel是328X2的标签
#include
#include
#include
#include
#include
using namespace std;
using namespace cv;
int main()
{
CvANN_MLP bp;
CvANN_MLP_TrainParams params;
params.train_method = CvANN_MLP_TrainParams::BACKPROP; //(Back Propagation,BP)反向传播算法
params.bp_dw_scale = 0.1;
params.bp_moment_scale = 0.1;
CvMLData trainlabels,traindatas,testlabels,testdatas;
trainlabels.read_csv("train_label.csv");
cv::Mat finaltrainlabelprimer= cv::Mat(trainlabels.get_values(), true);
float* finaltrainlabelprimernew = (float*)finaltrainlabelprimer.data;
Mat finaltrainlabel(328, 2, CV_32FC1, &finaltrainlabelprimernew);
traindatas.read_csv("train_data.csv");
cv::Mat finaltraindataprimer = cv::Mat(traindatas.get_values(), true);
float* finaltraindataprimernew = (float*)finaltraindataprimer.data;
Mat finaltraindata(328,1000, CV_32FC1, &finaltraindataprimernew);
Mat layerSizes = (Mat_
bp.create(layerSizes, CvANN_MLP::SIGMOID_SYM);
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//训练 报错说我的前两个参数要是floating-matrix
bp.train(finaltraindata, finaltrainlabel, Mat(), Mat(), params);
testdatas.read_csv("test_data.csv");
cv::Mat finaltestdata = cv::Mat(testdatas.get_values(), true);
Mat responseMat;
bp.predict(finaltestdata, responseMat);
return 0;
}
把Mat layerSizes = (Mat_
我懂了 我重新用新数据
取整了的数据 每个样本4维特征 然后label是 第二类的第二类的label是 然后程序没什么变 还是:
#include
#include
#include
#include
#include
using namespace std;
using namespace cv;
int main()
{
CvANN_MLP bp;
CvANN_MLP_TrainParams params;
params.train_method = CvANN_MLP_TrainParams::BACKPROP; //(Back Propagation,BP)反向传播算法
params.bp_dw_scale = 0.1;
params.bp_moment_scale = 0.1;
CvMLData trainlabels, traindatas, testlabels, testdatas;
trainlabels.read_csv("exampletrainlabel.csv");
cv::Mat finaltrainlabelprimer = cv::Mat(trainlabels.get_values(), true);
//float* finaltrainlabelprimernew = (float*)finaltrainlabelprimer.data;
//Mat finaltrainlabel(550, 2, CV_32FC1, &finaltrainlabelprimernew);
traindatas.read_csv("exampletraindata.csv");
cv::Mat finaltraindataprimer = cv::Mat(traindatas.get_values(), true);
//float* finaltraindataprimernew = (float*)finaltraindataprimer.data;
//Mat finaltraindata(550, 4, CV_32FC1, &finaltraindataprimernew);
Mat layerSizes = (Mat_
bp.create(layerSizes, CvANN_MLP::SIGMOID_SYM);
//bp.train(finaltraindata, finaltrainlabel, Mat(), Mat(), params);
bp.train(finaltraindataprimer, finaltrainlabelprimer, Mat(), Mat(), params);
testdatas.read_csv("exampletestdata.csv");
cv::Mat finaltestdata = cv::Mat(testdatas.get_values(), true);
Mat responseMat;
bp.predict(finaltestdata, responseMat);
for (int i = 0; i < responseMat.rows; ++i)
{
float* p = responseMat.ptr
if (p[0] > p[1])
cout <<0.9<< " " << 0.1<< endl;
else
cout << 0.1 << " " << 0.9<< endl;
}
return 0;
}
这样就不报错了 结果虽然都分到第二类去了 但没关系 特征是乱写的 不报错了就行 至少证明了数据不能像我之前的那样不能是科学计数法表示的 也不能是复数 还有发现csv读数据后直接转成Mat也行 可能因为本来在matlab里就是double 所以不用转成float了吧 另外我不知道不知道这里 将ANN的结果 重新归类部分错了没
#include
#include
#include
#include
#include
using namespace std;
using namespace cv;
int main()
{
CvANN_MLP bp;
CvANN_MLP_TrainParams params;
params.train_method = CvANN_MLP_TrainParams::BACKPROP; //(Back Propagation,BP)反向传播算法
params.bp_dw_scale = 0.1;
params.bp_moment_scale = 0.1;
CvMLData trainlabels, traindatas, testlabels, testdatas;
trainlabels.read_csv("trainlabel.csv");
cv::Mat finaltrainlabelprimer = cv::Mat(trainlabels.get_values(), true);
traindatas.read_csv("traindata.csv");
cv::Mat finaltraindataprimer = cv::Mat(traindatas.get_values(), true);
Mat layerSizes = (Mat_
bp.create(layerSizes, CvANN_MLP::SIGMOID_SYM);
bp.train(finaltraindataprimer, finaltrainlabelprimer, Mat(), Mat(), params);
testdatas.read_csv("testdata.csv");
cv::Mat finaltestdata = cv::Mat(testdatas.get_values(), true);
testlabels.read_csv("testlabel.csv");
cv::Mat finaltestlabel = cv::Mat(testlabels.get_values(), true);
Mat responseMat;
bp.predict(finaltestdata, responseMat);
int correct = 0;
float accurency;
for (int i = 0; i < responseMat.rows; ++i)
{
float* p = responseMat.ptr
float* being = finaltestlabel.ptr
if (p[0] > p[1])
cout <<0.9<< " " <<0.1<< " " << being[0] << " " << being[1] << endl;
else
cout <<0.1<< " " << 0.9 << " " << being[0] << " " << being[1]<< endl;
if (((p[0] > p[1]) && (being[0] > being[1])) || ((p[0] < p[1]) && (being[0] < being[1])))
++correct;
}
cout << endl;
accurency = (float)correct / finaltestlabel.rows;
cout << accurency << endl;
return 0;
}
刚刚重新用新特征试了下: