• 大小: 290KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-19
  • 语言: 其他
  • 标签: KNN  C++  源代码  

资源简介

本程序中,训练样本集含有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


评论

共有 条评论