KNNRgressor example update

nix
Gerard 6 years ago
parent 537c9899aa
commit 4b25eeba18

@ -62,50 +62,56 @@ code::
~test = FluidDataSet(s,\knn_regress_test); ~test = FluidDataSet(s,\knn_regress_test);
~output = FluidDataSet(s,\knn_regress_out); ~output = FluidDataSet(s,\knn_regress_out);
~tmpbuf = Buffer.alloc(s,1); ~tmpbuf = Buffer.alloc(s,1);
~regressor = FluidKNNRegressor(s);
) )
//Make source, target and test data //Make source, target and test data
( (
~sourcedata = 128.collect{|i|i/128}; ~sourcedata = 128.collect{|i|i/128};
~targetdata = 128.collect{|i| sin(2*pi*i/128) }; ~targetdata = 128.collect{|i| sin(2*pi*i/128) };
fork{ ~testdata = 128.collect{|i|(i/128)**2};
128.do{ |i|
((i + 1).asString ++ "/128").postln; ~source.load(
~tmpbuf.setn(0,i/128); Dictionary.with(
~source.addPoint(i,~tmpbuf); *[\cols -> 1,\data -> Dictionary.newFrom(
s.sync; ~sourcedata.collect{|x, i| [i.asString, [x]]}.flatten)])
~tmpbuf.setn(0,sin(2*pi*i/128)); );
~target.addPoint(i,~tmpbuf);
s.sync; ~target.load(
~tmpbuf.setn(0,(i/128)**2); d = Dictionary.with(
~test.addPoint(i,~tmpbuf); *[\cols -> 1,\data -> Dictionary.newFrom(
s.sync; ~targetdata.collect{|x, i| [i.asString, [x]]}.flatten)]);
if(i==127){"Source, target and test generated".postln}; );
}
} ~test.load(
Dictionary.with(
*[\cols -> 1,\data -> Dictionary.newFrom(
~testdata.collect{|x, i| [i.asString, [x]]}.flatten)])
);
~source.print;
~target.print;
~test.print;
) )
// Now make a regressor and fit it to the source and target, and predict against test // Now make a regressor and fit it to the source and target, and predict against test
//grab the output data whilst we're at it, so we can inspect //grab the output data whilst we're at it, so we can inspect
( (
~outputdata = Array(128); ~outputdata = Array(128);
fork{ ~regressor.fit(~source, ~target);
~regressor = FluidKNNRegressor(s); ~regressor.predict(~test, ~output, 1, action:{
s.sync; ~output.dump{|x| 128.do{|i|
~regressor.fit(~source,~target); ~outputdata.add(x["data"][i.asString][0])
~regressor.predict(~test,~output,1); }};
s.sync; });
128.do{|i|
~output.getPoint(i,~tmpbuf,{
~tmpbuf.loadToFloatArray(action:{|x|
~outputdata.addAll(x)
})
});
s.sync;
if(i==127){"Model fitted, output generated".postln};
}
}
) )
//We should see a single cycle of a chirp //We should see a single cycle of a chirp
~outputdata.plot; ~outputdata.plot;
:: ::

Loading…
Cancel
Save