これを行う簡単な方法は、アコードID3決定木を使用することです。
トリックは、使用する入力を決めることです - あなたはXだけでトレーニングすることはできません - ツリーはXの将来の値について何も学ばないでしょうが、Xから派生したいくつかの特徴を構築できますYの以前の値)が有用である。
通常、このような問題については、XではなくYの以前の値から導出されたフィーチャに基づいて各予測を行います。ただし、各予測間でYを連続して観測できると仮定しています任意のXを予測してください)、私は提示された質問に固執します。
私はこの問題を解決するためにアコードID3決定木を構築することにしました。機能として、x % n
のいくつかの異なる値を使用しました。ツリーがこれから答えを出すことができることを願っています。実際、もし私が(x-1) % 4
を機能として追加したのであれば、その属性を使って単一のレベルでそれを行うことができます。しかし、その点は、ツリーがパターンを見つけさせてくれる点だと思います。
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
希望に役立ちます:
// 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);
}
}
これは、それが生成する出力は次のとおりです。
そして、ここではそのためのコードです。
EDIT:以下のコメントは、時間インデックス(X)から派生した特徴ではなく、ターゲット(Y)の以前の値を訓練するように変更されています。これは、以前のYの値の履歴が必要なため、シリーズ開始時にトレーニングを開始できないことを意味します。この例では、x = 9で開始しました。 Yの絶対値は相対的な変化と同様に重要でない場合よりよく機能するであろう -
// 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);
}
}
また、Yの以前の値との差異に関する研修を検討することができます。
これは素晴らしく、私はこの例(入力と出力を作り出す方法)から多くのものを得ました。 この例は完全に機能しました。 "実際のケース"では、時間セリフ(x1 = 3:00 AM、x2 = 4:00am、x3 = 5:00am)のため、X値を計算に使用できません。すべてのY値の時間セリフを持ち、次のY値がどのようなものになるかを予測するのに役立つために、ここでpattenで見つけたいと思っています。 – RHC
確かに - 実際の時間が無関係で、値の関係がパターンが存在する場所であるときは、時系列のターゲット(Y)の以前の値を使うのが自然です。 – reddal
答えを編集して、以前のYの値を訓練するために例を修正する方法を追加します。 – reddal