K均值算法用c语言实现
时间: 2023-08-04 20:33:18 浏览: 84
以下是用 C 语言实现 K 均值算法的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX_ITERATIONS 100 // 最大迭代次数
#define NUM_POINTS 10 // 数据点数量
#define NUM_CLUSTERS 2 // 聚类数量
// 定义数据点结构体
typedef struct {
double x, y;
} Point;
// 计算两个点之间的距离
double distance(Point p1, Point p2) {
double dx = p1.x - p2.x;
double dy = p1.y - p2.y;
return sqrt(dx * dx + dy * dy);
}
// 初始化聚类中心点
void init_clusters(Point* data, Point* clusters) {
// 将前两个数据点作为初始聚类中心点
clusters[0] = data[0];
clusters[1] = data[1];
}
// 将数据点划分到最近的聚类中心点
void classify_points(Point* data, Point* clusters, int* assignments) {
for (int i = 0; i < NUM_POINTS; i++) {
double min_distance = INFINITY;
int min_index = -1;
for (int j = 0; j < NUM_CLUSTERS; j++) {
double d = distance(data[i], clusters[j]);
if (d < min_distance) {
min_distance = d;
min_index = j;
}
}
assignments[i] = min_index;
}
}
// 重新计算聚类中心点
void update_clusters(Point* data, Point* clusters, int* assignments) {
int counts[NUM_CLUSTERS] = {0};
double sum_x[NUM_CLUSTERS] = {0.0};
double sum_y[NUM_CLUSTERS] = {0.0};
for (int i = 0; i < NUM_POINTS; i++) {
int cluster = assignments[i];
counts[cluster]++;
sum_x[cluster] += data[i].x;
sum_y[cluster] += data[i].y;
}
for (int j = 0; j < NUM_CLUSTERS; j++) {
if (counts[j] > 0) {
clusters[j].x = sum_x[j] / counts[j];
clusters[j].y = sum_y[j] / counts[j];
}
}
}
// 打印聚类结果
void print_clusters(Point* clusters) {
printf("Clusters:\n");
for (int i = 0; i < NUM_CLUSTERS; i++) {
printf("(%f, %f)\n", clusters[i].x, clusters[i].y);
}
}
int main() {
// 初始化数据点
Point data[NUM_POINTS] = {{1, 1}, {2, 1}, {4, 3}, {5, 4}, {7, 5},
{10, 7}, {11, 5}, {12, 6}, {14, 7}, {15, 5}};
// 初始化聚类中心点
Point clusters[NUM_CLUSTERS];
init_clusters(data, clusters);
// 迭代计算聚类结果
int assignments[NUM_POINTS];
for (int i = 0; i < MAX_ITERATIONS; i++) {
classify_points(data, clusters, assignments);
update_clusters(data, clusters, assignments);
}
// 打印聚类结果
print_clusters(clusters);
return 0;
}
```
这个示例代码演示了如何使用 K 均值算法将 10 个二维数据点划分为 2 个聚类。
阅读全文