K.zip

  • PUDN用户
    了解作者
  • Visual C++
    开发工具
  • 2KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 3
    下载次数
  • 2012-05-08 09:49
    上传日期
近邻法的基本思想是在测试样本x的k个近邻中,按出现的样本类别来作为x的类别,即先对x的k个近邻一一找出它们的类别,然后对x类别进行判别,即在N个训练样本中,找出x的k个近邻。
K.zip
  • K.txt
    3.8KB
内容介绍
#include <iostream.h> #include <math.h> #include <fstream.h> #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 variable,the training set struct item knn[K];//global variable,the k-neareast-neighbour set int curTSize = 0; //current size of the training set int AddtoTSet(struct vector v) { if(curTSize>=MAXSZ) { cout<<endl<<"The training set has "<<MAXSZ<<" examples!"<<endl<<endl; return 0; } trSet[curTSize] = v; curTSize++; return 1; } double Distance(struct vector v1,struct 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:"<<freq[i]<<endl; 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"<<endl; return;} while(!filein.eof()) { filein>>c; trExmp.classlabel = c; cout<<"lable:"<<trExmp.classlabel<<"| "; for(int i = 0;i < NATTRS;i++) { filein>>n; trExmp.attributes[i] = n; cout<<trExmp.attributes[i]<<" "; } cout<<endl; if(!AddtoTSet(trExmp)) break; } filein.close(); struct vector testv={{1,18,11,11,0.5513196},17}; classlabel = Classify(testv); cout<<"The classlable of the testv is: "; cout<<classlabel<<endl; for(i = 0;i < K;i++) cout<<knn[i].distance<<"\t"<<knn[i].classlabel<<endl; //cout<<max(knn); } 4)输出结果: 0 1 1 1 1 1 0 2 2 2 2 2 0 3 3 3 3 3 1 4 4 4 4 4 1 5 5 5 5 5 1 6 6 6 6 6 2 7 7 7 7 7 2 8 8 8 8 8 2 9 9 9 9 9 3 10 10 10 10 10 3 11 11 11 11 11 3 12 12 12 12 12 4 13 13 13 13 13 4 14 14 14 14 14 4 15 15 15 15 15 6 16 16 16 16 16 6 17 17 17 17 17 6 18 18 18 18 18 7 19 19 19 19 19 7 20 20 20 20 20 7 21 21 21 21 21 8 22 22 22 22 22 8 23 23 23 23 23 8 24 24 24 24 24 9 25 25 25 25 25 9 26 26 26 26 26 9 27 27 27 27 27 10 28 28 28 28 28 10 29 29 29 29 29 10 30 30 30 30 30 10 30 30 30 30 30 The classlable of the testv is: 15.7698 1 15.185 2 14.9159 2 14.9793 2 15.3713 3 Press any key to continue
评论
    相关推荐
    • k近邻.zip
      K最近邻(k-Nearest Neighbor,KNN)分类算法,是一个理论上比较成熟的方法,也是最简单的机器学习算法之一。
    • k近邻法.zip
      knn方法对数据进行分类,内含代码和数据集
    • K近邻算法.rar
      多个模式识别算法的matlab代码,包括k近邻、二叉决策树、感知器、fisher线性判别等.
    • K近邻(KNN).zip
      利用python实现K临近算法预测信用卡违约情况,使用的是海豚大数据大数据分析赛的数据
    • k近邻算法.zip
      k近邻算法(knn)算法模拟 K-nearest neighbor algorithm (knn) simulation
    • KNN.zip K近邻算法
      k近邻算法
    • k-近邻算法.rar
      使用k-近邻算法来进行基础的电影类别分类
    • k近邻算法.zip
      我自己在网上找的一些关于knn算法的代码。大家可以看一下,可能存在一些问题。请大家指出改正
    • 1 K近邻.zip
      k近邻算法详解,修改相应参数便可直接调用
    • k-近邻算法.zip
      辅助教学,帮助机器学习爱好者学习K近邻算法的实现