MATLAB:线性回归
我正在尝试找出最有效的方法来查找数据集的线性回归方程 (y = mx + c),给定 2 × n 数组。
基本上我想知道当 X 为 50 时 Y 的值是多少。
我当前的方法还有很多不足之处:
inputData 是我的 2 × n 数组,X 在第一列,Y 在第二列。
x = 50
for i = 1 : size(inputData,1) % for every line in the inputData array
if (inputData(i,1) < x + 5) | (inputData(i,1) > x - 5) % if we're within 5 of the specified X value
arrayOfCloseYValues(i) = inputData(i, 2); % add the other position to the array
end
end
y = mean(arrayOfCloseYValues) % take the mean to find Y
正如您所看到的,我的上述方法只是尝试查找给定 X 值 5 以内的 Y 值并获取平均值。这是一种糟糕的方法,而且需要很长时间才能处理。
我真正需要的是一种强大的方法来计算 X 和 Y 的线性回归,以便我可以通过方程 y = mx + c...
PS 找到该值。在我上面的方法中,我实际上预先分配了内存并删除了末尾的尾随零,但为了简单起见,我删除了这部分。
I'm trying to work out the most efficient method to find the linear regression equation (y = mx + c) for a dataset, given a 2 by n array.
Basically I want to know what the value of Y is when X is, for example, 50.
My current method leaves a lot to be desired:
inputData is my 2 by n array, with X in the first column and Y in the second.
x = 50
for i = 1 : size(inputData,1) % for every line in the inputData array
if (inputData(i,1) < x + 5) | (inputData(i,1) > x - 5) % if we're within 5 of the specified X value
arrayOfCloseYValues(i) = inputData(i, 2); % add the other position to the array
end
end
y = mean(arrayOfCloseYValues) % take the mean to find Y
As you can see, my above method simply tries to find values of Y that are within 5 of the given X value and gets the mean. This is a terrible method, plus it takes absolutely ages to process.
What I really need is a robust method for calculating the linear regression for X and Y, so that I can find the value through the equation y = mx + c...
PS. In my above method I do actually pre-allocate memory and remove trailing zeros at the end, but I have removed this part for simplicity.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Polyfit 很好,但我认为你的问题更简单一些。你有一个 2 xn 的数据数组。假设第 1 列是 y,第 2 列是 x,那么:
应该为您提供斜率和偏移的最小二乘回归。
这是测试它的另一种方法:
应该让你:
Polyfit is fine, but I think you're problem is a bit simpler. You have a 2 x n array of data. Let's say column 1 is y and column 2 is x, then:
Should give you a least squares regression for the slope and offset.
Here's another way to test it:
Should get you: