KNN入门练习

来源:互联网 发布:软件收入百强 编辑:程序博客网 时间:2024/05/21 22:50

Knn算法简略入门

作为一名弱弱的大一新生,看了一个下午的IEEE论文,勉强看懂了论文的一丝皮毛,然而我并不会用矩阵处理,直接模拟矩阵处理时间复杂度过高且繁琐(MATLAB真的很重要),只好拿着思路直接模拟啦
(p.s.学好英语和线代真的很重要Orz)
贴几张论文的图这里写图片描述

这里写图片描述

基本步骤:

1.读入数据集2.K值处理    选出训练集D'(选用留出法处理数据集)    用训练集进行训练k从2~sqrt(n)    对训练集中的所有元素进行k临近判断,比较正确率    求精度比较得到一个k3.测试模型    用测试集测试比较正确率是否符合预期否则从新执行step2

这里写图片描述

代码展示

#include<cstdio>#include<cstdlib>#include<cmath>#include<iostream>#include<algorithm>#include<cstring>#define FOR(i,j,k) for(i=j;i<+k;++i)#define mal 0x7fffffff#define R(x) (rand() % x)using namespace std;struct ma{    int x,y,t,d;    int c;}; ma a[10000];ma b[10000];ma c[10000];int n,m,u;int sqr(int x){    return x*x;}void ca(int x,int y,ma a){    a.d=abs(x-a.x)+abs(y-a.y);}void init(){    freopen("1.txt","r",stdin);    scanf("%d",&n);    int i;    m=n*0.8;    FOR(i,1,n)       {            scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].c);            a[i].t=0;       }}void clset(){    int dis;    int i,j,k;    i=0;    while(i<m)        {            dis=R(n)+1;            if (!a[dis].t)                {                    a[dis].t++;                    i++;                }        }    j=0;    k=0;    FOR(i,1,n)      {        if (a[i].t) b[j++]=a[i];            else c[k++]=a[i];    }    u=k;} int comp(ma x,ma y){    return x.d<y.d;}double acr[10000]={0};int chk(){    int i,j,k;    int ans1;    int ans2;    int ar;    int lsar=-mal;    int ans;    int cj[10000];    FOR(k,2,sqrt(n))    {        ar=0;        FOR(i,1,m)            {                FOR(j,1,m)                {                    ca(b[j].x,b[j].y,b[i]);                }                sort(b+1,b+1+n,comp);                memset(cj,0,sizeof(c));                ans1=-mal;                FOR(j,1,k)                {                    cj[b[j].c]++;                    if (ans1<cj[b[j].c])                         {                            ans1=cj[b[j].c];                            ans2=b[j].c;                        }                }//zhao dao k ling jin                if (ans2==b[i].c) ar++;             }        if (ar>lsar) {                        lsar=ar;                        ans=k;                     }        }    return ans;}int test(int k){    int i,j,l;    double ar=0;    int ans1,ans2;    int cj[10000];    FOR(i,1,u)    {        FOR(j,1,m)            {                ca(c[j].x,c[j].y,b[i]);            }        sort(b+1,b+1+n,comp);        ans1=0;        FOR(j,1,k)            {                cj[b[j].c]++;                    if (ans1<cj[b[j].c])                         {                            ans1=cj[b[j].c];                            ans2=b[j].c;                        }                }             if  (ans2==c[i].c) ar++;   }   ar=ar/(double)u;   if (ar>0.8) return 1;else return 0;}int done(int x,int y,int k){    int i,j,l;    int ans1,ans2;    int cj[100000];    FOR(j,1,n)            {                ca(x,y,a[j]);            }        sort(a+1,a+1+n,comp);        ans1=0;        FOR(j,1,k)            {                cj[a[j].c]++;                    if (ans1<cj[a[j].c])                         {                            ans1=cj[a[j].c];                            ans2=a[j].c;                        }            }    return ans2;         }int main(){    init();    int k,x,y;    do    {        clset();        k=chk();    }while(test(k));    while(scanf("%d%d",&x,&y)!=EOF)    {        printf("%d",done(x,y,k));    }    return 0;}

昂,入门新生,如有错误恳请不吝赐教!

参考资料

Discriminant Adaptive Nearest Neighbor Classification

Trevor Hastie and Rolbert Tibshirani

IEEE TRANSACTIONS ON PAITERN ANALYSIS AND MACHINE INTELLIGENCE, VOL. 18, NO. 6, JUNE 1996

《机器学习》周志华

原创粉丝点击