private void CalculateClusterCenters() { for (int j = 0; j < this.Clusters.Count; j++) { ClusterCentroid c = this.Clusters[j]; double uX = 0.0; double uY = 0.0; double l = 0.0; for (int i = 0; i < this.Points.Count; i++) { ClusterPoint p = this.Points[i]; double uu = Math.Pow(U[i, j], this.Fuzzyness); uX += uu * c.X; uY += uu * c.Y; l += uu; } c.X = ((int)(uX / l)); c.Y = ((int)(uY / l)); this.Log += string.Format("Cluster Centroid: ({0}; {1})" + System.Environment.NewLine, c.X, c.Y); } }
public CMeansAlgorithm(List <ClusterPoint> points, List <ClusterCentroid> clusters, float fuzzy) { if (points == null) { throw new ArgumentNullException("points"); } if (clusters == null) { throw new ArgumentNullException("clusters"); } this.Points = points; this.Clusters = clusters; U = new double[this.Points.Count, this.Clusters.Count]; //Uk = new double[this.Points.Count, this.Clusters.Count]; this.Fuzzyness = fuzzy; double diff; // Iterate through all points to create initial U matrix for (int i = 0; i < this.Points.Count; i++) { ClusterPoint p = this.Points[i]; double sum = 0.0; for (int j = 0; j < this.Clusters.Count; j++) { ClusterCentroid c = this.Clusters[j]; diff = Math.Sqrt(Math.Pow(p.X - c.X, 2.0) + Math.Pow(p.Y - c.Y, 2.0)); U[i, j] = (diff == 0) ? Eps : diff; sum += U[i, j]; } double sum2 = 0.0; for (int j = 0; j < this.Clusters.Count; j++) { U[i, j] = 1.0 / Math.Pow(U[i, j] / sum, 2.0 / (Fuzzyness - 1.0)); sum2 += U[i, j]; } for (int j = 0; j < this.Clusters.Count; j++) { U[i, j] = U[i, j] / sum2; } } this.RecalculateClusterIndexes(); }
private double CalculateEulerDistance(ClusterPoint p, ClusterCentroid c) { return(Math.Sqrt(Math.Pow(p.X - c.X, 2) + Math.Pow(p.Y - c.Y, 2))); }
private void ExecAlg() { progressBar1.Value = 0; Point pt = new Point(); if (dgvGiven.Rows.Count - 1 > 0) { txtResult.Clear(); List <PointInfo> points = new List <PointInfo>(); for (int i = 0; i < dgvGiven.Rows.Count - 1; i++) { pt.X = Convert.ToInt32(dgvGiven.Rows[i].Cells[0].Value); pt.Y = Convert.ToInt32(dgvGiven.Rows[i].Cells[1].Value); points.Add(new PointInfo(pt.X, pt.Y)); } if (rbAlgDBSCAN.Checked) { double eps = (double)nudEps.Value; int minPts = (int)nudMinPts.Value; List <List <PointInfo> > clusters = DBSCANClass.GetClusters(points, eps, minPts); ShowResultLog(clusters, points); } else if (rbAlgKMean.Checked) { int K = (int)nudNumberOfClusters.Value; int PointsCount = dgvGiven.Rows.Count - 1; double[][] rawData = new double[PointsCount][]; for (int i = 0; i < PointsCount; i++) { rawData[i] = new double[] { Convert.ToDouble(dgvGiven.Rows[i].Cells[0].Value), Convert.ToDouble(dgvGiven.Rows[i].Cells[1].Value) }; } List <List <PointInfo> > clusters = K_Mean.GetClusters(rawData, K); //List<PointInfo> results = KMeanPoints.OrderBy(x => x.ClusterId).ToList(); ShowResultLog(clusters, points); } else if (rbAlgFCM.Checked) { int CentroidCount = 0; List <ClusterPoint> clusterPoints = new List <ClusterPoint>(); List <ClusterCentroid> centroids = new List <ClusterCentroid>(); for (int i = 0; i < dgvGiven.Rows.Count - 1; i++) { double x = Convert.ToDouble(dgvGiven.Rows[i].Cells[0].Value); double y = Convert.ToDouble(dgvGiven.Rows[i].Cells[1].Value); clusterPoints.Add(new ClusterPoint(x, y)); if ((bool)dgvGiven.Rows[i].Cells[3].Value == true) { CentroidCount++; centroids.Add(new ClusterCentroid(x, y)); } } if (CentroidCount != (int)nudNumberOfClusters.Value) { MessageBox.Show("The number of Centroids (Clusters) is not equal to number of selected centroid points\r\nPlease choose centroid points equal to number of clusters count", "Error on centroid points selection", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { CMeansAlgorithm alg = new CMeansAlgorithm(clusterPoints, centroids, (float)nudFuzzyExp.Value); int iterations = alg.Run(Math.Pow(10, -5), (int)nudIterations.Value); double[,] Matrix = alg.U; txtResult.Clear(); txtResult.Text = string.Format("FCM Algorithm Results with ({0}):\r\n", iterations); for (int j = 0; j < clusterPoints.Count; j++) { for (int i = 0; i < centroids.Count; i++) { ClusterPoint p = clusterPoints[j]; txtResult.Text += string.Format("\r\n{0:00} Point: ({1};{2}) ClusterIndex: {3} Value: {4:0.000}", j + 1, p.X, p.Y, p.ClusterIndex, Matrix[j, i]); pt.X = (int)p.X; pt.Y = (int)p.Y; PutPoint(pt, getColor((int)p.ClusterIndex), tbZoom.Value + 1); } } txtResult.Text += string.Format("\r\nIteration count: {0}", iterations); } } } else { MessageBox.Show("There are no ay information to calculate DBSCAN", "No data exist to mining", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }