算法散点图'最适合'线

编程入门 行业动态 更新时间:2024-10-25 04:16:43
本文介绍了算法散点图'最适合'线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在写使用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); } } }

更多推荐

算法散点图'最适合'线

本文发布于:2023-10-25 09:00:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1526509.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最适合   算法   散点图

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!