首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 数据库开发 >

求分类算法C4.5的C#代码

2012-03-01 
求助:求分类算法C4.5的C#代码各位大哥帮帮忙啊!小弟最近在研究一个分类算法C4.5。希望得一用C#编写的算法作

求助:求分类算法C4.5的C#代码
各位大哥帮帮忙啊!小弟最近在研究一个分类算法C4.5。希望得一用C#编写的算法作为参考。

[解决办法]
#pragma package(smart_init) #include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> #include <math.h> #define FAILURE 0 #define TRUE 1 #define FALSE 0 #define MAXVECTDIM 200 #define MAXPATTERN 100 #define MAXCLUSTER 20 char *f2a(double x, int width){ char cbuf[100000]; char *cp; int i,k; int d,s; cp=fcvt(x,width,&d,&s); if (s) { strcpy(cbuf,"-"); } else { strcpy(cbuf," "); } /* endif */ if (d>0) { for (i=0; i<d; i++) { cbuf[i+1]=cp[i]; } /* endfor */ cbuf[d+1]=0; cp+=d; strcat(cbuf,"."); strcat(cbuf,cp); } else { if (d==0) { strcat(cbuf,"."); strcat(cbuf,cp); } else { k=-d; strcat(cbuf,"."); for (i=0; i<k; i++) { strcat(cbuf,"0"); } /* endfor */ strcat(cbuf,cp); } /* endif */ } /* endif */ cp=&cbuf[0]; return cp; } // ***** Defined structures & classes ***** struct aCluster { double Center[MAXVECTDIM]; int Member[MAXPATTERN]; //Index of Vectors belonging to this cluster int NumMembers; }; struct aVector { double Center[MAXVECTDIM]; int Size; }; class K_Mean { public: float Pattern[MAXPATTERN][MAXVECTDIM+1]; aCluster Cluster[MAXCLUSTER]; int NumPatterns; // Number of patterns int SizeVector; // Number of dimensions in vector int NumClusters; // Number of clusters void DistributeSamples(); // Step 2 of K-means algorithm int CalcNewClustCenters();// Step 3 of K-means algorithm double EucNorm(int, int); // Calc Euclidean norm vector int FindClosestCluster(int); //ret indx of clust closest to pattern //whose index is void InitClusters(); // Step 1 of K-means algorithm void RunKMeans(); // Overall control K-means process void ShowCenters(); }; void K_Mean::ShowCenters() { int i,j; for (i=0; i<NumClusters; i++) { Cluster[i].Member[0]=i; } /* endfor */ } void K_Mean::InitClusters(){ int i,j; for (i=0; i<NumClusters; i++) { Cluster[i].Member[0]=i; for (j=0; j<SizeVector; j++) { Cluster[i].Center[j]=Pattern[i][j]; } /* endfor */ } /* endfor */ } void K_Mean::RunKMeans(){ int converged; converged=FALSE; while (converged==FALSE) { DistributeSamples(); converged=CalcNewClustCenters(); ShowCenters(); } /* endwhile */ } double K_Mean::EucNorm(int p, int c){ // Calc Euclidean norm of vector difference double dist,x; // between pattern vector, p, and cluster int i; // center, c. char zout[100000]; char znum[50000]; strcpy(zout,"d=sqrt("); dist=0; for (i=0; i<SizeVector ;i++){ x=(Cluster[c].Center[i]-Pattern[p][i])*(Cluster[c].Center[i]-Pattern[p][i]); strcat(zout,f2a(x,4)); if (i==0) strcat(zout,"+"); dist += (Cluster[c].Center[i]-Pattern[p][i])*(Cluster[c].Center[i]-Pattern[p][i]); } /* endfor */ return dist; } int K_Mean::FindClosestCluster(int pat){ int i, ClustID; double MinDist, d; MinDist =9.9e+99; ClustID=-1; for (i=0; i<NumClusters; i++) { d=EucNorm(pat,i); if (d<MinDist) { MinDist=d; ClustID=i; } /* endif */ } /* endfor */ if (ClustID<0) { exit(0); } /* endif */ return ClustID; } void K_Mean::DistributeSamples(){ int i,pat,Clustid,MemberIndex; //Clear membership list for all current clusters for (i=0; i<NumClusters;i++){ Cluster[i].NumMembers=0; } for (pat=0; pat<NumPatterns; pat++) { //Find cluster center to which the pattern is closest Clustid= FindClosestCluster(pat); //post this pattern to the cluster MemberIndex=Cluster[Clustid].NumMembers; Cluster[Clustid].Member[MemberIndex]=pat; Cluster[Clustid].NumMembers++; } /* endfor */ } int K_Mean::CalcNewClustCenters(){ int ConvFlag,VectID,i,j,k; double tmp[MAXVECTDIM]; char xs[100000]; char ys[100000]; char nc1[40000]; char nc2[40000]; char *pnc1; char *pnc2; char *fpv; /*pnc1=&nc1[0]; pnc2=&nc2[0]; */ ConvFlag=TRUE; //----------------------------------- for (i=0; i<NumClusters; i++) { //for each cluster // pnc1=itoa(Cluster[i].NumMembers,nc1,10); // pnc2=itoa(i,nc2,10); strcpy(xs,"Cluster Center"); strcat(xs,nc2); strcat(xs,"(1/"); strcpy(ys,"(1/"); strcat(xs,nc1); strcat(ys,nc1); strcat(xs,")("); strcat(ys,")("); for (j=0; j<SizeVector; j++) { // clear workspace tmp[j]=0.0; } /* endfor */ for (j=0; j<Cluster[i].NumMembers; j++) { //traverse member vectors VectID=Cluster[i].Member[j]; for (k=0; k<SizeVector; k++) { //traverse elements of vector tmp[k] += Pattern[VectID][k]; // add (member) pattern elmnt into temp if (k==0) { strcat(xs,f2a(Pattern[VectID][k],3)); } else { strcat(ys,f2a(Pattern[VectID][k],3)); } /* endif */ } /* endfor */ if(j<Cluster[i].NumMembers-1){ strcat(xs,"+"); strcat(ys,"+"); } else { strcat(xs,")"); strcat(ys,")"); } } /* endfor */ for (k=0; k<SizeVector; k++) { //traverse elements of vector tmp[k]=tmp[k]/Cluster[i].NumMembers; if (tmp[k] != Cluster[i].Center[k]) ConvFlag=FALSE; Cluster[i].Center[k]=tmp[k]; } /* endfor */ } /* endfor */ return ConvFlag; }

热点排行