The library for statistical analysis.
To install Bellona.Analysis, run the following command in the Package Manager Console on Visual Studio:
Install-Package Bellona.Analysis
NuGet Gallery | Bellona.Analysis
The following code creates a trained clustering model with colors data.
// Colors data.
var colors = new[] { Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Blue, Color.Indigo, Color.Violet };
// Initializes and trains a clustering model.
// The lambda expression is to extract features from each color by an array of System.Double.
var model = ClusteringModel.CreateAuto<Color>(c => new double[] { c.R, c.G, c.B })
.Train(colors);
The number of clusters doesn't need to be specified for CreateAuto
method.
Remark that ClusteringModel<T>
objects are immutable.
CreateAuto
method returns an empty model, and Train
method returns a trained model.
So use method chaining.
You can access the trained result via Clusters
property.
// Gets a cluster in the trained model.
var cluster0 = model.Clusters[0];
Console.WriteLine(cluster0.Id);
// Enumerates colors in cluster0.
foreach (var record in cluster0.Records)
Console.WriteLine(record.Element.Name);
// Specifies the maximum number of clusters and the maximum standard score in σ.
var model = ClusteringModel.CreateAuto<Color>(c => new double[] { c.R, c.G, c.B })
.Train(colors, 20, 1.5)
.Train(colors2, 30, 1.4);
Use CreateFromNumber
method to fix the number of clusters.
// Specifies the number of clusters and the maximum number of iterations.
var model = ClusteringModel.CreateFromNumber<Color>(c => new double[] { c.R, c.G, c.B }, 10)
.Train(colors, 30);
Remark that Assign
method doesn't train the model with the new data.
// Assigns the gold to the suitable cluster.
var cluster = model.Assign(Color.Gold);
var autoModel = ClusteringModel.CreateAuto<Color>(c => new double[] { c.R, c.G, c.B })
.Train(colors);
// Converts AutoClusteringModel to ClusteringModel.
var fixedModel = autoModel.ToFixedModel()
.Train(colors2);
var fixedModel = ClusteringModel.CreateFromNumber<Color>(c => new double[] { c.R, c.G, c.B }, 10)
.Train(colors);
// Converts ClusteringModel to AutoClusteringModel.
var autoModel = fixedModel.ToAutoModel()
.Train(colors2);
The sample source code is ClusteringSample.
Clustering colors in the System.Drawing.Color structure:
Clustering the prefectural capitals in Japan by their positions:
- .NET Standard 2.0
- .NET Framework 4.5
- v2.0.15 For .NET Standard.
- v1.1.14 Fix bugs.
- v1.1.13 Add minor utility methods.
- v1.0.10 Add the features for clustering.