kmeans聚类算法c代码
时间: 2023-06-23 20:50:29 浏览: 77
kmeans聚类算法matlab代码.zip
下面是K-means聚类算法的C语言实现代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define K 3 // 聚类数
#define N 10 // 数据点数
#define D 2 // 数据维度
// 随机生成数据集
void generate_data(float data[][D], int n) {
srand((unsigned int) time(NULL));
for (int i = 0; i < n; i++) {
for (int j = 0; j < D; j++) {
data[i][j] = (float) rand() / RAND_MAX;
}
}
}
// 计算两个点之间的距离
float distance(float *a, float *b) {
float diff[D];
for (int i = 0; i < D; i++) {
diff[i] = a[i] - b[i];
}
float dist = 0;
for (int i = 0; i < D; i++) {
dist += pow(diff[i], 2);
}
return sqrt(dist);
}
int main() {
float data[N][D];
generate_data(data, N);
float centroids[K][D];
for (int i = 0; i < K; i++) {
for (int j = 0; j < D; j++) {
centroids[i][j] = data[i][j];
}
}
int assignments[N];
float distances[N];
int changed = 1;
while (changed) {
changed = 0;
// 计算每个点到每个聚类中心的距离
for (int i = 0; i < N; i++) {
float min_dist = INFINITY;
int min_idx;
for (int j = 0; j < K; j++) {
float dist = distance(data[i], centroids[j]);
if (dist < min_dist) {
min_dist = dist;
min_idx = j;
}
}
if (assignments[i] != min_idx) {
changed = 1;
}
assignments[i] = min_idx;
distances[i] = min_dist;
}
// 更新聚类中心
for (int i = 0; i < K; i++) {
float sum[D] = {0};
int count = 0;
for (int j = 0; j < N; j++) {
if (assignments[j] == i) {
for (int k = 0; k < D; k++) {
sum[k] += data[j][k];
}
count++;
}
}
if (count > 0) {
for (int k = 0; k < D; k++) {
centroids[i][k] = sum[k] / count;
}
}
}
}
// 输出结果
for (int i = 0; i < K; i++) {
printf("Cluster %d:\n", i);
for (int j = 0; j < N; j++) {
if (assignments[j] == i) {
printf("%f, %f\n", data[j][0], data[j][1]);
}
}
printf("\n");
}
return 0;
}
```
这是一个简单的K-means聚类算法实现,可以通过随机生成数据集进行测试,最终输出聚类结果。
阅读全文