|
|
|
|
@ -52,10 +52,15 @@ Run when done, passes predicted label as argument
|
|
|
|
|
EXAMPLES::
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
~labels = FluidLabelSet(s,\knnclassify_help_labels);
|
|
|
|
|
~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]];
|
|
|
|
|
~examplelabels = [\red,\orange,\green,\blue];
|
|
|
|
|
~source.clear;
|
|
|
|
|
~labels.clear;
|
|
|
|
|
~tmpbuf = Buffer.alloc(s,2);
|
|
|
|
|
fork{
|
|
|
|
|
s.sync;
|
|
|
|
|
~examplepoints.do{|x,i|
|
|
|
|
|
(""++(i+1)++"/4").postln;
|
|
|
|
|
~tmpbuf.setn(0,x);
|
|
|
|
|
~source.addPoint(i,~tmpbuf);
|
|
|
|
|
~labels.addLabel(i,~examplelabels[i]);
|
|
|
|
|
s.sync
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
d = Dictionary.new;
|
|
|
|
|
d.add(\cols -> 2);
|
|
|
|
|
d.add(\data -> Dictionary.newFrom(~examplepoints.collect{|x, i|[i.asString, x]}.flatten));
|
|
|
|
|
~source.load(d);
|
|
|
|
|
~examplelabels.collect{|x,i| ~labels.addLabel(i, x);};
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
//Make some random, but clustered test points
|
|
|
|
|
(
|
|
|
|
|
~testpoints = (4.collect{64.collect{(1.sum3rand) + [1,-1].choose}.clump(2)}).flatten(1) * 0.5;
|
|
|
|
|
~test.clear;
|
|
|
|
|
fork {
|
|
|
|
|
s.sync;
|
|
|
|
|
~testpoints.do{|x,i|
|
|
|
|
|
~tmpbuf.setn(0,x);
|
|
|
|
|
~test.addPoint(i,~tmpbuf);
|
|
|
|
|
s.sync;
|
|
|
|
|
if(i==(~testpoints.size - 1)){"Generated test data".postln;}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
~testpoints = (4.collect{
|
|
|
|
|
64.collect{(1.sum3rand) + [1,-1].choose}.clump(2)
|
|
|
|
|
}).flatten(1) * 0.5;
|
|
|
|
|
d = Dictionary.with(
|
|
|
|
|
*[\cols -> 2,\data -> Dictionary.newFrom(
|
|
|
|
|
~testpoints.collect{|x, i| [i, x]}.flatten)]);
|
|
|
|
|
~test.load(d);
|
|
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
//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.predict(~test, ~mapping, 1);
|
|
|
|
|
s.sync;
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
//Return labels of clustered points
|
|
|
|
|
@ -156,5 +146,4 @@ w.refresh;
|
|
|
|
|
w.front;
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|