/// <summary> /// Initializes a new instance of KMeans algorithm /// </summary> /// /// <param name="k">The number of clusters to divide input data.</param> /// <param name="distance">The distance function to use. Default is to /// use the <see cref="Accord.Math.Distance.SquareEuclidean(double[], double[])"/> distance.</param> /// public KMeans(int k, Func <double[], double[], double> distance) { if (k <= 0) { throw new ArgumentOutOfRangeException("k"); } if (distance == null) { throw new ArgumentNullException("distance"); } this.distance = distance; // To store centroids of the clusters this.proportions = new double[k]; this.centroids = new double[k][]; this.covariances = new double[k][, ]; // Create the object-oriented structure to hold // information about the k-means' clusters. List <KMeansCluster> clusterList = new List <KMeansCluster>(k); for (int i = 0; i < k; i++) { clusterList.Add(new KMeansCluster(this, i)); } this.clusters = new KMeansClusterCollection(this, clusterList); }
/// <summary> /// Initializes a new instance of the KMeans algorithm /// </summary> /// /// <param name="k">The number of clusters to divide the input data into.</param> /// <param name="distance">The distance function to use. Default is to /// use the <see cref="Accord.Math.Distance.SquareEuclidean(double[], double[])"/> distance.</param> /// public KMeans(int k, Func <double[], double[], double> distance) { if (k <= 0) { throw new ArgumentOutOfRangeException("k"); } if (distance == null) { throw new ArgumentNullException("distance"); } // Create the object-oriented structure to hold // information about the k-means' clusters. this.clusters = new KMeansClusterCollection(k, distance); }
/// <summary> /// Initializes a new instance of the KMeans algorithm /// </summary> /// /// <param name="k">The number of clusters to divide the input data into.</param> /// <param name="distance">The distance function to use. Default is to /// use the <see cref="Accord.Math.Distance.SquareEuclidean(double[], double[])"/> distance.</param> /// public KMeans(int k, Func <double[], double[], double> distance) { if (k <= 0) { throw new ArgumentOutOfRangeException("k"); } if (distance == null) { throw new ArgumentNullException("distance"); } this.Tolerance = 1e-5; this.ComputeInformation = true; this.UseCentroidSeeding = true; // Create the object-oriented structure to hold // information about the k-means' clusters. this.clusters = new KMeansClusterCollection(k, distance); }
/// <summary> /// Initializes a new instance of the KMeans algorithm /// </summary> /// /// <param name="k">The number of clusters to divide the input data into.</param> /// <param name="distance">The distance function to use. Default is to use the /// <see cref="Accord.Math.Distance.SquareEuclidean(double[], double[])"/> distance.</param> /// public KMeans(int k, IDistance <double[]> distance) { if (k <= 0) { throw new ArgumentOutOfRangeException("k"); } if (distance == null) { throw new ArgumentNullException("distance"); } this.Tolerance = 1e-5; this.ComputeCovariances = true; this.ComputeError = true; this.UseSeeding = Seeding.KMeansPlusPlus; this.MaxIterations = 0; // Create the object-oriented structure to hold // information about the k-means' clusters. this.clusters = new KMeansClusterCollection(k, distance); }
public static object KCluster(double[,] Data, int NumClusters) { int nRows = Data.GetLength(0), nCols = Data.GetLength(1); double[][] data = new double[nRows][]; for (int r = 0; r < nRows; ++r) { data[r] = new double[nCols]; for (int c = 0; c < nCols; ++c) { data[r][c] = Data[r, c]; } } // Unfortunately, you can't set the initial cluster centers manually here, which // means that every time this function is called, it will return different clusters. aml.KMeans km = new aml.KMeans(NumClusters); aml.KMeansClusterCollection kcc = km.Learn(data); int nClusters = kcc.Count; double[,] ret = new double[nClusters, nCols]; // TODO - 19Apr20 - needs testing, this used to previously put the mean[c] into ret[x,c]? int x = 0; foreach (KMeansClusterCollection.KMeansCluster cc in kcc.Clusters.OrderBy(xx => xx.Centroid.Mean())) { for (int c = 0; c < nCols; ++c) { ret[x, c] = cc.Centroid.Mean(); } x++; } return(ret); }
/// <summary> /// Initializes a new instance of the <see cref="KMeansCluster"/> class. /// </summary> /// /// <param name="owner">The owner collection.</param> /// <param name="index">The cluster index.</param> /// public KMeansCluster(KMeansClusterCollection owner, int index) { this.owner = owner; this.index = index; }