问题描述
你好,我想通过math.net进行多元回归,我有点困惑.
Hello I am trying to get multiple regression with math.net and I am a little confused.
var xdata = new DenseMatrix(
new double[,]{{1, 36, 66, 45, 32},
{1, 37, 68, 12, 2},
{1, 47, 64, 78, 34},
{1, 32, 53, 56, 32},
{1, 1, 101, 24, 90}});
var ydata = new double[] { 15, 20, 25, 55, 95 };
var X = DenseMatrix.CreateFromColumns(new[] { new DenseVector(xdata.Length, 1), new DenseVector(xdata) });
var y = new DenseVector(ydata);
var p = X.QR().Solve(y);
var a = p[0];
var b = p[1];
我想我听不懂Math.Net,这方面的任何帮助都会很棒.基本上我有多个x和一个y,需要从它们中获取系数数据.
I guess I don't understand Math.Net, any help with this would be great. Basically I have multiple x and a single y and need to get the coefficient data from them.
推荐答案
从准备矩阵的方式(即第一列始终为1)看来,我似乎实际上有4个自变量,并且您正在寻找只需将所有自变量的线性组合作为目标函数的简单回归:
From the way you've prepared your matrix (i.e. first column always 1), it seems to me you actually have 4 independent variables and you're looking for a simple regression with just a linear combination of all the independent variables as target function:
y : (x1, ..., x4) -> p0 + p1*x1 + ... + p4*x4
在这种情况下,只需删除行var X = ...
,然后将xdata
重命名为X
,则所有5个参数将按预期在p
向量中可用.
If this is the case, just remove the line var X = ...
and instead rename xdata
to X
, then all 5 parameters will be available in the p
vector as expected.
鉴于上述数据,您将获得大约:
Given the data above, you'll end up with approximately:
y : (x1, ..., x4) -> 123.2 - 8.9*x1 + 2.8*x2 + 3.7*x3 - 4.4*x4
这篇关于使用math.net进行多元回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!