编程语言
首页 > 编程语言> > c#-在Accord.net Framework中使用Liblinear进行多重分类

c#-在Accord.net Framework中使用Liblinear进行多重分类

作者:互联网

我需要使用Liblinear实现多个分类器. Accord.net机器学习框架提供了所有Liblinear属性,但Crammer和Singer的用于多类分类的公式除外. This is the process.

解决方法:

学习多类机器的通常方法是使用MulticlassSupportVectorLearning class.该类可以教一对多的机器,然后可以使用投票或淘汰策略对它们进行查询.

因此,这是一个关于如何对多个类别进行线性训练的示例:

// Let's say we have the following data to be classified
// into three possible classes. Those are the samples:
// 
double[][] inputs =
{
    //               input         output
    new double[] { 0, 1, 1, 0 }, //  0 
    new double[] { 0, 1, 0, 0 }, //  0
    new double[] { 0, 0, 1, 0 }, //  0
    new double[] { 0, 1, 1, 0 }, //  0
    new double[] { 0, 1, 0, 0 }, //  0
    new double[] { 1, 0, 0, 0 }, //  1
    new double[] { 1, 0, 0, 0 }, //  1
    new double[] { 1, 0, 0, 1 }, //  1
    new double[] { 0, 0, 0, 1 }, //  1
    new double[] { 0, 0, 0, 1 }, //  1
    new double[] { 1, 1, 1, 1 }, //  2
    new double[] { 1, 0, 1, 1 }, //  2
    new double[] { 1, 1, 0, 1 }, //  2
    new double[] { 0, 1, 1, 1 }, //  2
    new double[] { 1, 1, 1, 1 }, //  2
};

int[] outputs = // those are the class labels
{
    0, 0, 0, 0, 0,
    1, 1, 1, 1, 1,
    2, 2, 2, 2, 2,
};

// Create a one-vs-one multi-class SVM learning algorithm 
var teacher = new MulticlassSupportVectorLearning<Linear>()
{
    // using LIBLINEAR's L2-loss SVC dual for each SVM
    Learner = (p) => new LinearDualCoordinateDescent()
    {
        Loss = Loss.L2
    }
};

// Learn a machine
var machine = teacher.Learn(inputs, outputs);

// Obtain class predictions for each sample
int[] predicted = machine.Decide(inputs);

// Compute classification accuracy
double acc = new GeneralConfusionMatrix(expected: outputs, predicted: predicted).Accuracy;

您还可以尝试使用“一对多休息”策略来解决多类别决策问题.在这种情况下,您可以使用MultilabelSupportVectorLearning教学算法来代替上面显示的多类教学算法.

标签:accord-net,machine-learning,classification,liblinear,c
来源: https://codeday.me/bug/20191120/2044364.html