From 603d309f0d530af3f968d6d1f9113bd69888c2e1 Mon Sep 17 00:00:00 2001 From: Owen Green Date: Tue, 14 Jul 2020 17:59:23 +0100 Subject: [PATCH] Update dataest examples to interface changes --- .../dataset/demo-dataset-maker-utilities.scd | 3 ++- .../dataset/super-simple-1D-example.scd | 10 ++++---- .../dataset/super-simple-1D-example2.scd | 12 ++++----- .../super-simple-classifier-example.scd | 4 +-- .../super-simple-dictionary-json-example.scd | 11 ++++---- ...-normalization-standardization-example.scd | 25 ++++++++++--------- .../super-simple-regressor-example.scd | 4 +-- 7 files changed, 35 insertions(+), 34 deletions(-) diff --git a/release-packaging/Examples/dataset/demo-dataset-maker-utilities.scd b/release-packaging/Examples/dataset/demo-dataset-maker-utilities.scd index 4d0e313..46bce5c 100644 --- a/release-packaging/Examples/dataset/demo-dataset-maker-utilities.scd +++ b/release-packaging/Examples/dataset/demo-dataset-maker-utilities.scd @@ -150,7 +150,8 @@ FluidBufCompose.process(s,~loader.buffer,a,(b-a),numChans: 1, destination: ~targ //find its nearest neighbours ~friends = Array; -~tree.kNearest(~flatbuf[0],5,{|x| ~friends = x.postln;}) +~tree.numNeighbours = 5; +~tree.kNearest(~flatbuf[0],{|x| ~friends = x.postln;}) // play them in a row ( diff --git a/release-packaging/Examples/dataset/super-simple-1D-example.scd b/release-packaging/Examples/dataset/super-simple-1D-example.scd index 98bd2d9..d10bbfd 100644 --- a/release-packaging/Examples/dataset/super-simple-1D-example.scd +++ b/release-packaging/Examples/dataset/super-simple-1D-example.scd @@ -16,12 +16,12 @@ Routine{ ~tree = FluidKDTree.new(s) ~tree.fit(~ds,action:{"Done indexing".postln}) -k = 5; //play with this +~tree.numNeighbours = 5; //play with this ( Routine{ 10.do{|i| ~point.set(0,i); - ~tree.kNearest(~point, k, {|x| "Neighbours for a value of % are ".postf(i); x.postln}); + ~tree.kNearest(~point, {|x| "Neighbours for a value of % are ".postf(i); x.postln}); s.sync; } }.play @@ -29,9 +29,9 @@ Routine{ /*** KMEANS ***/ -~kmeans = FluidKMeans.new(s) -~nClusters = 2; //play with this -~kmeans.fit(~ds,~nClusters,100,action:{|x| "Done fitting with these number of items per cluster ".post;x.postln;}) +~kmeans = FluidKMeans.new(s,maxIter:100) +~kmeans.numClusters = 2; //play with this +~kmeans.fit(~ds,action:{|x| "Done fitting with these number of items per cluster ".post;x.postln;}) ( Routine{ diff --git a/release-packaging/Examples/dataset/super-simple-1D-example2.scd b/release-packaging/Examples/dataset/super-simple-1D-example2.scd index 0347497..5e6517f 100644 --- a/release-packaging/Examples/dataset/super-simple-1D-example2.scd +++ b/release-packaging/Examples/dataset/super-simple-1D-example2.scd @@ -18,13 +18,13 @@ Routine{ ~tree = FluidKDTree.new(s) ~tree.fit(~ds,action:{"Done indexing".postln}) -k = 5; //play with this +~tree.numNeighbours = 5; //play with this ( Routine{ 15.do{|i| ~point.set(0,i); - ~tree.kNearest(~point, k, {|x| "Neighbours for a value of % are ".postf(i); x.post;" with respective distances of ".post;}); - ~tree.kNearestDist(~point,k,{|x| x.postln}); + ~tree.kNearest(~point, {|x| "Neighbours for a value of % are ".postf(i); x.post;" with respective distances of ".post;}); + ~tree.kNearestDist(~point, {|x| x.postln}); s.sync; } }.play @@ -33,9 +33,9 @@ Routine{ /*** KMEANS ***/ -~kmeans = FluidKMeans.new(s) -~nClusters = 2; //play with this -~kmeans.fit(~ds,~nClusters,100,action:{|x| "Done fitting with these number of items per cluster ".post;x.postln;}) +~kmeans = FluidKMeans.new(s,maxIter:100) +~kmeans.numClusters = 2; //play with this +~kmeans.fit(~ds, action:{|x| "Done fitting with these number of items per cluster ".post;x.postln;}) ( Routine{ diff --git a/release-packaging/Examples/dataset/super-simple-classifier-example.scd b/release-packaging/Examples/dataset/super-simple-classifier-example.scd index da60257..eb0855a 100644 --- a/release-packaging/Examples/dataset/super-simple-classifier-example.scd +++ b/release-packaging/Examples/dataset/super-simple-classifier-example.scd @@ -3,7 +3,7 @@ ~simpleOutput = FluidLabelSet(s,\simpleOutput); b = Buffer.alloc(s,2); ~knn = FluidKNNClassifier(s); -k = 3 +~knn.numNeighbours = 3 ) ( @@ -22,7 +22,7 @@ v.mouseDownAction = {|view, x, y|myx=x;myy=y;w.refresh; // myx.postln;myy.postln; Routine{ b.setn(0,[myx,myy]); - ~knn.predictPoint(b, k, action: {|x|x.postln;}); + ~knn.predictPoint(b, action: {|x|x.postln;}); s.sync; }.play;}; diff --git a/release-packaging/Examples/dataset/super-simple-dictionary-json-example.scd b/release-packaging/Examples/dataset/super-simple-dictionary-json-example.scd index c1d33d4..fd11823 100644 --- a/release-packaging/Examples/dataset/super-simple-dictionary-json-example.scd +++ b/release-packaging/Examples/dataset/super-simple-dictionary-json-example.scd @@ -13,12 +13,11 @@ ~ds.print //fun with kdtree to see it actually works -~kdtree = FluidKDTree.new(s) +~kdtree = FluidKDTree.new(s,numNeighbours:5) ~kdtree.fit(~ds,{\done.postln;}) - ~target = Buffer.loadCollection(s,(4).dup(10)); -~kdtree.kNearest(~target,5,{|a|a.postln;}) -~kdtree.kNearestDist(~target,5,{|a|a.postln;}) +~kdtree.kNearest(~target, {|a|a.postln;}) +~kdtree.kNearestDist(~target, {|a|a.postln;}) ///////////////////////////////////////////// @@ -39,7 +38,7 @@ ~ls.print // testin with a classifier toy example -~classifier = FluidKNNClassifier.new(s); +~classifier = FluidKNNClassifier.new(s, numNeighbours:2); ~classifier.fit(~ds,~ls, {\done.postln;}) -~classifier.predictPoint(~target,2,action: {|x|x.postln;}) \ No newline at end of file +~classifier.predictPoint(~target, action: {|x|x.postln;}) diff --git a/release-packaging/Examples/dataset/super-simple-normalization-standardization-example.scd b/release-packaging/Examples/dataset/super-simple-normalization-standardization-example.scd index 35417f4..bbe3bfd 100644 --- a/release-packaging/Examples/dataset/super-simple-normalization-standardization-example.scd +++ b/release-packaging/Examples/dataset/super-simple-normalization-standardization-example.scd @@ -73,10 +73,10 @@ Routine{ ~dataset.getPoint("point-7",~query_buf); // find the 2 points with the shortest distances in the dataset -~tree = FluidKDTree.new(s); +~tree = FluidKDTree.new(s,numNeighbours:2); ~tree.fit(~dataset) -~tree.kNearest(~query_buf,2, {|x| ("Labels:" + x).postln}); -~tree.kNearestDist(~query_buf,2, {|x| ("Distances:" + x).postln}); +~tree.kNearest(~query_buf, {|x| ("Labels:" + x).postln}); +~tree.kNearestDist(~query_buf, {|x| ("Distances:" + x).postln}); // its nearest neighbourg is itself: it should be itself and the distance should be 0. The second point is depending on your input dataset. // normalise that point (~query_buf) to be at the right scale @@ -85,10 +85,10 @@ Routine{ ~normbuf.getn(0,~nb_of_dim,{arg vec;vec.postln;}); // make a tree of the normalized database and query with the normalize buffer -~normtree = FluidKDTree.new(s); +~normtree = FluidKDTree.new(s,numNeighbours:2); ~normtree.fit(~normed_dataset) -~normtree.kNearest(~normbuf,2, {|x| ("Labels:" + x).postln}); -~normtree.kNearestDist(~normbuf,2, {|x| ("Distances:" + x).postln}); +~normtree.kNearest(~normbuf, {|x| ("Labels:" + x).postln}); +~normtree.kNearestDist(~normbuf, {|x| ("Distances:" + x).postln}); // its nearest neighbourg is still itself as it should be, but the 2nd neighbourg will probably have changed. The distance is now different too // standardize that same point (~query_buf) to be at the right scale @@ -97,10 +97,10 @@ Routine{ ~stdbuf.getn(0,~nb_of_dim,{arg vec;vec.postln;}); // make a tree of the standardized database and query with the normalize buffer -~stdtree = FluidKDTree.new(s); +~stdtree = FluidKDTree.new(s, numNeighbours: 2); ~stdtree.fit(~standardized_dataset) -~stdtree.kNearest(~stdbuf,2, {|x| ("Labels:" + x).postln}); -~stdtree.kNearestDist(~stdbuf,2, {|x| ("Distances:" + x).postln}); +~stdtree.kNearest(~stdbuf, {|x| ("Labels:" + x).postln}); +~stdtree.kNearestDist(~stdbuf, {|x| ("Distances:" + x).postln}); // its nearest neighbourg is still itself as it should be, but the 2nd neighbourg will probably have changed yet again. The distance is also different too // where it starts to be interesting is when we query points that are not in our original dataset @@ -113,6 +113,7 @@ Routine{ ~standardize.transformPoint(~query_buf,~stdbuf); //query the single nearest neighbourg via 3 different data scaling. Depending on the random source at the begining, you will get small to large differences between the 3 answers! -~tree.kNearest(~query_buf,1, {|x| ("Original:" + x).post;~tree.kNearestDist(~query_buf,1, {|x| (" with a distance of " + x).postln});}); -~normtree.kNearest(~normbuf,1, {|x| ("Normalized:" + x).post;~normtree.kNearestDist(~normbuf,1, {|x| (" with a distance of " + x).postln});}); -~stdtree.kNearest(~stdbuf,1, {|x| ("Standardized:" + x).post; ~stdtree.kNearestDist(~stdbuf,1, {|x| (" with a distance of " + x).postln});}); \ No newline at end of file +[~tree,~normtree,~stdtree].do{|t| t.numNeighbours =1 }; +~tree.kNearest(~query_buf, {|x| ("Original:" + x).post;~tree.kNearestDist(~query_buf,1, {|x| (" with a distance of " + x).postln});}); +~normtree.kNearest(~normbuf, {|x| ("Normalized:" + x).post;~normtree.kNearestDist(~normbuf,1, {|x| (" with a distance of " + x).postln});}); +~stdtree.kNearest(~stdbuf, {|x| ("Standardized:" + x).post; ~stdtree.kNearestDist(~stdbuf,1, {|x| (" with a distance of " + x).postln});}); diff --git a/release-packaging/Examples/dataset/super-simple-regressor-example.scd b/release-packaging/Examples/dataset/super-simple-regressor-example.scd index c5a078f..fee6dba 100644 --- a/release-packaging/Examples/dataset/super-simple-regressor-example.scd +++ b/release-packaging/Examples/dataset/super-simple-regressor-example.scd @@ -44,11 +44,11 @@ Routine{ // query 512 points along the line (slow because of all that sync'ing) ( -k = 1; // change to see how many points the system uses to regress +~knn.numNeighbours = 1; // change to see how many points the system uses to regress Routine{ 512.do{|i| b.set(0,i*61); - ~knn.predictPoint(b,k,action:{|d|~mappingresult.set(i,d);}); + ~knn.predictPoint(b,action:{|d|~mappingresult.set(i,d);}); s.sync; i.postln; }