From c3f240148d5a2549ba815469d9c8a9345dc9215b Mon Sep 17 00:00:00 2001 From: Gerard Date: Thu, 14 May 2020 14:21:24 +0100 Subject: [PATCH] add FluidKNNClassifier / FluidKNNRegressor --- .../Classes/FluidKNNClassifier.sc | 19 ++++++++++++++++++ .../Classes/FluidKNNRegressor.sc | 20 +++++++++++++++++++ .../super-simple-classifier-example.scd | 6 +++--- .../super-simple-regressor-example.scd | 6 +++--- 4 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 release-packaging/Classes/FluidKNNClassifier.sc create mode 100644 release-packaging/Classes/FluidKNNRegressor.sc diff --git a/release-packaging/Classes/FluidKNNClassifier.sc b/release-packaging/Classes/FluidKNNClassifier.sc new file mode 100644 index 0000000..ef6a7b6 --- /dev/null +++ b/release-packaging/Classes/FluidKNNClassifier.sc @@ -0,0 +1,19 @@ +FluidKNNClassifier : FluidManipulationClient { + fit{|dataset, labelset, action| + this.pr_sendMsg(\fit,[dataset.asString, labelset.asString], action); + } + + predict{ |dataset, labelset, k, action| + this.pr_sendMsg(\predict, + [dataset.asString, labelset.asString, k], + action, [string(FluidMessageResponse,_,_)] + ); + } + + predictPoint { |buffer, k, action| + this.pr_sendMsg(\predictPoint, + [buffer.asUGenInput, k], action, + [number(FluidMessageResponse,_,_)] + ); + } +} \ No newline at end of file diff --git a/release-packaging/Classes/FluidKNNRegressor.sc b/release-packaging/Classes/FluidKNNRegressor.sc new file mode 100644 index 0000000..d3716b0 --- /dev/null +++ b/release-packaging/Classes/FluidKNNRegressor.sc @@ -0,0 +1,20 @@ +FluidKNNRegressor : FluidManipulationClient { + fit{|sourceDataset, targetDataset, action| + this.pr_sendMsg(\fit, + [sourceDataset.asString, targetDataset.asString], + action + ); + } + + predict{ |sourceDataset, targetDataset, k, action| + this.pr_sendMsg(\predict, + [sourceDataset.asString, targetDataset.asString, k], + action, + [string(FluidMessageResponse,_,_)]); + } + + predictPoint { |buffer, k, action| + this.pr_sendMsg(\predictPoint, [buffer.asUGenInput, k], action, + [number(FluidMessageResponse,_,_)]); + } +} diff --git a/release-packaging/Examples/dataset/super-simple-classifier-example.scd b/release-packaging/Examples/dataset/super-simple-classifier-example.scd index c365365..8c58f29 100644 --- a/release-packaging/Examples/dataset/super-simple-classifier-example.scd +++ b/release-packaging/Examples/dataset/super-simple-classifier-example.scd @@ -2,7 +2,7 @@ ~simpleInput = FluidDataSet(s,\simpleInput,2); ~simpleOutput = FluidLabelSet(s,\simpleOutput,2); b = Buffer.alloc(s,2); -~knn = FluidKNN(s); +~knn = FluidKNNClassifier(s); k = 3 ) @@ -23,7 +23,7 @@ v.mouseDownAction = {|view, x, y|myx=x;myy=y;w.refresh; Routine{ b.setn(0,[myx,myy]); s.sync; - ~knn.classifyPoint(b, ~simpleOutput, k, {|x|x.postln;}); + ~knn.predictPoint(b, k, {|x|x.postln;}); }.play;}; //custom redraw function @@ -58,6 +58,6 @@ Routine{ ) // fit the dataset -~knn.fit(~simpleInput,action:{"fitting done".postln}) +~knn.fit(~simpleInput,~simpleOutput, action:{"fitting done".postln}) // now click on the grid and read the estimated class according to the nearest K neighbours. diff --git a/release-packaging/Examples/dataset/super-simple-regressor-example.scd b/release-packaging/Examples/dataset/super-simple-regressor-example.scd index e8c4e95..4c5c7bd 100644 --- a/release-packaging/Examples/dataset/super-simple-regressor-example.scd +++ b/release-packaging/Examples/dataset/super-simple-regressor-example.scd @@ -36,8 +36,8 @@ Routine{ ~mappingresult = Buffer.alloc(s,512); //make the process then fit the data -~knn = FluidKNN(s) -~knn.fit(~simpleInput,action:{"fitting done".postln}) +~knn = FluidKNNRegressor(s) +~knn.fit(~simpleInput, ~simpleOutput, action:{"fitting done".postln}) // query 512 points along the line (slow because of all that sync'ing) ( @@ -46,7 +46,7 @@ Routine{ 512.do{|i| b.set(0,i*61); s.sync; - ~knn.regressPoint(b,~simpleOutput,k,action:{|d|~mappingresult.set(i,d);}); + ~knn.predictPoint(b,k,action:{|d|~mappingresult.set(i,d);}); s.sync; i.postln; }