资源简介
1、掌握数据预处理的方法,对数据进行预处理;
2、掌握基本K-MEANS算法的使用;
代码片段和文件信息
// K-MEANS.cpp : 定义控制台应用程序的入口点。
//
#include “stdafx.h“
#include
#include
#include
#include
#include
#include
#define k 5//簇的数目
using namespace std;
//存放元组的属性信息
typedef vector Tuple;//存储每条数据记录
int dataNum;//数据集中数据记录数目
int dimNum;//每条记录的维数
ofstream fileout(“out.txt“);
//计算两个元组间的欧几里距离
double getDistXY(const Tuple& t1 const Tuple& t2)
{
double sum = 0;
for (int i = 1; i <= dimNum; ++i)
{
sum += (t1[i] - t2[i]) * (t1[i] - t2[i]);
}
return sqrt(sum);
}
//根据质心,决定当前元组属于哪个簇
int clusterOfTuple(Tuple means[] const Tuple& tuple)
{
double dist = getDistXY(means[0] tuple);
double tmp;
int label = 0;//标示属于哪一个簇
for (int i = 1; i tmp = getDistXY(means[i] tuple);
if (tmp }
return label;
}
//获得给定簇集的平方误差
double getVar(vector clusters[] Tuple means[])
{
double var = 0;
for (int i = 0; i < k; i++)
{
vector t = clusters[i];
for (int j = 0; j< t.size(); j++)
{
var += getDistXY(t[j] means[i]);
}
}
//cout<<“sum:“< return var;
}
//获得当前簇的均值(质心)
Tuple getMeans(const vector& cluster)
{
int num = cluster.size();
Tuple t(dimNum + 1 0);
for (int i = 0; i < num; i++)
{
for (int j = 1; j <= dimNum; ++j)
{
t[j] += cluster[i][j];
}
}
for (int j = 1; j <= dimNum; ++j)
t[j] /= num;
return t;
//cout<<“sum:“< }
void print(const vector clusters[])
{
for (int lable = 0; lable {
cout << “第“ << lable + 1 << “个簇:“ << endl;
fileout << “第“ << lable + 1 << “个簇:“ << endl;
vector t = clusters[lable];
for (int i = 0; i {
cout << i + 1 << “.(“;
fileout << i + 1 << “.(“;
for (int j = 0; j <= dimNum; ++j)
{
cout << t[i][j] << “ “;
fileout << t[i][j] << “ “;
}
cout << “)\n“;
fileout << “)\n“;
}
}
}
void KMeans(vector& tuples)
{
vector clusters[k];//k个簇
Tuple means[k];//k个中心点
int i = 0;
//一开始随机选取k条记录的值作为k个簇的质心(均值)
//srand((unsigned int)time(NULL));
for (i = 0; i {
int iToSelect = rand() % tuples.size();
if (means[iToSelect].size() == 0)
{
for (int j = 0; j <= dimNum; ++j)
{
means[i].push_back(tuples[iToSelect][j]);
}
++i;
}
}
int lable = 0;
//根据默认的质心给簇赋值
for (i = 0; i != tuples.size(); ++i){
lable = clusterOfTuple(means tuples[i]);
clusters[lable].push_back(tuples[i]);
}
double oldVar = -1;
double newVar = getVar(clusters means);
cout << “初始的的整体误差平方和为:“ << newVar << endl;
fileout << “初始的的整体误差平方和为:“ << newVar << endl;
int t = 0;
while (abs(newVar - oldVar) >= 0.5) //当新旧函数值相差不到1即准则函数值不发生明显变化时,算法终止
{
cout << “第 “ << ++t << “ 次迭代开始:“ << endl;
fileout << “第 “ << ++t << “ 次迭代开始:“ << endl;
for (i = 0; i < k; i++) //更新每个簇的中心点
{
means[i] = getMeans(cluste
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3584142 2017-05-23 10:21 K-MEANS\K-MEANS\idf.txt
文件 202230 2013-05-22 13:30 K-MEANS\K-MEANS\input.txt
文件 12300 2017-05-16 14:23 K-MEANS\K-MEANS\iris.txt
文件 4691 2017-05-23 10:26 K-MEANS\K-MEANS\K-MEANS.cpp
文件 4532 2017-05-16 14:11 K-MEANS\K-MEANS\K-MEANS.vcxproj
文件 1314 2017-05-16 14:11 K-MEANS\K-MEANS\K-MEANS.vcxproj.filters
文件 1414 2017-05-16 15:07 K-MEANS\K-MEANS\kmeans.txt
文件 1416464 2017-05-23 10:28 K-MEANS\K-MEANS\out.txt
文件 1510 2017-05-16 14:11 K-MEANS\K-MEANS\ReadMe.txt
文件 213 2017-05-16 14:11 K-MEANS\K-MEANS\stdafx.cpp
文件 234 2017-05-16 14:11 K-MEANS\K-MEANS\stdafx.h
文件 236 2017-05-16 14:11 K-MEANS\K-MEANS\targetver.h
文件 858543 2017-05-16 14:19 K-MEANS\K-MEANS\train.data
文件 858545 2009-04-24 16:10 K-MEANS\K-MEANS\train.data.bak
文件 967 2017-05-16 14:11 K-MEANS\K-MEANS.sln
目录 0 2017-08-03 22:50 K-MEANS\K-MEANS
目录 0 2017-08-03 22:50 K-MEANS
----------- --------- ---------- ----- ----
6947335 17
- 上一篇:单播通信实验
- 下一篇:一键生成带有所有文件名的Excel表格
评论
共有 条评论