KNNClassifier example update

nix
Gerard 6 years ago
parent 54f36f7736
commit fb89c0d9a0

@ -52,10 +52,15 @@ Run when done, passes predicted label as argument
EXAMPLES:: EXAMPLES::
code:: code::
//A dataset of example points, and a label set of corresponding labels
//+
//A dataset of test data and a labelset for predicted labels // Make:
// - A KNN Classifier
// - A dataset of example points, and a label set of corresponding labels
// - A dataset of test data and a labelset for predicted labels
( (
~classifier = FluidKNNClassifier(s);
~source= FluidDataSet(s,\knnclassify_help_examples); ~source= FluidDataSet(s,\knnclassify_help_examples);
~labels = FluidLabelSet(s,\knnclassify_help_labels); ~labels = FluidLabelSet(s,\knnclassify_help_labels);
~test = FluidDataSet(s,\knnclassify_help_test); ~test = FluidDataSet(s,\knnclassify_help_test);
@ -66,45 +71,30 @@ code::
( (
~examplepoints = [[0.5,0.5],[-0.5,0.5],[0.5,-0.5],[-0.5,-0.5]]; ~examplepoints = [[0.5,0.5],[-0.5,0.5],[0.5,-0.5],[-0.5,-0.5]];
~examplelabels = [\red,\orange,\green,\blue]; ~examplelabels = [\red,\orange,\green,\blue];
~source.clear; d = Dictionary.new;
~labels.clear; d.add(\cols -> 2);
~tmpbuf = Buffer.alloc(s,2); d.add(\data -> Dictionary.newFrom(~examplepoints.collect{|x, i|[i.asString, x]}.flatten));
fork{ ~source.load(d);
s.sync; ~examplelabels.collect{|x,i| ~labels.addLabel(i, x);};
~examplepoints.do{|x,i|
(""++(i+1)++"/4").postln;
~tmpbuf.setn(0,x);
~source.addPoint(i,~tmpbuf);
~labels.addLabel(i,~examplelabels[i]);
s.sync
}
}
) )
//Make some random, but clustered test points //Make some random, but clustered test points
( (
~testpoints = (4.collect{64.collect{(1.sum3rand) + [1,-1].choose}.clump(2)}).flatten(1) * 0.5; ~testpoints = (4.collect{
~test.clear; 64.collect{(1.sum3rand) + [1,-1].choose}.clump(2)
fork { }).flatten(1) * 0.5;
s.sync; d = Dictionary.with(
~testpoints.do{|x,i| *[\cols -> 2,\data -> Dictionary.newFrom(
~tmpbuf.setn(0,x); ~testpoints.collect{|x, i| [i, x]}.flatten)]);
~test.addPoint(i,~tmpbuf); ~test.load(d);
s.sync;
if(i==(~testpoints.size - 1)){"Generated test data".postln;}
}
}
) )
//Make a new KNN classifier model, fit it to the example dataset and labels, and then run preduction on the test data into our mapping label set
//Fit the classifier to the example dataset and labels, and then run prediction on the test data into our mapping label set
( (
fork{
~classifier = FluidKNNClassifier(s);
s.sync;
~classifier.fit(~source,~labels); ~classifier.fit(~source,~labels);
~classifier.predict(~test, ~mapping, 1); ~classifier.predict(~test, ~mapping, 1);
s.sync;
}
) )
//Return labels of clustered points //Return labels of clustered points
@ -156,5 +146,4 @@ w.refresh;
w.front; w.front;
) )
:: ::

Loading…
Cancel
Save