diff --git a/test/test-knn1.js b/test/test-knn1.js new file mode 100644 index 0000000..f0d8e86 --- /dev/null +++ b/test/test-knn1.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.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)