C++实现kmeans聚类算法
时间: 2023-12-24 08:03:34 浏览: 87
源码文档KMEANS-聚类算法实现程序
以下是一个简单的C++实现K-means聚类算法的示例:
```cpp
#include <iostream>
#include <vector>
#include <cmath>
// 计算两个点之间的欧氏距离
double distance(std::vector<double>& point1, std::vector<double>& point2) {
double sum = 0.0;
for (int i = 0; i < point1.size(); i++) {
sum += pow(point1[i] - point2[i], 2);
}
return sqrt(sum);
}
// 执行K-means聚类算法
void kmeans(std::vector<std::vector<double>>& data, int k) {
int n = data.size();
int dim = data[0].size();
// 初始化聚类中心
std::vector<std::vector<double>> centroids(k, std::vector<double>(dim));
for (int i = 0; i < k; i++) {
centroids[i] = data[i];
}
// 迭代更新聚类中心
bool converged = false;
while (!converged) {
std::vector<int> clusterCounts(k, 0);
std::vector<std::vector<double>> newCentroids(k, std::vector<double>(dim));
// 分配数据点到最近的聚类中心
for (int i = 0; i < n; i++) {
double minDist = std::numeric_limits<double>::max();
int closestCentroid = 0;
for (int j = 0; j < k; j++) {
double dist = distance(data[i], centroids[j]);
if (dist < minDist) {
minDist = dist;
closestCentroid = j; }
}
clusterCounts[closestCentroid]++;
for (int d = 0; d < dim; d++) {
newCentroids[closestCentroid][d] += data[i][d];
}
}
// 更新聚类中心
converged = true;
for (int i = 0; i < k; i++) {
for (int d = 0; d < dim; d++) {
if (clusterCounts[i] > 0) {
newCentroids[i][d] /= clusterCounts[i];
}
if (newCentroids[i][d] != centroids[i][d]) {
converged = false;
}
centroids[i][d] = newCentroids[i][d];
}
}
}
// 输出聚类结果
for (int i = 0; i < k; i++) {
std::cout << "Cluster " << i << ": ";
for (int d = 0; d < dim; d++) {
std::cout << centroids[i][d] << " ";
}
std::cout << std::endl;
}
}
int main() {
// 示例数据
std::vector<std::vector<double>> data = {{1.0, 2.0}, {2.0, 1.0}, {10.0, 12.0}, {12.0, 10.0}, {20.0, 22.0}, {22.0, 20.0}};
int k = 2; // 聚类数
// 执行K-means聚类算法
kmeans(data, k);
return 0;
}
```
阅读全文