I wrote an article titled “Anomaly Detection Using K-Means Clustering with JavaScript” in the December 2025 edition of Microsoft Visual Studio Magazine. See https://visualstudiomagazine.com/articles/2025/12/01/anomaly-detection-using-k-means-clustering-with-javascript.aspx.
Anomaly detection is the process of examining a dataset to find data items that are unusual in some way. The terms anomaly detection and outlier detection are often used interchangeably.
There are many different anomaly detection techniques. Each technique has pros and cons. My article explains how to identify anomalous data items using k-means data clustering, implemented with the JavaScript language. The idea is simple: cluster the dataset of interest, then find the data items that are most different from their associated cluster means.
The output of the demo program is:
Begin k-means anomaly detection JavaScript Loading synthetic (200) data X data: [ 0] -0.1660 0.4406 -0.9998 -0.3953 -0.7065 0.4840 [ 1] 0.0776 -0.1616 0.3704 -0.5911 0.7562 0.1568 [ 2] -0.9452 0.3409 -0.1654 0.1174 -0.7192 0.8054 [ 3] 0.9365 -0.3732 0.3846 0.7528 0.7892 0.1345 [ 4] -0.8299 -0.9219 -0.6603 0.7563 -0.8033 0.7955 . . . Clustering with k=3 Done Clustering: 2 0 1 0 1 2 1 2 1 1 2 2 0 2 1 2 1 0 1 2 1 2 0 2 1 0 1 2 2 1 2 0 0 0 1 0 2 0 2 0 1 0 0 1 1 0 2 1 2 1 1 2 2 0 0 2 0 2 0 1 2 1 1 0 2 0 0 1 1 1 0 0 2 1 0 1 1 1 0 1 0 1 0 1 0 0 2 2 1 1 0 0 0 0 2 1 1 2 1 2 2 1 1 1 0 0 0 0 2 2 1 2 0 0 0 0 0 1 0 0 2 0 0 2 1 0 1 2 2 1 2 1 2 1 1 1 1 0 2 1 2 2 1 2 1 1 0 0 2 0 2 2 1 0 2 0 1 1 0 0 1 1 0 0 2 2 0 2 2 0 1 0 0 2 1 2 2 2 1 1 2 1 0 0 2 1 2 2 1 0 0 2 2 2 1 0 1 0 2 1 Means: [ 0] 0.2662 -0.3617 0.5050 0.0062 0.2880 0.2144 [ 1] -0.6035 0.0620 -0.1627 0.0374 -0.1405 0.5631 [ 2] 0.4208 0.3543 -0.3674 -0.1722 0.0537 0.2609 WCSS: 240.7398 Anomalies: cluster idx distance data[idx] ----------------------------------------- 0 [107] 1.6625 [ 0.51 0.43 0.97 -0.44 -0.99 0.28 ] 1 [136] 1.6207 [ -0.05 0.97 0.89 0.29 -0.69 0.49 ] 2 [138] 1.6812 [ 0.15 0.84 -0.87 0.98 -0.89 0.42 ] End demo
This means data item [0] is assigned to cluster 2, item [1] is assigned to cluster 0, item [2] is assigned to cluster 1, and so on.
The WCSS (within cluster sum of squares) is a measure of how well the data has been clustered, where smaller values are better.
For cluster 0, the data item that belongs to the cluster that is farthest from the mean of cluster 0 is item [107] = (0.5175, 0.4339, 0.9747, -0.4438, -0.9924, 0.2879). That item is a distance of 1.6625 away from the mean of cluster 0. For cluster 1, the most anomalous item is [136], and for cluster 2, the most anomalous item is [138].
Anomaly detection using k-means clustering is just one of many anomaly detection techniques. Different techniques tend to find different types of anomalies. Other clustering-based anomaly detection techniques includes DBSCAN anomaly detection and self-organizing map anomaly detection. Another category of anomaly detection techniques analyze source data to generate a math model of the structure of the data, and then analyze reconstruction error to find anomalous items. Examples includes neural network autoencoder anomaly detection and quadratic regression anomaly detection.

“Amazing Stories” is a science fiction magazine. It was launched in April 1926 and was the first magazine devoted solely to science fiction. The magazine has been published for 99 years (but with significant interupptions).
Covers featuring giant insects were not anomalous.
Left: July 1926, big fly. Center: October 1926, big roach. Right: November 1958, big ant.

.NET Test Automation Recipes
Software Testing
SciPy Programming Succinctly
Keras Succinctly
R Programming
2026 Visual Studio Live
2025 Summer MLADS Conference
2025 DevIntersection Conference
2025 Machine Learning Week
2025 Ai4 Conference
2025 G2E Conference
2025 iSC West Conference
You must be logged in to post a comment.