Accord.Net中的多项式支持向量回归

时间:2017-06-05 19:27:34

标签: c# accord.net

我正在尝试学习框架(Accord),但文档通常带有破碎的代码片段。
我想要类似于this的东西。

我尝试了不同的东西,但似乎没有任何效果。有没有人有一个有效的非线性支持向量回归的例子? 我也尝试了the official example,它似乎也无法发挥作用。

1 个答案:

答案 0 :(得分:2)

在项目采用新的统一学习API之后,文档仍在整理,现在所有机器学习模型都是通用的。其中大部分内容昨天刚刚更新,但有些部分可能仍需要注意。

回答原始问题,您可以在下面找到多项式SV回归的示例。假设我们有二维输入向量,我们想学习从这些向量到单个标量值的映射。

// Declare a very simple regression problem 
// with only 2 input variables (x and y):
double[][] inputs =
{
    new[] { 3.0, 1.0 },
    new[] { 7.0, 1.0 },
    new[] { 3.0, 1.0 },
    new[] { 3.0, 2.0 },
    new[] { 6.0, 1.0 },
};

double[] outputs =
{
    65.3,
    94.9,
    65.3,
    66.4,
    87.5,
};

为了举例,我们将机器Complexity参数设置为一个非常高的值,迫使学习算法找到硬边缘解决方案,否则这些解决方案将无法很好地推广。在对实际问题进行训练时,将属性UseKernelEstimation和UseComplexityHeuristic设置为true或执行网格搜索以查找其最佳参数:

// Create a LibSVM-based support vector regression algorithm
var teacher = new FanChenLinSupportVectorRegression<Polynomial>()
{
    Tolerance = 1e-5,
    // UseKernelEstimation = true, 
    // UseComplexityHeuristic = true
    Complexity = 10000,
    Kernel = new Polynomial(degree: 1) // you can change the degree
};

现在,在我们创建学习算法之后,我们可以用它来训练数据中的SVM模型:

// Use the algorithm to learn the machine
var svm = teacher.Learn(inputs, outputs);

最后我们可以得到机器对输入集的答案,我们可以检查机器预测的值与预期的基本事实相比有多好:

// Get machine's predictions for inputs
double[] prediction = svm.Score(inputs);

// Compute the error in the prediction (should be 0.0)
double error = new SquareLoss(outputs).Loss(prediction);