简单的Accord.net机器学习示例 [英] Simple accord.net machine learning example

查看:57
本文介绍了简单的Accord.net机器学习示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是机器学习的新手,也是accord.net的新手(我使用C#代码).

I’m new to machine learning and new to accord.net (I code C#).

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

I want to create a simple project where I look at a simple time series of data that oscillate, then I want accord.net to learn it and predict what the next value will be.

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

This is what the data (time series) should look like:

X - Y

1 - 1

2 - 2

3 - 3

4 - 2

5 - 1

6 - 2

7 - 3

8 - 2

9 - 1

然后,我希望它可以预测以下内容:

Then I want it to predict the following:

X - Y

10 - 2

11 - 3

12 - 2

13 - 1

14 - 2

15 - 3

你们能帮我解决一些例子吗?

Can you guys help me out with some examples on how to solve it?

推荐答案

一种简单的方法是使用Accord ID3决策树.

A simple way to do this would be to use an Accord ID3 decision tree.

诀窍是弄清楚要使用的输入-您不能仅在X上进行训练-树无法从中了解有关X的未来值的任何信息-但是您可以构建从X(或以前的版本)衍生的某些功能Y的值).

The trick is to work out what inputs to use - you can't just train on X - the tree won't learn anything about future values of X from that - however you can build some features derived from X (or previous values of Y) that will be useful.

通常对于这样的问题-您将基于从Y的先前值(被预测的事物)而不是X得出的特征进行每次预测.但是,假设您可以在每次预测之间依次观察Y(因此您不能预测任何人为的X),所以我会坚持提出的问题.

Normally for problems like this - you would make each prediction based on features derived from previous values of Y (the thing being predicted) rather than X. However that assumes you can observe Y sequentially between each prediction (you can't then predict for any arbitary X) so I'll stick with the question as presented.

我在下面构建了Accord ID3决策树以解决此问题.我使用了x % n的几个不同值作为功能-希望树可以解决这个问题.实际上,如果我添加(x-1) % 4作为一项功能,则可以仅使用该属性在单个级别上完成此功能-但我想重点在于让树找到模式.

I had a go at building an Accord ID3 decision tree to solve this problem below. I used a few different values of x % n as the features - hoping the tree could work out the answer from this. In fact if I'd added (x-1) % 4 as a feature it could do it in a single level with just that attribute - but I guess the point is more to let the tree find the patterns.

这是该代码:

    // 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开始,只是因为它保持相同的顺序.

EDIT : Following comments, below the example is modified to train on previous values of the target (Y) - rather than features derived from the time index (X). This means you can't start training at the start of your series - as you need a back history of previous values of Y. In this example I started at x=9 just because that keeps the same sequence.

        // 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的绝对值不如相对变化那么重要,则效果更好.

You could also consider training on the differences between previous values of Y - which would work better where the absolute value of Y is not as important as the relative change.

这篇关于简单的Accord.net机器学习示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆