c# - Simple accord.net machine learning example -
i’m new machine learning , new accord.net
(i code c#).
i want create simple project @ simple time series of data oscillate, want accord.net learn , predict next value be.
this data (time series) should like:
x - y
1 - 1 2 - 2 3 - 3 4 - 2 5 - 1 6 - 2 7 - 3 8 - 2 9 - 1
then want predict following:
x - y
10 - 2 11 - 3 12 - 2 13 - 1 14 - 2 15 - 3
can guys me out examples on how solve it?
a simple way use accord id3 decision tree.
the trick work out inputs use - can't train on x - tree won't learn future values of x - can build features derived x (or previous values of y) useful.
normally problems - make each prediction based on features derived previous values of y (the thing being predicted) rather x. assumes can observe y sequentially between each prediction (you can't predict arbitary x) i'll stick question presented.
i had go @ building accord id3 decision tree solve problem below. used few different values of x % n
features - hoping tree work out answer this. in fact if i'd added (x-1) % 4
feature in single level attribute - guess point more let tree find patterns.
and here code :
// sequence y follows int[] ysequence = new int[] { 1, 2, 3, 2 }; // generates correct y given x int calcy(int x) => ysequence[(x - 1) % 4]; // generates inputs - few differnt mod of x int[] calcinputs(int x) => new int[] { x % 2, x % 3, x % 4, x % 5, x % 6 }; // http://stackoverflow.com/questions/40573388/simple-accord-net-machine-learning-example [testmethod] public void accordid3teststackoverflowquestion2() { // build training data set int numtrainingcases = 12; int[][] inputs = new int[numtrainingcases][]; int[] outputs = new int[numtrainingcases]; console.writeline("\t\t\t\t x \t y"); (int x = 1; x <= numtrainingcases; x++) { int y = calcy(x); inputs[x-1] = calcinputs(x); outputs[x-1] = y; console.writeline("trainingdata \t " +x+"\t "+y); } // define how many values each input can have decisionvariable[] attributes = { new decisionvariable("mod2",2), new decisionvariable("mod3",3), new decisionvariable("mod4",4), new decisionvariable("mod5",5), new decisionvariable("mod6",6) }; // define how many outputs (+1 because y doesn't use zero) int classcount = outputs.max()+1; // create tree decisiontree tree = new decisiontree(attributes, classcount); // create new instance of id3 algorithm id3learning id3learning = new id3learning(tree); // learn training instances! populates tree id3learning.learn(inputs, outputs); console.writeline(); // try predict cases werent in training data (int x = numtrainingcases+1; x <= 2* numtrainingcases; x++) { int[] query = calcinputs(x); int answer = tree.decide(query); // makes prediction assert.areequal(calcy(x), answer); // check answer expected - ie tree got right console.writeline("prediction \t\t " + x+"\t "+answer); } }
this output produces :
x y trainingdata 1 1 trainingdata 2 2 trainingdata 3 3 trainingdata 4 2 trainingdata 5 1 trainingdata 6 2 trainingdata 7 3 trainingdata 8 2 trainingdata 9 1 trainingdata 10 2 trainingdata 11 3 trainingdata 12 2 prediction 13 1 prediction 14 2 prediction 15 3 prediction 16 2 prediction 17 1 prediction 18 2 prediction 19 3 prediction 20 2 prediction 21 1 prediction 22 2 prediction 23 3 prediction 24 2
hope helps.
edit : following comments, below example modified train on previous values of target (y) - rather features derived time index (x). means can't start training @ start of series - need history of previous values of y. in example started @ x=9 because keeps same sequence.
// sequence y follows int[] ysequence = new int[] { 1, 2, 3, 2 }; // generates correct y given x int calcy(int x) => ysequence[(x - 1) % 4]; // generates inputs - few differnt mod of x int[] calcinputs(int x) => new int[] { calcy(x-1), calcy(x-2), calcy(x-3), calcy(x-4), calcy(x - 5) }; //int[] calcinputs(int x) => new int[] { x % 2, x % 3, x % 4, x % 5, x % 6 }; // http://stackoverflow.com/questions/40573388/simple-accord-net-machine-learning-example [testmethod] public void accordid3testteststackoverflowquestion2() { // build training data set int numtrainingcases = 12; int starttrainingat = 9; int[][] inputs = new int[numtrainingcases][]; int[] outputs = new int[numtrainingcases]; console.writeline("\t\t\t\t x \t y"); (int x = starttrainingat; x < numtrainingcases + starttrainingat; x++) { int y = calcy(x); inputs[x- starttrainingat] = calcinputs(x); outputs[x- starttrainingat] = y; console.writeline("trainingdata \t " +x+"\t "+y); } // define how many values each input can have decisionvariable[] attributes = { new decisionvariable("y-1",4), new decisionvariable("y-2",4), new decisionvariable("y-3",4), new decisionvariable("y-4",4), new decisionvariable("y-5",4) }; // define how many outputs (+1 because y doesn't use zero) int classcount = outputs.max()+1; // create tree decisiontree tree = new decisiontree(attributes, classcount); // create new instance of id3 algorithm id3learning id3learning = new id3learning(tree); // learn training instances! populates tree id3learning.learn(inputs, outputs); console.writeline(); // try predict cases werent in training data (int x = starttrainingat+numtrainingcases; x <= starttrainingat + 2 * numtrainingcases; x++) { int[] query = calcinputs(x); int answer = tree.decide(query); // makes prediction assert.areequal(calcy(x), answer); // check answer expected - ie tree got right console.writeline("prediction \t\t " + x+"\t "+answer); } }
you consider training on differences between previous values of y - work better absolute value of y not important relative change.
Comments
Post a Comment