-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLinearSVM.cpp
More file actions
46 lines (36 loc) · 873 Bytes
/
LinearSVM.cpp
File metadata and controls
46 lines (36 loc) · 873 Bytes
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
#include "LinearSVM.h"
LinearSVM::LinearSVM() : linearmodel(NULL)
{
}
LinearSVM::~LinearSVM()
{
if (linearmodel!=NULL)
free_and_destroy_model(&linearmodel);
}
int LinearSVM::load_svm_model(const char* modelpath)
{
linearmodel = load_model(modelpath);
if (NULL==linearmodel)
return -1;
return 0;
}
int LinearSVM::predict_s(const std::vector<float>& features)
{
if (linearmodel == NULL)//模型加载失败
return -1;
feature_node *pnode = NULL;
pnode = new feature_node[features.size() + 1];
if (pnode = NULL)
return -1;//feature_node 内存分配失败
for (size_t i = 0; i < features.size(); i++)
{
pnode[i].index = (int)(i + 1);//linearsvm 索引从1开始
pnode[i].value = features[i];
}
pnode[features.size()].index = -1;//linearsvm 索引结束标志
int label = -1;
label = (int)predict(linearmodel, pnode);
delete[] pnode;
pnode = NULL;
return 0;
}