Prostym sposobem na to byłoby użycie drzewa decyzyjnego ID3 Accord.
Sztuką jest ustalenie, jakich wejść użyć - nie można po prostu ćwiczyć na X - drzewo nie nauczy się niczego na temat przyszłych wartości X - jednak można zbudować pewne funkcje pochodzące z X (lub poprzednie wartości Y), które będą przydatne.
Zwykle w przypadku takich problemów - każda prognoza powinna opierać się na cechach wyprowadzonych z poprzednich wartości Y (przewidywanej rzeczy), a nie X. Jednakże zakłada się, że można obserwować Y sekwencyjnie między każdą prognozą (nie można następnie przewidzieć dowolne arbitralne X), więc pozostanę z przedstawionym pytaniem.
Udało mi się zbudować drzewo decyzyjne Accord ID3, aby rozwiązać ten problem poniżej. Użyłem kilku różnych wartości x % n
jako funkcji - mając nadzieję, że drzewo może znaleźć odpowiedź na to pytanie. W rzeczywistości, jeśli dodałem (x-1) % 4
jako cechę, mógłbym to zrobić na jednym poziomie z tym właśnie atrybutem - ale myślę, że chodzi o to, żeby pozwolić drzewu znaleźć wzory.
A oto kod, który:
// this is the sequence y follows
int[] ysequence = new int[] { 1, 2, 3, 2 };
// this generates the correct Y for a given X
int CalcY(int x) => ysequence[(x - 1) % 4];
// this generates some inputs - just a few differnt mod of x
int[] CalcInputs(int x) => new int[] { x % 2, x % 3, x % 4, x % 5, x % 6 };
// for http://stackoverflow.com/questions/40573388/simple-accord-net-machine-learning-example
[TestMethod]
public void AccordID3TestStackOverFlowQuestion2()
{
// build the 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");
for (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 only because y doesn't use zero)
int classCount = outputs.Max()+1;
// create the tree
DecisionTree tree = new DecisionTree(attributes, classCount);
// Create a new instance of the ID3 algorithm
ID3Learning id3learning = new ID3Learning(tree);
// Learn the training instances! Populates the tree
id3learning.Learn(inputs, outputs);
Console.WriteLine();
// now try to predict some cases that werent in the training data
for (int x = numtrainingcases+1; x <= 2* numtrainingcases; x++)
{
int[] query = CalcInputs(x);
int answer = tree.Decide(query); // makes the prediction
Assert.AreEqual(CalcY(x), answer); // check the answer is what we expected - ie the tree got it right
Console.WriteLine("Prediction \t\t " + x+"\t "+answer);
}
}
To wyjście produkuje:
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
nadzieję, że pomoże.
EDYCJA: Po komentarzach, poniżej przykład jest modyfikowany, aby ćwiczyć poprzednie wartości celu (Y) - a nie cechy wyprowadzone z indeksu czasu (X). Oznacza to, że nie możesz rozpocząć treningu na początku serii - ponieważ potrzebujesz wstecznej historii poprzednich wartości Y. W tym przykładzie zacząłem od x = 9 tylko dlatego, że zachowuje tę samą sekwencję.
// this is the sequence y follows
int[] ysequence = new int[] { 1, 2, 3, 2 };
// this generates the correct Y for a given X
int CalcY(int x) => ysequence[(x - 1) % 4];
// this generates some inputs - just a 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 };
// for http://stackoverflow.com/questions/40573388/simple-accord-net-machine-learning-example
[TestMethod]
public void AccordID3TestTestStackOverFlowQuestion2()
{
// build the 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");
for (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 only because y doesn't use zero)
int classCount = outputs.Max()+1;
// create the tree
DecisionTree tree = new DecisionTree(attributes, classCount);
// Create a new instance of the ID3 algorithm
ID3Learning id3learning = new ID3Learning(tree);
// Learn the training instances! Populates the tree
id3learning.Learn(inputs, outputs);
Console.WriteLine();
// now try to predict some cases that werent in the training data
for (int x = starttrainingat+numtrainingcases; x <= starttrainingat + 2 * numtrainingcases; x++)
{
int[] query = CalcInputs(x);
int answer = tree.Decide(query); // makes the prediction
Assert.AreEqual(CalcY(x), answer); // check the answer is what we expected - ie the tree got it right
Console.WriteLine("Prediction \t\t " + x+"\t "+answer);
}
}
Można również rozważyć szkolenie na temat różnic pomiędzy poprzednimi wartościami Y - które pracują lepiej, gdy wartość bezwzględna Y nie jest tak ważne jak względnej zmiany.
To jest genialne, dużo pochyliłem się nad tym przykładem (jak produkować wejścia i wyjścia). Przykład zadziałał idealnie. Ale w "rzeczywistym przypadku", nie mogę użyć wartości X do obliczeń, ponieważ jest to szereg czasowy (np. X1 = 3:00 AM, x2 = 4: 00am, x3 = 5: 00am), więc tylko ja mieć serię czasową wszystkich wartości Y i chcieć ją znaleźć w tym miejscu, aby pomóc przewidzieć, jaka będzie następna wartość Y. Jeśli tak się stanie? – RHC
Oczywiście - bardziej naturalne jest użycie poprzednich wartości celu (Y) dla szeregów czasowych - przynajmniej wtedy, gdy rzeczywisty czas jest nieistotny, a związek między wartościami jest tam, gdzie leży wzór. – reddal
Będę edytować odpowiedź, aby dodać, jak przykład można zmodyfikować, aby ćwiczyć na poprzednich wartościach z Y. – reddal