CSharp 实现聚类K-means cluster算法 源代码
时间: 2024-04-24 09:05:57 浏览: 11
非常感谢您的提问,以下是CSharp实现聚类K-means cluster算法的源代码:
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
namespace KMeansCluster
{
public class KMeans
{
private readonly int _clusterCount;
private readonly List<double[]> _data;
private readonly List<double[]> _means;
private readonly List<int> _assignments;
private readonly Random _random;
public KMeans(int clusterCount, List<double[]> data)
{
_clusterCount = clusterCount;
_data = data;
_means = new List<double[]>();
_assignments = new List<int>();
_random = new Random();
}
public List<int> Cluster()
{
InitializeMeans();
bool changed = true;
while (changed)
{
AssignPointsToClusters();
changed = UpdateMeans();
}
return _assignments;
}
private void InitializeMeans()
{
for (int i = 0; i < _clusterCount; i++)
{
int index = _random.Next(_data.Count);
_means.Add(_data[index]);
}
}
private void AssignPointsToClusters()
{
_assignments.Clear();
foreach (double[] point in _data)
{
double distance = double.MaxValue;
int assignment = 0;
for (int i = 0; i < _means.Count; i++)
{
double tempDistance = EuclideanDistance(point, _means[i]);
if (tempDistance < distance)
{
distance = tempDistance;
assignment = i;
}
}
_assignments.Add(assignment);
}
}
private bool UpdateMeans()
{
List<double[]> newMeans = new List<double[]>();
for (int i = 0; i < _clusterCount; i++)
{
var clusterPoints = _data.Where((p, j) => _assignments[j] == i);
if (clusterPoints.Count() == 0)
{
newMeans.Add(_means[i]);
}
else
{
double[] mean = new double[_data[0].Length];
for (int j = 0; j < _data[0].Length; j++)
{
mean[j] = clusterPoints.Average(p => p[j]);
}
newMeans.Add(mean);
}
}
bool changed = !newMeans.SequenceEqual(_means);
_means.Clear();
_means.AddRange(newMeans);
return changed;
}
private double EuclideanDistance(double[] a, double[] b)
{
double sum = 0;
for (int i = 0; i < a.Length; i++)
{
sum += Math.Pow(a[i] - b[i], 2);
}
return Math.Sqrt(sum);
}
}
}
```
希望这个代码能够帮助到您,如果您有任何问题,请随时联系我。