资源简介
本程序中,训练样本集含有30个样本,矢量长度为5,对样本{1,18,11,11,0.5513196}进行K=5的K-最近邻分类.
样本从文件data.txt中读取,程序运行结果显示所有样本以及其类别,待分类样本所属的类别({1,18,11,11,0.5513196}属于"2"类),以及它的5个最近邻的类别和与它之间的距离。

代码片段和文件信息
#include
#include
#include
#define NATTRS 5 //number of attributes
#define MAXSZ 1700 //max size of training set
#define MAXVALUE 10000.0 //the biggest attribute‘s value is below 10000(int)
#define K 5
struct vector {
double attributes[NATTRS];
double classlabel;
};
struct item {
double distance;
double classlabel;
};
struct vector trSet[MAXSZ];//global variablethe training set
struct item knn[K];//global variablethe k-neareast-neighbour set
int curTSize = 0; //current size of the training set
int AddtoTSet(struct vector v)
{
if(curTSize>=MAXSZ) {
cout< return 0;
}
trSet[curTSize] = v;
curTSize++;
return 1;
}
double Distance(struct vector v1struct vector v2)
{
double d = 0.0;
double tem = 0.0;
for(int i = 0;i < NATTRS;i++)
tem += (v1.attributes[i]-v2.attributes[i])*(v1.attributes[i]-v2.attributes[i]);
d = sqrt(tem);
return d;
}
int max(struct item knn[]) //return the no. of the item which has biggest distance(
//should be replaced)
{
int maxNo = 0;
if(K > 1)
for(int i = 1;i < K;i++)
if(knn[i].distance>knn[maxNo].distance)
maxNo = i;
return maxNo;
}
double Classify(struct vector v)//decide which class label will be assigned to
//a given input vetor with the knn method
{
double dd = 0;
int maxn = 0;
int freq[K];
double mfreqC = 0;//the class label appears most frequently
int i;
for(i = 0;i < K;i++)
knn[i].distance = MAXVALUE;
for(i = 0;i < curTSize;i++)
{
dd = Distance(trSet[i]v);
maxn = max(knn);//for every new state of the training set should update maxn
if(dd < knn[maxn].distance) {
knn[maxn].distance = dd;
knn[maxn].classlabel = trSet[i].classlabel;
}
}
for(i = 0;i < K;i++)//freq[i] represents knn[i].classlabel appears how many times
freq[i] = 1;
for(i = 0;i < K;i++)
for(int j = 0;j < K;j++)
if((i!=j)&&(knn[i].classlabel == knn[j].classlabel))
freq[i]+=1;
for(i = 0;i < K;i++)
cout<<“freq:“<
int mfreq = 1;
mfreqC = knn[0].classlabel;
for(i = 0;i < K;i++)
if(freq[i] > mfreq) {
mfreq = freq[i];//mfreq represents the most frepuences
mfreqC = knn[i].classlabel; //mfreqNo is the item no. with the most frequent
//classlabel
}
return mfreqC;
}
void main()
{
double classlabel;
double c;
double n;
struct vector trExmp;
int i;
ifstream filein(“E:\\knn\\data.txt“);
if(filein.fail()){cout<<“Can‘t open data.txt“< while(!filein.eof())
{
filein>>c;
trExmp.classlabel = c;
cout<<“lable:“<
for(int i = 0;i < NATTRS;i++)
{
filein>>n;
trExmp.attributes[i] = n;
cout< }
cout< if(!AddtoTSet(trExmp))
break;
}
filein.cl
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 497 2006-10-26 02:13 K-最近邻分类\data.txt
文件 3264 2009-08-13 13:46 K-最近邻分类\knn\classify.cpp
文件 3425 2004-04-27 22:40 K-最近邻分类\knn\classify.dsp
文件 541 2004-04-27 22:41 K-最近邻分类\knn\classify.dsw
文件 50176 2009-08-13 13:49 K-最近邻分类\knn\classify.ncb
文件 48640 2009-08-13 13:49 K-最近邻分类\knn\classify.opt
文件 250 2009-08-13 13:49 K-最近邻分类\knn\classify.plg
文件 249937 2009-08-13 13:46 K-最近邻分类\knn\Debug\classify.exe
文件 310452 2009-08-13 13:46 K-最近邻分类\knn\Debug\classify.ilk
文件 14658 2009-08-13 13:46 K-最近邻分类\knn\Debug\classify.obj
文件 281688 2009-08-13 13:09 K-最近邻分类\knn\Debug\classify.pch
文件 574464 2009-08-13 13:46 K-最近邻分类\knn\Debug\classify.pdb
文件 50176 2009-08-13 13:49 K-最近邻分类\knn\Debug\vc60.idb
文件 61440 2009-08-13 13:46 K-最近邻分类\knn\Debug\vc60.pdb
文件 4350 2006-10-26 02:35 K-最近邻分类\knn\Myknn.dsp
文件 535 2004-04-25 23:27 K-最近邻分类\knn\Myknn.dsw
文件 50176 2006-10-26 11:28 K-最近邻分类\knn\Myknn.ncb
文件 48640 2006-10-26 11:28 K-最近邻分类\knn\Myknn.opt
文件 1377 2006-10-26 11:27 K-最近邻分类\knn\Myknn.plg
文件 17 2004-04-25 18:45 K-最近邻分类\knn\Output.dat
文件 339 2006-10-26 11:20 K-最近邻分类\KNN程序的说明.txt
文件 634 2006-10-26 11:22 K-最近邻分类\输出结果.txt
目录 0 2009-08-20 21:08 K-最近邻分类\knn\Debug
目录 0 2009-08-20 21:08 K-最近邻分类\knn
目录 0 2009-08-20 21:08 K-最近邻分类
----------- --------- ---------- ----- ----
1755676 25
- 上一篇:forms2.0.exe
- 下一篇:大数据培训视频教程.zip
相关资源
- bp神经网络源代码,可直接运行
- 仿知乎界面小程序源代码
- 贪吃蛇源代码.fla
- VisualStudioUninstaller vs卸载工具
- 组态王驱动开发包3.0.0.7(中文)
- 多窗口后台鼠标连点器
- dotnet 写字板 实验 源代码 不好请要不
- 使用选择性重传协议实现UDP可靠通信
- 图像二维小波变换的实现源代码
- 八三编码器设计 VHDL代码 简单,包附
- linux应用层的华容道游戏源代码
- VC 获得文件属性 获取文件的创建时
- 网上拍卖系统完整源代码
- CSMA/CD等动画演示加源代码
- silicon lab公司的收音IC SI47XX全套开发工
- 读者写者问题(读者优先,写者优先
- 合同管理系统的源代码(附数据库)
- 用VC 编写的仿QQ聊天室程序源代码
- STM32F103 串口程序(完整版)
- VPC3_DPV1源代码,Profibus
- PB做的托盘程序(最小化后在左下角显
- 外点法程序
- 外罚函数程序
- qt-电子点菜系统
- 透明加密源码及说明
- 排队机叫号 源代码
- 五子棋C 源代码
- CAD LISP24个源代码
- 二叉树基本操作源代码
- 推箱子及人工智能寻路C 源代码
评论
共有 条评论