jam/test/test-knn1.js

52 lines
920 B
JavaScript

var dataset = [
{a: 1, b:2, y: 2},
{a: 3, b:1, y: 4},
{a: 5, b:2, y: 6},
{a: 7, b:1, y: 8},
{a: 9, b:2, y: 0},
];
var model = ml.learn({
algorithm:ml.ML.KNN,
data:dataset,
features : ["a","b"],
target : 'y'
});
var nearest = ml.classify(model,[{ a: 3, b:1}, { a:6, b:1}]);
print(nearest);
dataset = [
{a: 1, b:2, y: 'yellow'},
{a: 3, b:1, y: 'green'},
{a: 5, b:2, y: 'yellow'},
{a: 7, b:1, y: 'red'},
{a: 9, b:2, y: 'blue'},
];
model = ml.learn({
algorithm:ml.ML.KNN,
data:dataset,
features : ["a","b"],
target : 'y'
});
nearest = ml.classify(model,[{ a: 7, b:1}, { a:4, b:1}]);
print(nearest);
var x = [[0, 0, 0], [0, 1, 1], [1, 1, 0], [2, 2, 2], [1, 2, 2], [2, 1, 2]];
var y = [0, 0.2, 0.4, 1, 1.2, 1.4];
model = ml.learn({
algorithm:ml.ML.KNN,
x:x,
y:y,
});
var test_data =[[0, 1, 0],
[2, 2, 3]];
print(ml.classify(model,test_data))
print(model)