From edf4357ebcf7e71313b1c067afe60f5ea8aee066 Mon Sep 17 00:00:00 2001 From: sbosse Date: Tue, 27 Aug 2024 00:16:12 +0200 Subject: [PATCH] Tue 27 Aug 00:14:56 CEST 2024 --- test/test-knn2.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 test/test-knn2.js diff --git a/test/test-knn2.js b/test/test-knn2.js new file mode 100644 index 0000000..f4b133c --- /dev/null +++ b/test/test-knn2.js @@ -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))