can you use c++ write a k-means
时间: 2024-04-29 15:21:05 浏览: 7
Yes, it is possible to use the C programming language to implement the k-means algorithm. Here is an example implementation:
```
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define K 3 // number of clusters
#define N 10 // number of data points
#define D 2 // number of dimensions
typedef struct {
double x[D];
} Point;
double distance(Point p1, Point p2) {
double sum = 0;
for (int i = 0; i < D; i++) {
sum += pow(p1.x[i] - p2.x[i], 2);
}
return sqrt(sum);
}
int main() {
Point data[N] = { {1, 2}, {2, 1}, {2, 3}, {3, 2}, {5, 6}, {6, 5}, {6, 7}, {7, 6}, {9, 8}, {8, 9} };
Point centroids[K] = { {1, 2}, {5, 6}, {9, 8} };
int assignment[N];
// repeat until convergence
int iter = 0;
while (1) {
// assignment step
int changed = 0;
for (int i = 0; i < N; i++) {
double min_dist = INFINITY;
int min_idx;
for (int j = 0; j < K; j++) {
double dist = distance(data[i], centroids[j]);
if (dist < min_dist) {
min_dist = dist;
min_idx = j;
}
}
if (assignment[i] != min_idx) {
assignment[i] = min_idx;
changed = 1;
}
}
if (!changed) {
break;
}
// update step
int counts[K] = { 0 };
for (int i = 0; i < N; i++) {
int idx = assignment[i];
for (int j = 0; j < D; j++) {
centroids[idx].x[j] += data[i].x[j];
}
counts[idx]++;
}
for (int j = 0; j < K; j++) {
if (counts[j] > 0) {
for (int l = 0; l < D; l++) {
centroids[j].x[l] /= counts[j];
}
}
}
iter++;
}
// print results
printf("Iterations: %d\n", iter);
for (int i = 0; i < K; i++) {
printf("Centroid %d: (%.2f, %.2f)\n", i+1, centroids[i].x[0], centroids[i].x[1]);
}
for (int i = 0; i < N; i++) {
printf("Data point %d assigned to centroid %d\n", i+1, assignment[i]+1);
}
return 0;
}
```
This implementation uses a fixed dataset of 10 two-dimensional points and 3 clusters. The algorithm iteratively assigns each data point to the nearest centroid and updates the centroids based on the assigned points. It continues until convergence, i.e., when no data points change assignments. The final centroids and assignments are printed to the console. Note that this implementation does not handle error cases or input validation, and can be improved for better performance.