gpt4 book ai didi

c# - K均值算法C#

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:19:39 26 4
gpt4 key购买 nike

我尝试在 C# 中实现 K-means 算法,但不知何故它的输出是黑色(小)图像。我写了下面的代码:

public static Color[,] Kmeans(int number_clusters, Vector[,] input, int iterations)
{
int length = input.GetLength(0);
int height = input.GetLength(1);

Random randomizer = new Random();

//Inits centroides
List<Vector> centroides = new List<Vector>(number_clusters);
List<List<Vector>> clusters = new List<List<Vector>>(number_clusters);

for(int i = 0; i < number_clusters; i++)
{
Vector centroid = input[randomizer.Next(0, length), randomizer.Next(0, height)];

List<Vector> cluster = new List<Vector>();

cluster.Add(centroid);
clusters.Add(cluster);

centroides.Add(centroid);
}

int count = 0;

while (count < iterations) {
for (int x = 0; x < input.GetLength(0); x++)
{
for (int y = 0; y < input.GetLength(1); y++)
{
Vector currentVector = input[x, y];
double min_distance = Double.PositiveInfinity;
Vector closest_centroid = centroides[0];

//Finds closest centroid
foreach (Vector centroid in centroides)
{
double distance = currentVector.Distance(centroid);
if (Math.Pow(distance, 2) < min_distance)
{
min_distance = Math.Pow(distance, 2);
closest_centroid = centroid;
}
}

//Find cluster with that centroid in it
List<Vector> to_assign = clusters[0];
foreach (List<Vector> cluster_list in clusters)
{
if (cluster_list[0].r == closest_centroid.r && cluster_list[0].g == closest_centroid.g && cluster_list[0].b == closest_centroid.b)
{
to_assign = cluster_list;
break;
}
}

to_assign.Add(currentVector);
int current_length = to_assign.Count();
Vector current_centroid = to_assign[0];

Vector new_centroid = new Vector(0, 0, 0);

foreach (Vector vector in to_assign)
{
new_centroid.Sum(vector);
}

new_centroid = new Vector(new_centroid.r / current_length, new_centroid.g / current_length, new_centroid.b / current_length);
to_assign.RemoveAt(0);
to_assign.Insert(0, new_centroid);

for (int i = 0; i < centroides.Count(); i++)
{
if (centroides[i].r == current_centroid.r && centroides[i].g == current_centroid.g && current_centroid.b == centroides[i].b)
{
centroides[i] = new_centroid;
break;
}
}
}
}
count++;
}

foreach(List<Vector> cluster_list in clusters)
{
Vector current_centroid = cluster_list[0];
for(int i = 1; i < cluster_list.Count(); i++)
{
cluster_list[i].r = current_centroid.r;
cluster_list[i].g = current_centroid.g;
cluster_list[i].b = current_centroid.b;
}
}

Color[,] output = new Color[length, height];
foreach (List<Vector> cluster_list in clusters)
{
for (int i = 0; i < cluster_list.Count(); i++)
{
Vector current_vector = cluster_list[i];
output[current_vector.x, current_vector.y] = Color.FromArgb((int) current_vector.r, (int) current_vector.g, (int) current_vector.b);
}
}

return output;
}

Vector 是以下类:

public class Vector
{
public double r, g, b;
public int x, y;
public Vector(double r, double g, double b)
{
this.r = r;
this.g = g;
this.b = b;
}

public void SetCoordinates(int x, int y)
{
this.x = x;
this.y = y;
}

public double Distance(Vector v2)
{
double r_power = Math.Pow((r - v2.r), 2);
double g_power = Math.Pow((g - v2.g), 2);
double b_power = Math.Pow((b - v2.b), 2);

double distance = Math.Sqrt((r_power + g_power + b_power));

return distance;
}

public Vector Product(int scalar)
{
return new Vector(r * scalar, g * scalar, b * scalar);
}

public double Length()
{
return Math.Sqrt((Math.Pow(r, 2) + Math.Pow(g, 2) + Math.Pow(b, 2)));
}

public Vector Sum(Vector v2)
{
double new_r = r + v2.r;
double new_g = g + v2.g;
double new_b = b + v2.b;

return new Vector(new_r, new_g, new_b);
}
}

我尝试通过以下算法描述来实现代码:

将对象分配给集群:对于每个对象v在测试集中做以下步骤:1个计算平方距离之间v和每个质心k每个簇 (d2个(v,k)).2个分配对象v到具有最近质心的集群。更新质心:对于每个集群k计算它们的平均向量。平均向量的计算类似于数字的平均值:我们对集群中的所有向量求和(用向量和)然后除(与逆的标量积)乘以簇中的向量数。重复以上两步

谁能帮我解决这个算法

最佳答案

我找到了解决方案:

    foreach (Vector vector in to_assign)
{
new_centroid.Sum(vector);
}

应该改为:

foreach (Vector vector in to_assign)
{
new_centroid = new_centroid.Sum(vector);
}

这是因为 Sum 方法返回一个新的 Vector,现在不存储

关于c# - K均值算法C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50665215/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com