Skip to content
This repository was archived by the owner on Oct 18, 2023. It is now read-only.

Commit 69180f0

Browse files
committed
Added getDecisionFunction to svm
1 parent eef86fb commit 69180f0

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

cc/modules/machinelearning/SVM.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ NAN_MODULE_INIT(SVM::Init) {
2626
Nan::SetPrototypeMethod(ctor, "predict", Predict);
2727
Nan::SetPrototypeMethod(ctor, "getSupportVectors", GetSupportVectors);
2828
Nan::SetPrototypeMethod(ctor, "getUncompressedSupportVectors", GetUncompressedSupportVectors);
29+
Nan::SetPrototypeMethod(ctor, "getDecisionFunction", GetDecisionFunction);
2930
Nan::SetPrototypeMethod(ctor, "calcError", CalcError);
3031
Nan::SetPrototypeMethod(ctor, "save", Save);
3132
Nan::SetPrototypeMethod(ctor, "load", Load);
@@ -139,6 +140,25 @@ NAN_METHOD(SVM::GetUncompressedSupportVectors) {
139140
#endif
140141
}
141142

143+
NAN_METHOD(SVM::GetDecisionFunction) {
144+
FF_METHOD_CONTEXT("SVM::GetDecisionFunction");
145+
146+
if (!info[0]->IsNumber()) {
147+
FF_THROW("expected arg 0 to be a Int");
148+
}
149+
150+
FF_OBJ alpha = FF_NEW_INSTANCE(Mat::constructor);
151+
FF_OBJ svidx = FF_NEW_INSTANCE(Mat::constructor);
152+
FF_ARG_INT(0, int i);
153+
float rho = FF_UNWRAP(info.This(), SVM)->svm->getDecisionFunction(i, FF_UNWRAP_MAT_AND_GET(alpha), FF_UNWRAP_MAT_AND_GET(svidx));
154+
155+
FF_OBJ ret = FF_NEW_OBJ();
156+
Nan::Set(ret, FF_NEW_STRING("rho"), Nan::New((double)rho));
157+
Nan::Set(ret, FF_NEW_STRING("alpha"), alpha);
158+
Nan::Set(ret, FF_NEW_STRING("svidx"), svidx);
159+
FF_RETURN(ret);
160+
}
161+
142162
NAN_METHOD(SVM::CalcError) {
143163
FF_METHOD_CONTEXT("SVM::CalcError");
144164
FF_ARG_INSTANCE(0, cv::Ptr<cv::ml::TrainData> trainData, TrainData::constructor, FF_UNWRAP_TRAINDATA_AND_GET);

cc/modules/machinelearning/SVM.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class SVM: public Nan::ObjectWrap {
4444
static NAN_METHOD(Predict);
4545
static NAN_METHOD(GetSupportVectors);
4646
static NAN_METHOD(GetUncompressedSupportVectors);
47+
static NAN_METHOD(GetDecisionFunction);
4748
static NAN_METHOD(CalcError);
4849
static NAN_METHOD(Save);
4950
static NAN_METHOD(Load);

lib/typings/SVM.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export class SVM {
1717
constructor(params: { c?: number, coef0?: number, degree?: number, gamma?: number, nu?: number, p?: number, kernelType?: number, classWeights?: Mat });
1818
calcError(trainData: TrainData, test: boolean): { error: number, responses: Mat };
1919
getSupportVectors(): Mat;
20+
getDecisionFunction(): { rho: number, alpha: Mat, svidx: Mat };
2021
load(file: string): void;
2122
predict(sample: number[], flags?: number): number;
2223
predict(samples: Mat, flags?: number): number[];

test/tests/modules/machinelearning/SVMTests.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,15 @@ module.exports = () => {
190190
});
191191
});
192192

193+
describe('getDecisionFunction', () => {
194+
it('should return decision function', () => {
195+
const ret = svm.getDecisionFunction(0);
196+
expect(ret).to.have.property('rho').to.be.a('number');
197+
expect(ret).to.have.property('alpha').to.be.instanceOf(cv.Mat);
198+
expect(ret).to.have.property('svidx').to.be.instanceOf(cv.Mat);
199+
});
200+
});
201+
193202
describe('calcError', () => {
194203
it.skip('calcError', () => {});
195204
});

0 commit comments

Comments
 (0)