我对机器学习不熟悉,对accord.net(我的代码是c)。
我想创建一个简单的项目,在这里我可以看到一个简单的数据振荡时间序列,然后我想让accord.net学习它并预测下一个值是什么。
数据(时间序列)应该是这样的:
X-Y轴

1 - 1

2 - 2

3 - 3

4 - 2

5 - 1

6 - 2

7 - 3

8 - 2

9 - 1

我希望它能预测以下情况:
X-Y轴
10 - 2

11 - 3

12 - 2

13 - 1

14 - 2

15 - 3

你们能帮我举几个例子来解决这个问题吗?

最佳答案

一个简单的方法是使用accord id3决策树。
诀窍是找出要使用的输入-你不能只是在x上训练-树不会从中了解x的未来值-但是你可以构建一些从x(或y的以前值)派生的有用特性。
通常对于像这样的问题-你会根据从y(被预测的事物)而不是x的先前值中得到的特征来进行每个预测。但是这假设你可以在每个预测之间按顺序观察y(然后你不能预测任意的x),所以我将坚持提出的问题。
为了解决下面的问题,我尝试构建一个accord id3决策树。我使用了几个不同的值x % n作为特性-希望树能从中找出答案。事实上,如果我将(x-1) % 4作为一个特性添加进来,它就可以在一个级别上使用这个属性来完成这项工作——但我想重点是让树找到模式。
这里是代码:

    // 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);
        }
    }

这是它产生的输出:
                 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

希望能有所帮助。
编辑:下面的注释修改了示例,以训练目标(y)的先前值,而不是从时间索引(x)派生的特征。这意味着你不能在系列开始时就开始训练,因为你需要一个y以前值的历史记录。在这个例子中,我从x=9开始只是因为它保持了相同的序列。
        // 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之前的值之间的差异,如果y的绝对值没有相对变化那么重要的话,这会更好。

08-25 00:12