You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.0 KiB
Markdown

s.reboot
~urn = { |n=31416, min=0,max=31415| (min..max).scramble.keep(n) };
// creates 200 indices, then values of the output of a fundion with a predictable shape of a sinewave
n = 200
~idx = ~urn.value(n)
~data = n.collect{|i|sin(~idx[i]/5000)}
// creates the dataset with these associated indices and values
(
~simpleInput = FluidDataSet(s);
~simpleOutput = FluidDataSet(s);
b = Buffer.alloc(s,1);
c = Buffer.alloc(s,1);
~mappingviz = Buffer.alloc(s,512);
)
(
Routine{
n.do{|i|
b.set(0,~idx[i]);
c.set(0,~data[i]);
~simpleInput.addPoint(i.asString,b,{("Added Input" + i).postln});
~simpleOutput.addPoint(i.asString,c,{("Added Output" + i).postln});
~mappingviz.set((~idx[i]/61.4).asInteger,~data[i]);
s.sync;
}
}.play
)
~simpleInput.print
~simpleOutput.print
//look at the seeing material
~mappingviz.plot(minval:-1,maxval:1)
//create a buffer to query
~mappingresult = Buffer.alloc(s,512);
//make the process then fit the data
~knn = FluidKNNRegressor(s,3,1)
~knn.fit(~simpleInput, ~simpleOutput, action:{"fitting done".postln})
// query 512 points along the line (slow because of all that sync'ing)
(
~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,action:{|d|~mappingresult.set(i,d);});
s.sync;
i.postln;
}
}.play
)
// look at the interpolated values
~mappingresult.plot
// change the number of neighbours to regress on
~knn.numNeighbours_(5)
~knn.fit(~simpleInput, ~simpleOutput, action:{"fitting done".postln})
// instead of doing the mapping per point, let's do a dataset of 512 points
~target = FluidDataSet(s)
~target.load(Dictionary.newFrom([\cols, 1, \data, Dictionary.newFrom(512.collect{|i|[i.asString, [i.asFloat * 61]]}.flatten)]))
~regressed = FluidDataSet(s)
~knn.predict(~target, ~regressed, action:{"prediction done".postln})
//dump the regressed values
~outputArray = Array.newClear(512);
~regressed.dump{|x| x["data"].keysValuesDo{|key,val|~outputArray[key.asInteger] = val[0]}}
~outputArray.plot