Ridge regression in C# with help of Math .Net Numerics

Hello everybody,

today I want to share a piece of code that took me almost few months of learning in order to implement it. It is Ridge regression. Below goes code that gives you weights for Ridge regression:

public double[] RidgeRegression(double[][] xTrain, double[] yTrain, double lambda = 0.1)
    var M = Matrix<double>.Build;
    var x = M.DenseOfRowArrays(xTrain);
    var ones = Vector<double>.Build.Dense(x.RowCount, 1);
    x = x.InsertColumn(0, ones);
    var y = Vector<double>.Build.DenseOfArray(yTrain);
    var xt = x.Transpose();
    var lambdaIdentity = lambda * M.DenseIdentity(x.ColumnCount);
    var sumDot = xt.Multiply(x) + lambdaIdentity;
    var theInverse = sumDot.Inverse();
    var inverseXt = theInverse * xt;
    var w = inverseXt * y;
    return w.ToArray();

So, here it is.

You can use it for your purposes in case if you'll have some kind of multicolinear, lineary dependent rows/columns in order to build efficient linear models.

No Comments

Add a Comment