-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnorm_bayes_classifer
70 lines (64 loc) · 2.07 KB
/
norm_bayes_classifer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// bayes_classifier
//
// Created by 郭成成 on 2017/1/4.
// Copyright © 2017年 郭成成. All rights reserved.
//
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
using namespace std;
using namespace cv;
double inputArr[10][13]=
{
1,0.708333,1,1,-0.320755,-0.105023,-1,1,-0.419847,-1,-0.225806,0,1,
-1,0.583333,-1,0.333333,-0.603774,1,-1,1,0.358779,-1,-0.483871,0,-1,
1,0.166667,1,-0.333333,-0.433962,-0.383562,-1,-1,0.0687023,-1,-0.903226,-1,-1,
-1,0.458333,1,1,-0.358491,-0.374429,-1,-1,-0.480916,1,-0.935484,0,-0.333333,
-1,0.875,-1,-0.333333,-0.509434,-0.347032,-1,1,-0.236641,1,-0.935484,-1,-0.333333,
-1,0.5,1,1,-0.509434,-0.767123,-1,-1,0.0534351,-1,-0.870968,-1,-1,
1,0.125,1,0.333333,-0.320755,-0.406393,1,1,0.0839695,1,-0.806452,0,-0.333333,
1,0.25,1,1,-0.698113,-0.484018,-1,1,0.0839695,1,-0.612903,0,-0.333333,
1,0.291667,1,1,-0.132075,-0.237443,-1,1,0.51145,-1,-0.612903,0,0.333333,
1,0.416667,-1,1,0.0566038,0.283105,-1,1,0.267176,-1,0.290323,0,1
};
double testArr[]=
{
0.25,1,1,-0.226415,-0.506849,-1,-1,0.374046,-1,-0.83871,0,-1
};
int main(int argc, const char * argv[]) {
// insert code here...
Mat traindata(10,12,CV_32FC1);
for(int i=0;i<10;i++)
{
for(int j=0;j<12;j++)
{
traindata.at<float>(i,j)=inputArr[i][j+1];
}
}
Mat train_response(10,1,CV_32FC1);
for(int i=0;i<10;i++)
{
train_response.at<float>(i,0)=inputArr[i][0];
}
CvNormalBayesClassifier nbc;
bool trainflag=nbc.train(traindata, train_response);
if(trainflag)
{
cout<<"train over..."<<endl;
nbc.save("/Users/c_c/Desktop/normbayes.txt");
}
else{
cout<<"train error..."<<endl;
exit(-1);
}
CvNormalBayesClassifier testnbc;
testnbc.load("/Users/c_c/Desktop/normbayes.txt");
Mat testsample(1,12,CV_32FC1);
for(int i=0;i<12;i++)
{
testsample.at<float>(0,i)=testArr[i];
}
float flag=testnbc.predict(testsample);
cout<<"测试数据类别为:"<<flag<<endl;
return 0;
}