SimpleClassify.cpp:
[cpp:firstline[1]]
view plain
copy
- /**
- * @author bianzhiqi 200732580077
- * @date 2009-10-28
- */
- #include <cstdlib>
- #include <iostream>
- #include <math.h>
- #include “destances.h”
- using namespace std;
- const int M=5;//模式集中向量的个数
- const int N=4;//向量中元素的个数
- double T;
- struct classNode{
- double x[N];
- classNode* next;
- };
- classNode classes[M];//存储聚类中心
- int n=0;//记录聚类中心的个数
- //对单个向量聚类操作
- void classify(double x[]){
- double min=T;
- int minNum=-1;
- for(int i=0;i<n;i++){
- double y[N];
- for(int j=0;j<N;j++)
- y[j]=classes[i].x[j];
- double dest=getEuclidean(x,y,N);
- if(dest<=T){
- //找到最小的欧氏距离,并记录下对应聚类中心所在的单元
- if(dest<=min){
- min=dest;
- minNum=i;
- }
- }
- }
- //minNum被改变,
- if(minNum!=-1){
- classNode* temp;
- for(int i=0;i<N;i++)
- temp->x[i]=x[i];
- temp->next=classes[minNum].next;
- classes[minNum].next=temp;
- return;
- }
- classNode cls;
- for(int i=0;i<N;i++)
- cls.x[i]=x[i];
- cls.next=NULL;
- classes[n]=cls;
- n++;
- return;
- }
- //对整个矢量集进行聚类操作
- void AsemClassify(double x[M][N]){
- for(int i=0;i<M;i++){
- double array[N];
- if(i==0){
- classNode cls;
- for(int i=0;i<N;i++)
- cls.x[i]=x[0][i];
- cls.next=NULL;
- classes[0]=cls;
- n++;
- }
- else{
- for(int j=0;j<N;j++)
- array[j]=x[i][j];
- classify(array);
- }
- }
- }
- //遍历链表,得到聚类结果
- void getResult(){
- for(int i=0;i<n;i++){
- cout<<“Class “<<i+1<<” : “<<endl;
- for(int j=0;j<N;j++)
- cout<<classes[i].x[j]<<” “;
- cout<<endl;
- classNode* temp;
- temp=classes[i].next;
- if(temp){
- for(int j=0;j<N;j++)
- cout<<temp->x[j]<<” “;
- cout<<endl;
- temp=temp->next;
- }
- }
- }
- int main(int argc, char *argv[]){
- double all[M][N];
- cout<<“请输入”<<M<<“*”<<N<<“的数组:”<<endl;
- for(int i=0;i<M;i++)
- for(int j=0;j<N;j++)
- cin>>all[i][j];
- AsemClassify(all);
- getResult();
- }
distances.cpp:
[cpp:firstline[1]]
view plain
copy
- /**
- * @author bianzhiqi 200732580077
- * @date 2009-10-15
- */
- #include <cstdlib>
- #include <iostream>
- #include <math.h>
- #include “Matrix.h”
- using namespace std;
- //欧氏距离
- double getEuclidean(double x[], double y[], int n)
- {
- double euclidean=0;
- for(int i=0;i<n;i++)
- {
- euclidean+=(x[i]-y[i])*(x[i]-y[i]);
- }
- euclidean=sqrt(euclidean);
- return euclidean;
- }
- //绝对值距离
- double getManhattan(double x[], double y[], int n)
- {
- double manhattan=0;
- for(int i=0;i<n;i++)
- {
- manhattan+=fabs(x[i]-y[i]);
- }
- return manhattan;
- }
- //切氏距离
- double getChebyshev(double x[], double y[], int n)
- {
- double chebyshev=0;
- for(int i=0;i<n;i++)
- {
- double temp;
- temp=fabs(x[i]-y[i]);
- if(temp>chebyshev) chebyshev=temp;
- }
- return chebyshev;
- }
- //明氏距离
- double getMinkowski(double x[], double y[], int n,int m)
- {
- double minkowski=0;
- for(int i=0;i<n;i++)
- {
- double temp=1;
- for(int j=0;j<m;j++)
- {
- temp=temp*fabs(x[i]-y[i]);
- }
- minkowski+=temp;
- }
- minkowski=pow(minkowski,1/m);
- return minkowski;
- }
- //Camberra距离
- double getCamberra(double x[], double y[], int n)
- {
- double camberra=0;
- for(int i=0;i<n;i++)
- {
- camberra+=(x[i]-y[i])/(x[i]+y[i]);
- }
- camberra=fabs(camberra);
- return camberra;
- }