, () , , , , . UpdateMeans (. 4).
. 4. UpdateMeans
1. static void UpdateMeans(double [][] rawData, int [] clustering,
2. double [][] means)
3. {
4. int numClusters = means.Length;
5. for (int k = 0; k < means.Length; ++k)
6. for (int j = 0; j < means[k].Length; ++j)
7. means[k][j] = 0.0;
8. int [] clusterCounts = new int [numClusters];
9. for (int i = 0; i < rawData.Length; ++i)
10. {
11. int cluster = clustering[i];
12. ++clusterCounts[cluster];
13. for (int j = 0; j < rawData[i].Length; ++j)
14. means[cluster][j] += rawData[i][j];
15. }
16. for (int k = 0; k < means.Length; ++k)
17. for (int j = 0; j < means[k].Length; ++j)
18. means[k][j] /= clusterCounts[k]; //
19. return;
20. }
UpdateMeans , means , . , means , , , , (ref parameter). means Allocate:
1. static double [][] Allocate(int numClusters, int numAttributes)
2. {
3. double [][] result = new double [numClusters][];
4. for (int k = 0; k < numClusters; ++k)
5. result[k] = new double [numAttributes];
6. return result;
7. }
means , . , means[0][1] = 150.33, (1) 0 150.33.
UpdateMeans means, , , , . , , - 0, .
ComputeCentroid (. 5) , . . , .
. 5. ComputeCentroid
1. static double [] ComputeCentroid(double [][] rawData, int [] clustering,
2. int cluster, double [][] means)
3. {
4. int numAttributes = means[0].Length;
5. double [] centroid = new double [numAttributes];
6. double minDist = double. MaxValue;
7. for (int i = 0; i < rawData.Length; ++i) //
8. {
9. int c = clustering[i];
10. if (c!= cluster) continue;
|
|
11. double currDist = Distance(rawData[i], means[cluster]);
12. if (currDist < minDist)
13. {
14. minDist = currDist;
15. for (int j = 0; j < centroid.Length; ++j)
16. centroid[j] = rawData[i][j];
17. }
18. }
19. return centroid;
20. }
ComputeCentroid , , . , Distance. , ( ), .
UpdateCentroids ComputeCentroid , :
1. static void UpdateCentroids(double [][] rawData, int [] clustering,
2. double [][] means, double [][] centroids)
3. {
4. for (int k = 0; k < centroids.Length; ++k)
5. {
6. double [] centroid = ComputeCentroid(rawData, clustering, k, means);
7. centroids[k] = centroid;
8. }
9. }
UpdateCentroids , centroids . centroids means: , .
, (), . , . , .