首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >简单的accord.net机器学习示例

简单的accord.net机器学习示例
EN

Stack Overflow用户
提问于 2016-11-13 11:40:32
回答 1查看 10K关注 0票数 8

我是机器学习新手,也是accord.net新手(我编码C#)。

我想要创建一个简单的项目,在这个项目中,我查看一个简单的时间序列数据振荡,然后我希望accord.net学习它并预测下一个值将是什么。

数据(时间序列)应该是这样的:

X - Y

代码语言:javascript
复制
1 - 1

2 - 2

3 - 3

4 - 2

5 - 1

6 - 2

7 - 3

8 - 2

9 - 1

然后我要它预测如下:

X - Y

代码语言:javascript
复制
10 - 2

11 - 3

12 - 2

13 - 1

14 - 2

15 - 3

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-13 18:58:32

要做到这一点,一个简单的方法是使用协议ID3决策树。

诀窍是找出要使用的输入--你不能仅仅在X上训练--树不会从它那里学到任何关于X的未来值的东西,但是您可以构建一些从X(或Y的先前值)派生出来的特性,这些特性将是有用的。

通常,对于类似这样的问题,您可以根据Y(被预测的事物)的先前值而不是X的特性来进行预测,但是这假设您可以在每个预测之间依次观察Y(然后不能对任意的X进行预测),所以我将继续讨论提出的问题。

我试过构建一个协议ID3决策树来解决下面的问题。我使用了x % n的几个不同的值作为特性--希望树能从中找到答案。事实上,如果我添加了(x-1) % 4作为一个特性,它可以在一个级别上使用这个属性--但我想重点是让树找到模式。

这是密码:

代码语言:javascript
复制
    // 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);
        }
    }

这是它产生的输出:

代码语言:javascript
复制
                 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开始只是因为它保持了相同的序列。

代码语言:javascript
复制
        // 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的绝对值不如相对变化重要的情况下,Y值会更好地工作。

票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40573388

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档