|
|
|
|
@ -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,59 +71,44 @@ 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;
|
|
|
|
|
}
|
|
|
|
|
~classifier.fit(~source,~labels);
|
|
|
|
|
~classifier.predict(~test, ~mapping, 1);
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
//Return labels of clustered points
|
|
|
|
|
(
|
|
|
|
|
~assignments = Array.new(~testpoints.size);
|
|
|
|
|
fork{
|
|
|
|
|
~testpoints.do{|x,i|
|
|
|
|
|
~mapping.getLabel(i,action:{|l|
|
|
|
|
|
~assignments.add(l);
|
|
|
|
|
});
|
|
|
|
|
s.sync;
|
|
|
|
|
if(i==(~testpoints.size - 1)){"Got assignments".postln;}
|
|
|
|
|
};
|
|
|
|
|
~assignments.postln;
|
|
|
|
|
~testpoints.do{|x,i|
|
|
|
|
|
~mapping.getLabel(i, action:{|l|
|
|
|
|
|
~assignments.add(l);
|
|
|
|
|
});
|
|
|
|
|
s.sync;
|
|
|
|
|
if(i==(~testpoints.size - 1)){"Got assignments".postln;}
|
|
|
|
|
};
|
|
|
|
|
~assignments.postln;
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@ -137,24 +127,23 @@ d = ((~testpoints + 1) * 0.5).flatten(1).unlace;
|
|
|
|
|
w = Window("scatter", Rect(128, 64, 200, 200));
|
|
|
|
|
~colours = [Color.blue,Color.red,Color.green,Color.magenta];
|
|
|
|
|
w.drawFunc = {
|
|
|
|
|
Pen.use {
|
|
|
|
|
e[0].size.do{|i|
|
|
|
|
|
var r = Rect(e[0][i],e[1][i],10,10);
|
|
|
|
|
Pen.fillColor = c[~examplelabels[i]];
|
|
|
|
|
Pen.fillOval(r);
|
|
|
|
|
};
|
|
|
|
|
d[0].size.do{|i|
|
|
|
|
|
var x = (d[0][i]*200);
|
|
|
|
|
var y = (d[1][i]*200);
|
|
|
|
|
var r = Rect(x,y,5,5);
|
|
|
|
|
Pen.fillColor = c[~assignments[i].asSymbol].alpha_(0.3);
|
|
|
|
|
Pen.fillOval(r);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Pen.use {
|
|
|
|
|
e[0].size.do{|i|
|
|
|
|
|
var r = Rect(e[0][i],e[1][i],10,10);
|
|
|
|
|
Pen.fillColor = c[~examplelabels[i]];
|
|
|
|
|
Pen.fillOval(r);
|
|
|
|
|
};
|
|
|
|
|
d[0].size.do{|i|
|
|
|
|
|
var x = (d[0][i]*200);
|
|
|
|
|
var y = (d[1][i]*200);
|
|
|
|
|
var r = Rect(x,y,5,5);
|
|
|
|
|
Pen.fillColor = c[~assignments[i].asSymbol].alpha_(0.3);
|
|
|
|
|
Pen.fillOval(r);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
w.refresh;
|
|
|
|
|
w.front;
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|