-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
basic implementation of probability estimates
- Loading branch information
Showing
4 changed files
with
91 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const Kernel = new require('ml-kernel'); | ||
const SVM = require('../asm'); | ||
const range = require('lodash.range'); | ||
|
||
'use strict'; | ||
|
||
const gamma = 0.2; | ||
const cost = 1; | ||
|
||
function exec(SVM, precomputed) { | ||
const data = require('ml-dataset-iris'); | ||
var trainData; | ||
|
||
const features = data.getNumbers(); | ||
let labels = data.getClasses(); | ||
const classes = data.getDistinctClasses(); | ||
const c = {}; | ||
classes.forEach((v, idx) => c[v] = idx); | ||
labels = labels.map(l => c[l]); | ||
|
||
|
||
if (precomputed) { | ||
const kernel = new Kernel('gaussian', {sigma: 1 / Math.sqrt(gamma)}); | ||
trainData = kernel.compute(features).addColumn(0, range(1, labels.length + 1)); | ||
} else { | ||
trainData = features; | ||
} | ||
|
||
const svm = new SVM({ | ||
quiet: true, | ||
cost: cost, | ||
kernel: precomputed ? SVM.KERNEL_TYPES.PRECOMPUTED : SVM.KERNEL_TYPES.RBF, | ||
gamma, | ||
probabilityEstimates: true | ||
}); | ||
svm.train(trainData, labels); | ||
var pred = svm.predictProbability(trainData); | ||
console.log(JSON.stringify(pred, null, 2)); | ||
} | ||
|
||
exec(SVM, true); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters