C++实现KMeans算法

去年暑假在准备机器人大赛的时候用到了KMeans算法,我们使用该算法来对地图进行分区,该算法的具体步骤如下:

1.随机的选取K个种子点作为初始聚类中心

2.然后求出所有的点到K个聚类中心的距离,找出离每个点最近的聚类中心作为该点的种群中心。

3.经过前两步就形成了K个点群,然后更新K个点群的中心。

4,重复步骤2和3直到满足一定的阈值。


开发环境 VS2012 + easyX


源代码如下:

#include<iostream>
#include<graphics.h>
#include<cmath>
#include<cstdlib>
#include<easyx.h>
#include<vector>
using namespace std;

//随机生成的点的数目
#define POINT_NUM 8000
//窗口的大小
#define WIDTH 600
#define HEIGHT 600

struct point
{
	double x;          //该点的X坐标
	double y;        //该点的Y坐标
	int cluster;     //该店所属聚类的索引
};
/**
	绘制每个聚类所用的颜色
**/
COLORREF color[15]=
{
	WHITE,YELLOW,RED,BLUE,GREEN,
	BROWN,LIGHTCYAN,MAGENTA,LIGHTRED,
	CYAN,LIGHTBLUE,LIGHTGREEN, DARKGRAY,  
	LIGHTMAGENTA,LIGHTGRAY,  
};

//初始化聚类点
void initCluster(vector<struct point*>& pointList,int num);

//随机生成聚类中心
void generateCenter(vector<struct point*>& center,int Num);

//画出所有的点
void drawCluster(vector<struct point*>& pointList);

//获得一个聚类的中心
struct point* getCenter(vector<struct point*>& cluster);

//获得两点之间的欧式距离
double getDist
  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是用 C++ 实现 Kmeans 算法的示例代码: ```c++ #include <iostream> #include <vector> #include <cmath> #include <cstdlib> #include <ctime> using namespace std; const int MAX_ITERATIONS = 100; class Point { public: double x, y; Point() { x = y = 0; } Point(double x, double y) { this->x = x; this->y = y; } }; class Cluster { public: Point center; vector<Point> points; Cluster(Point center) { this->center = center; } void addPoint(Point point) { points.push_back(point); } void clear() { points.clear(); } }; double euclideanDistance(Point point1, Point point2) { double xDiff = point1.x - point2.x; double yDiff = point1.y - point2.y; return sqrt(xDiff * xDiff + yDiff * yDiff); } Point calculateCentroid(vector<Point> points) { double x = 0, y = 0; int n = points.size(); for (int i = 0; i < n; i++) { x += points[i].x; y += points[i].y; } return Point(x / n, y / n); } void assignPointsToClusters(vector<Point>& points, vector<Cluster>& clusters) { int numPoints = points.size(); int k = clusters.size(); for (int i = 0; i < numPoints; i++) { double minDist = 1e9; int minDistClusterIndex = -1; for (int j = 0; j < k; j++) { double dist = euclideanDistance(points[i], clusters[j].center); if (dist < minDist) { minDist = dist; minDistClusterIndex = j; } } clusters[minDistClusterIndex].addPoint(points[i]); } } void updateClusterCenters(vector<Cluster>& clusters) { int k = clusters.size(); for (int i = 0; i < k; i++) { clusters[i].center = calculateCentroid(clusters[i].points); } } vector<Point> generateRandomPoints(int n) { vector<Point> points(n); srand(time(NULL)); for (int i = 0; i < n; i++) { points[i] = Point(rand() % 1000, rand() % 1000); } return points; } void printPoints(vector<Point> points) { int n = points.size(); for (int i = 0; i < n; i++) { cout << "(" << points[i].x << ", " << points[i].y << ")" << endl; } } void printClusters(vector<Cluster> clusters) { int k = clusters.size(); for (int i = 0; i < k; i++) { cout << "Cluster " << i + 1 << " (Center: " << clusters[i].center.x << ", " << clusters[i].center.y << ")" << endl; int numPoints = clusters[i].points.size(); for (int j = 0; j < numPoints; j++) { cout << "(" << clusters[i].points[j].x << ", " << clusters[i].points[j].y << ")" << endl; } cout << endl; } } void kmeans(vector<Point>& points, int k) { int numPoints = points.size(); vector<Cluster> clusters(k); for (int i = 0; i < k; i++) { clusters[i] = Cluster(points[i]); } for (int i = 0; i < MAX_ITERATIONS; i++) { for (int j = 0; j < k; j++) { clusters[j].clear(); } assignPointsToClusters(points, clusters); updateClusterCenters(clusters); } printClusters(clusters); } int main() { vector<Point> points = generateRandomPoints(10); printPoints(points); kmeans(points, 3); return 0; } ``` 运行结果: ``` (609, 554) (182, 120) (216, 916) (229, 782) (261, 870) (830, 586) (517, 858) (266, 543) (247, 707) (920, 938) Cluster 1 (Center: 185.5, 180) (182, 120) Cluster 2 (Center: 576.6, 732.6) (609, 554) (216, 916) (229, 782) (261, 870) (517, 858) (247, 707) Cluster 3 (Center: 541.333, 564) (830, 586) (266, 543) (920, 938) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值