使用excel互操作在C#中生成预测值

问题描述 投票:0回答:1

我想使用excel互操作预测函数生成预测值。我需要使用excel预测线性函数,但我想将预测值存储到数据库。

我不知道如何开始请帮助我。

c# excel-formula excel-interop
1个回答
5
投票

如果你只需要一个线性预测函数,我认为Excel Interop可能有点过分。您可以从C#项目添加对Office COM库的引用并调用WorksheetFunction.Forecast函数,或者您可以直接添加一个方法来在C#代码中执行相同的操作。我认为线性预测逻辑基本上如下:

static double Forecast(double[] xValues, double[] yValues, double forecastPoint)
{
    var xAverage = xValues.Average();
    var yAverage = yValues.Average();

    var bounds = yValues
        .Select((y, i) => new { Value = y, Index = i })
        .Aggregate(new { Top = 0.0, Bottom = 0.0 }, (acc, cur) =>
            new
            {
                Top = acc.Top + (xValues[cur.Index] - xAverage) * (yValues[cur.Index] - yAverage),
                Bottom = acc.Bottom + Math.Pow(xValues[cur.Index] - xAverage, 2.0)
            });

    var level = bounds.Top / bounds.Bottom;

    return (yAverage - level * xAverage) + level * forecastPoint;
}
© www.soinside.com 2019 - 2024. All rights reserved.