Tue 27 Aug 00:14:56 CEST 2024

This commit is contained in:
sbosse 2024-08-27 00:16:12 +02:00
parent 04ad14c0f7
commit edf4357ebc

51
test/test-knn2.js Normal file
View File

@ -0,0 +1,51 @@
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.KNN2,
data:dataset,
features : ["a","b"],
target : 'y'
});
var nearest = ml.classify(model,[{ a: 3, b:1}, { a:6, b:1}]);
print(nearest);
// Categorical datat is not supported (due to interpolation feature)!
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.KNN2,
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.KNN2,
x:x,
y:y,
});
print(toJSON(model).length+' Bytes')
var test_data =[[0, 1, 0],
[2, 2, 3]];
print(ml.classify(model,test_data))