K-Means Data Clustering from Scratch Using C# -- Visual Studio Magazine
K-Means Data Clustering from Scratch Using C# -- Visual Studio Magazine
NET C#/VB/TYPESCRIPT
The demo program displays the loaded data using the MatShow() function:
Console.WriteLine("Data: ");
MatShow(X, 2, 6); // 2 decimals, 6 wide
MOST POPULAR
In a non-demo scenario, you might want to display all the data to make sure it was loaded
properly.
Console.WriteLine("clustering = ");
VecShow(clustering, 3);
1 1 2 0 0 1 . . .
The clustering information can be displayed in several ways. For example, instead of
displaying by data item, you can display by cluster ID. Or, you can display each data item and
its associated cluster ID on the same line.
Wrapping Up
The k-means implementation presented in this article can be used as-is or modified in several
ways. The demo uses random partition initialization. The order of the N data items is
MOST POPULAR
randomized, then the first k data items are assigned to the first k cluster IDS, and then the
remaining N-k data items are assigned to a random cluster ID. This approach guarantees that
there is at least one data item assigned to each cluster ID but doesn't guarantee that the initial
distribution of cluster ID counts are equal. Instead of assigning random cluster IDs, you can
cycle though the cluster IDs sequentially.
The Cluster() method calls ClusterOnce() this.trials times looking for the clustering that has
the smallest within-cluster sum of squares. Instead of iterating a fixed number of times, you
can track previous-wcss and current-wcss and exit the loop when the two values do not
change after a threshold value (such as N times).
The UpdateClustering() uses Euclidean distance to assign each data item to the cluster ID that
has the closest mean. There are alternatives to Euclidean distance, such as Manhattan
distance and Minkowski distance. However, Euclidean distance works fine in most scenarios.
« previous 1 2
PRINTABLE FORMAT
How Good Is GitHub New .NET 9 Templates 'F# Meets XAML' in Open Fir
Copilot? Depends … for Blazor Hybrid, … Source … Em
Name
Featured
MOST POPULAR
Subscribe on YouTube
.NET Insight
MOST POPULAR
Email Address*
Email Address*
Country*
SUBMIT
Most Popular Articles
Open Source 'Eclipse Theia IDE' Exits Beta to Challenge Visual Studio Code
VSLive! 4-Day Hands-On Training Seminar: Full Stack Hands-On Development with .NET (Core)
July 16-19, 2024
MOST POPULAR
Live! 360 2-Day Hands-On Seminar: Swimming in the Lakes of Microsoft Fabric and AI - A
Hands-on Experience
August 20-21, 2024
VSLive! 2-Day Hands-On Training Seminar: Developing Secure ASP.NET Web Apps
September 24-25, 2024
VSLive! 4-Day Hands-On Training Seminar: Full Stack Hands-On Development with .NET (Core)
December 10-13, 2024
Visual Studio Live! Las Vegas
March 10-14, 2025
Free Webcasts
MoneyTree Achieves Compliance and Speeds Innovation with AWS and Sumo Logic
AI Boardroom
ADTmag
AWS Insider
Campus Technology
Environmental Protection
Live! 360
MCPmag
MedCloudInsider
Pure AI
Redmond
Security Today
Spaces 4 Learning
TechMentor
Techtactics in Education
THE Journal
©1996-2024 1105 Media Inc. See our Privacy Policy, Cookie Policy and Terms of Use. CA: Do Not Sell My
Personal Info
Problems? Questions? Feedback? E-mail us.