算法散点图'最适合'线 [英] Algorithm for scatter plot 'best-fit' line

查看:345
本文介绍了算法散点图'最适合'线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写使用MSChart控件做套X和Y数据点的散点图在C#中的小应用程序。其中的一些可能相当大(数百个数据点)。

I'm writing a small application in C# using MSChart control to do Scatter Plots of sets of X and Y data points. Some of these can be rather large (hundreds of data points).

想问问,如果有一个'标准'algorith密谋跨越点最佳拟合线。我想到X数据点划分到套预定数量,例如10或20,并为每个组采取相应的Y值的平均值和中X值,依此类推,以创建订单。这是一个正确的做法?

Wanted to ask if there's a 'standard' algorith for plotting a best-fit line across the points. I'm thinking to divide the X data points to a predefined number of sets, say 10 or 20, and for each set take the average of the corresponding Y values and the middle X value, and so on to create the line. Is this a correct approach?

我搜索现有线程,但他们似乎都对实现使用类似于Matlab现有的应用程序一样。

I've searched existing threads but they all seem to be about achieving the same using existing applications like Matlab.

谢谢,

推荐答案

使用线性最小二乘法

public class XYPoint
{
    public int X;
    public double Y;
}

class Program
{
    public static List<XYPoint> GenerateLinearBestFit(List<XYPoint> points, out double a, out double b)
    {
        int numPoints = points.Count;
        double meanX = points.Average(point => point.X);
        double meanY = points.Average(point => point.Y);

        double sumXSquared = points.Sum(point => point.X * point.X);
        double sumXY = points.Sum(point => point.X * point.Y);

        a = (sumXY / numPoints - meanX * meanY) / (sumXSquared / numPoints - meanX * meanX);
        b = (a * meanX - meanY);

        double a1 = a;
        double b1 = b;

        return points.Select(point => new XYPoint() { X = point.X, Y = a1 * point.X - b1 }).ToList();
    }

    static void Main(string[] args)
    {
        List<XYPoint> points = new List<XYPoint>()
                                   {
                                       new XYPoint() {X = 1, Y = 12},
                                       new XYPoint() {X = 2, Y = 16},
                                       new XYPoint() {X = 3, Y = 34},
                                       new XYPoint() {X = 4, Y = 45},
                                       new XYPoint() {X = 5, Y = 47}
                                   };

        double a, b;

        List<XYPoint> bestFit = GenerateLinearBestFit(points, out a, out b);

        Console.WriteLine("y = {0:#.####}x {1:+#.####;-#.####}", a, -b);

        for(int index = 0; index < points.Count; index++)
        {
            Console.WriteLine("X = {0}, Y = {1}, Fit = {2:#.###}", points[index].X, points[index].Y, bestFit[index].Y);
        }
    }
}

这篇关于算法散点图'最适合'线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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