“Linear Regression Using JavaScript” in Visual Studio Magazine

I wrote an article titled “Linear Regression Using JavaScript” in the July 2025 edition of Microsoft Visual Studio Magazine. See https://visualstudiomagazine.com/articles/2025/07/07/linear-regression-using-javascript.aspx.

The goal of a machine learning regression problem is to predict a single numeric value. For example, you might want to predict an employee’s salary based on age, height, years of experience, and so on. There are approximately a dozen common regression techniques. The most basic technique is called linear regression, or sometimes multiple linear regression, where the “multiple” indicates two or more predictor variables.

The form of a basic linear regression prediction model is y’ = (w0 * x0) + (w1 * x1) + . . . + (wn * xn) + b, where y’ is the predicted value, the xi are predictor values, the wi are weights (also called coefficients), and b is the bias (also called the intercept).

Compared to other regression techniques, such as kernel ridge regression and Gaussian process regression which are designed to handle complex data, linear regression often has slightly worse prediction accuracy, but has better model interpretability.

My article presents a demo of linear regression, implemented from scratch, using the JavaScript language.

The output of the demo is:

Begin basic linear regression with SGD training using
  node.js JavaScript

Loading train (200) and test (40) from file

First three train X:
 -0.1660   0.4406  -0.9998  -0.3953  -0.7065
  0.0776  -0.1616   0.3704  -0.5911   0.7562
 -0.9452   0.3409  -0.1654   0.1174  -0.7192

First three train y:
   0.4840
   0.1568
   0.8054

Creating and training model

Setting SGD lrnRate = 0.001
Setting SGD maxEpochs = 200
epoch =      0  MSE = 0.1095  acc = 0.0000
epoch =     40  MSE = 0.0027  acc = 0.5050
epoch =     80  MSE = 0.0026  acc = 0.4650
epoch =    120  MSE = 0.0026  acc = 0.4600
epoch =    160  MSE = 0.0026  acc = 0.4600
Done

Model weights:
 -0.2656   0.0333  -0.0453   0.0358  -0.1146
Model bias: 0.3620

Computing model accuracy

Train acc (within 0.15) = 0.6400
Test acc (within 0.15) = 0.7750

Train MSE = 0.0026
Test MSE = 0.0020

Predicting for x =
  -0.1660    0.4406   -0.9998   -0.3953   -0.7065
Predicted y = 0.5329

End demo

The demo data is synthetic. It as generated by a 5-10-1 neural network with random weights and bias values. This explains why the model accuracy is not very good (64.00% = 128 out of 200 correct, where a prediction is scored as correct if it’s within 15% of the true target value.).

Linear regression is the simplest form of machine learning prediction. Linear regression works well only when the source data is linear, or nearly-linear, meaning the relationship between predictor variables and the target variable can be defined by the math equation y’ = (w0 * x0) + . . + (wn * xn) + b. In most cases, you won’t know beforehand if your data is linear.

In many scenarios, it’s a good idea to start with linear regression. If your linear regression model does not predict well, then you can try more complex math-based techniques including k-nearest neighbors regression, linear regression with interactions, kernel ridge regression, and neural network regression.



Pinball machine were outlawed in New York and most other states in 1942. They were judged to be gambling games of pure luck in disguise. (The machines’ owners would manually pay players from a hidden cache of money).

In 1976, pinball expert Roger Sharpe was hired by the Music and Amusement Association of New York to be their star witness in their attempt to overturn the ban on pinball. The plan was for Sharpe to play the “El Dorado” pinball machine in front of the City Council, to demonstrate pinball is a game of skill, not chance. Sharpe was very familiar with “El Dorado”. Before the scheduled meeting, an “El Dorado” machine was moved into the Council Chambers.

At the last second, the City Council, suspecting a trick of some sort, demanded that Sharpe play on the “Bank Shot” machine — a pinball machine Sharpe had never played before! The Council had secretly moved a ‘Bank Shot” machine into the Chambers.

Anyway, after a few games of warmup, Sharpe successfully “called his shot” on “Bank Shot”, and the City Council overturned the ban on pinball machines in New York.

Pinball machine art is an interesting topic all by itself. Pinball art from the 1970s has a distinctive linear style.


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

Leave a Reply