“Linear Regression with Pseudo-Inverse Training Using C#” in Visual Studio Magazine

I wrote an article titled “Linear Regression with Pseudo-Inverse Training Using C#” in the December 2025 edition of Microsoft Visual Studio Magazine. See https://visualstudiomagazine.com/articles/2025/12/15/linear-regression-with-pseudo-inverse-training-using-csharp.aspx.

The goal of a machine learning regression problem is to predict a single numeric value. For example, you might want to predict the bank account balance of an employee based on his age, height, and years of work experience.

There are roughly a dozen main regression techniques, including nearest neighbors regression, and neural network regression. Linear regression is the most fundamental technique.

The form of a linear regression prediction equation is y’ = (w0 * x0) + (w1 * x1) + . . + (wn * xn) + b where y’ is the predicted value, the xi are predictor values, the wi are constants called model weights, and b is a constant called the bias. For example, y’ = predicted balance = (-0.54 * age) + (0.38 * height) + (0.11 * experience) + 0.72. Training the model is the process of finding the values of the weights and bias so that predicted y values are close to known correct target y values in a set of training data.

There are three main techniques to train a linear regression model: stochastic gradient descent (SGD), pseudo-inverse, and closed form training. My article explains how to implement the pseudo-inverse training technique.

The output of my demo program is:

Begin C# linear regression using pseudo-inverse training

Loading synthetic train (200) and test (40) data
Done

First three train X:
 -0.6046  0.7260  0.9668 -0.6723  0.1947
  0.9341  0.0945  0.9454  0.4296  0.3955
 -0.9820 -0.2269 -0.9117  0.9133 -0.1277

First three train y:
  0.7180
  0.2507
  0.5698

Creating and training Linear Regression model 
 using QR p-inverse
Done

Coefficients/weights:
-0.2500  -0.0220  0.0272  -0.1434  0.0511
Bias/constant: 0.4938

Evaluating model

Accuracy train (within 0.10) = 0.7500
Accuracy test (within 0.10) = 0.7750

MSE train = 0.0013
MSE test = 0.0011

Predicting for x =
  -0.6046   0.7260   0.9668  -0.6723   0.1947

Predicted y = 0.7616

End demo

There are several algorithms to compute a pseudo-inverse. The demo uses QR decomposition via the Householder algorithm.

For large datasets, training with SGD is often best, but it requires values for a learning rate parameter and a maximum epochs parameter, and those must be determined by trial and error. For medium size datasets, training with pseudo-inverse works well, but it is the most complicated technique. For small datasets, closed form training is simpler than pseudo-inverse, but it can fail more easily.



I’m a big fan of 1950s science fiction movies. Two of my favorites featured spacecraft with tubes.

Left: In “Forbidden Planet” (1956), the crew of the starship C-57D go to planet Altair IV to determine the fate of an earlier expedition. The crew enter “DC” stations as the ship decelerates out of hyperdrive. The tubes/stations are bordered by a green energy field rather than glass or plastic.

Right: In “This Island Earth” (1955), scientists from Earth are recruited by aliens from Metaluna to help in a war with the Zagons. The Metaluna spacecraft has some sort of conditioning tubes to deal with the differences between the atmospheres of Earth and Metaluna.


This entry was posted in Machine Learning. Bookmark the permalink.

Leave a Reply