📄 clustering.cpp
字号:
#include<stdio.h>
#include <iostream>
#include<string>
#include<fstream>
#include <sstream>
using namespace std;
void DBSCAN();
void K_means();
void main()
{
int algorithm=1;
cout<<"Choose clustering algorithm:"<<endl;
cout<<"DBSCAN: 1"<<endl;
cout<<"K-means:2"<<endl;
cout<<"your choice is:";
cin>>algorithm;
cout<<algorithm<<endl;
if(algorithm==1)
DBSCAN();
else if(algorithm==2)
K_means();
}
void K_means()
{
ifstream infile;
string str;
//float weight=1.2;
//对象数目
int num_of_Objects;
//聚类数目
int num_of_Clusters;
//聚类中心
//float *x_cent;
//float *y_cent;
//float *z_cent;
float * x;
float * y;
float * z;
int * cluster_num;
//属性数量
int num_of_Attributes=0;
int flag_record_Attributes_num=0;
//实际使用属性数量
int use_num_Attributes=0;
//记录每个聚类中的对象数目
//int * objects_in_Cluster;
cout<<"begin Kmeans"<<endl;
infile.open("a.txt");
while(!infile.eof())
{
infile>>str;
/* if(str.compare("Attributes:")==0&&flag_record_Attributes_num!=1)
{
infile>>num_of_Attributes;
flag_record_Attributes_num=1;
int temp_Attributes=0;
string *Attributes=new string[num_of_Attributes];
while(temp_Attributes<num_of_Attributes)
{
infile>>str;
if(str.compare("Ignored:")==0)
use_num_Attributes=temp_Attributes;
else
{
Attributes[temp_Attributes]=str;
//cout<<Attributes[temp_Attributes]<<endl;
temp_Attributes++;
}
}
}*/
//读出对象数目
//else
if(str.compare("Instances:")==0)
{
infile>>num_of_Objects;
cout<<"num objects:"<<num_of_Objects<<endl;
}
//读出聚类数目
else if(str.compare("-N")==0)
{
infile>>num_of_Clusters;
/* x_cent=new float[num_of_Clusters];
y_cent=new float[num_of_Clusters];
z_cent=new float[num_of_Clusters];
objects_in_Cluster=new int[num_of_Clusters];*/
cout<<"num_of_Clusters:"<<num_of_Clusters<<endl;
}
/* else if(str.compare("Cluster")==0)
{
infile>>str;
if(str.compare("centroids:")==0)
{
for(int i=0;i<num_of_Clusters;i++)
{
infile>>str;
infile>>str;
infile>>str;
infile>>x_cent[i];
//x_cent[i]=x_cent[i]
infile>>y_cent[i];
infile>>z_cent[i];
cout<<x_cent[i]<<" "<<y_cent[i]<<" "<<z_cent[i]<<endl;
}
}
}*/
}
infile.close();
ifstream infile_1;
x=new float[num_of_Objects];
y=new float[num_of_Objects];
z=new float[num_of_Objects];
x[0]=1;
cluster_num=new int[num_of_Objects];
infile_1.open("a.arff");
int index=0;
//读出坐标和所属类的编号
while(!infile_1.eof())
{
infile_1>>str;
if(str.compare("@data")==0)
{
for(int i=0;i<num_of_Objects;i++)
{
infile_1>>index;
cout<<"index:"<<index<<endl;
infile_1>>x[i];
cout<<"x[0]:"<<x[i]<<endl;
infile_1>>y[i];
infile_1>>z[i];
infile_1>>str;
infile_1>>str;
infile_1>>str;
infile_1>>str;
infile_1>>str;
infile_1>>str;
infile_1>>str;
infile_1>>str;
infile_1>>str;
infile_1>>str;
infile_1>>str;
infile_1>>str;
infile_1>>str;
infile_1>>str;
char temp=str.at(7);
cluster_num[index]=atoi(&temp);
//cout<<clusterNum[index]<<endl;
}
}
}
infile_1.close();
ofstream outfile;
outfile.open("result_kmeans.m");
//输出“hold on”
str="hold on\n";
outfile<<str;
//画点的样式
string pattern[100]={"k.","r^","o","b+","r*","ks","x","+",">","<",
"k.","r^","go","b+","*","s","x","+",">","<",
"k.","r^","go","b+","*","s","x","+",">","<",
"k.","r^","go","b+","*","s","x","+",">","<",
"k.","r^","go","b+","*","s","x","+",">","<",
"k.","r^","go","b+","*","s","x","+",">","<",
"k.","r^","go","b+","*","s","x","+",">","<",
"k.","r^","go","b+","*","s","x","+",">","<",
"k.","r^","go","b+","*","s","x","+",">","<",
"k.","r^","go","b+","*","s","x","+",">","<"};
//输出各个点的坐标值
/*例如: x = [52 43 45];
y = [1 4 4];
z= [0 2 2];
x1=[62 51 54 46]
y1=[3 0 0 0]
z1=[3 0 0 0]
*/
for(int i=0;i<num_of_Objects;i++)
{
outfile<<"plot3("<<x[i]<<","<<y[i]<<","<<z[i]<<",'"<<pattern[cluster_num[i]]<<"')"<<endl;
}
str="hold off\nbox on\ngrid on\n";
outfile<<str;
//输出坐标轴定义
str="axis([30 90 0 6 0 6])\nxlabel('age')\nylabel('BIRADS-L')\n";
outfile<<str;
str="zlabel('Focus_Num')";
outfile<<str;
outfile.close();
}
void DBSCAN()
{
ifstream infile;
//属性数量
int num_of_Attributes=0;
int flag_record_Attributes_num=0;
//实际使用属性数量
int use_num_Attributes=0;
//对象数目
int num_of_Objects=0;
//DBSCAN算法参数
float Epsilon;
int Minpts;
//Cluster数目
int num_of_Clusters=0;
//噪声数目
int num_of_Noise=0;
//算法耗时
float time;
//坐标值
float * x;
float * y;
float * z;
//记录每个对象所属的聚类编号
int * cluster_num;
//记录每个聚类中的对象数目
int * objects_in_Cluster;
int flag_record_xyz=0;
cout<<"begin dbscan"<<endl;
infile.open("boca-focumNum.txt");
string str;
string *temp_Objects;
float weightx=1;
float weighty=10;
float weightz=10;
// int i=4000;
while(!infile.eof())
{
// i--;
infile>>str;
// cout<<str<<endl;
//读出属性名和属性数量
if(str.compare("Attributes:")==0&&flag_record_Attributes_num!=1)
{
infile>>num_of_Attributes;
flag_record_Attributes_num=1;
int temp_Attributes=0;
string *Attributes=new string[num_of_Attributes];
while(temp_Attributes<num_of_Attributes)
{
infile>>str;
if(str.compare("Ignored:")==0)
use_num_Attributes=temp_Attributes;
else
{
Attributes[temp_Attributes]=str;
cout<<Attributes[temp_Attributes]<<endl;
temp_Attributes++;
}
}
}
//得到对象数目
else if(str.compare("Clustered")==0)
{
infile>>str;
if(str.compare("DataObjects:")==0)
{
infile>>num_of_Objects;
temp_Objects=new string[num_of_Objects];
x=new float[num_of_Objects];
y=new float[num_of_Objects];
z=new float[num_of_Objects];
cluster_num=new int[num_of_Objects];
int j;
char temp[10];
for(j=0;j<num_of_Objects;j++)
{
itoa(j,temp,10);
temp_Objects[j].append(temp);
}
}
else if(str.compare("Instances")==0)
{
int index;
//读出每个聚类中的对象数目
for(int i=0;i<num_of_Clusters;i++)
{
infile>>index;
infile>>objects_in_Cluster[index];
cout<<"objects in clusters:"<<index<<" "<<objects_in_Cluster[index]<<endl;
infile>>str;
infile>>str;
}
}
}
//得到Epsilon值
else if(str.compare("Epsilon:")==0)
infile>>Epsilon;
else if(str.compare("minPoints:")==0)
infile>>Minpts;
else if(str.compare("Number")==0)
{
infile>>str;
if(str.compare("of")==0)
{
infile>>str;
if(str.compare("generated")==0)
{
infile>>str;
if(str.compare("clusters:")==0)
{
infile>>num_of_Clusters;
objects_in_Cluster=new int[num_of_Clusters];
}
}
}
}
else if(str.compare("time:")==0)
infile>>time;
//编号为10之前的对象解析
else if(num_of_Objects>0)
{
int j=0;
for(j=10;j<num_of_Objects;j++)
{
// cout<<"str:"<<"("+temp_Objects[j]+".)"<<endl;
if(j>=10&&str.compare("("+temp_Objects[j]+".)")==0)
{
infile>>str;
int index=-1;
while(true)
{
index=str.find_first_of(",",index+1);
if(index==str.npos)
break;
else
str.replace(index,1," ");
}
istringstream is(str);
is>>x[j]>>y[j]>>z[j];
cout<<x[j]<<" "<<y[j]<<" "<<z[j]<<endl;
x[j]=x[j]/weightx;
y[j]=y[j]/weighty;
z[j]=z[j]/weightz;
infile>>str;
if(str.compare("-->")==0)
{
infile>>str;
//噪声
if(str.compare("NOISE")==0)
{
cluster_num[j]=-1;
cout<<"noise"<<endl;
}
//聚类编号
else
{
cluster_num[j]=atoi(str.c_str());
cout<<"num_cluster:"<<cluster_num[j]<<endl;
}
}
// flag_record_xyz=1;
}
}
}
//编号为10之前的对象解析
if(num_of_Objects>0&&str.compare("(")==0)
{
infile>>str;
int temp;
if(str.compare(1,1,".")==0)
{
temp= atoi(str.replace(1,1,"").c_str());
cout<<temp<<endl;
infile>>str;
int index=-1;
while(true)
{
index=str.find_first_of(",",index+1);
if(index==str.npos)
break;
else
str.replace(index,1," ");
}
istringstream is(str);
is>>x[temp]>>y[temp]>>z[temp];
x[temp]=x[temp]/weightx;
y[temp]=y[temp]/weighty;
z[temp]=z[temp]/weightz;
cout<<x[temp]<<" "<<y[temp]<<" "<<z[temp]<<endl;
infile>>str;
if(str.compare("-->")==0)
{
infile>>str;
//噪声
if(str.compare("NOISE")==0)
{
cluster_num[temp]=-1;
cout<<"noise"<<endl;
}
//聚类编号
else
{
cluster_num[temp]=atoi(str.c_str());
cout<<"num_cluster:"<<cluster_num[temp]<<endl;
}
}
}
}
if(str.compare("Unclustered")==0)
{
infile>>str;
if(str.compare("instances")==0)
{
infile>>str;
infile>>num_of_Noise;
cout<<"num_of_noise:"<<num_of_Noise<<endl;
}
}
}
cout<<"num_of_Attributes:"<<num_of_Attributes<<endl;
cout<<use_num_Attributes<<endl;
cout<<"num_of_Objects:"<<num_of_Objects<<endl;
cout<<"Epsilon:"<<Epsilon<<endl;
cout<<"Minpts:"<<Minpts<<endl;
cout<<"num_of_Clusters:"<<num_of_Clusters<<endl;
cout<<"Time cost:"<<time<<endl;
infile.close();
ofstream outfile;
outfile.open("result.m");
float *draw_pixel_x=new float[num_of_Objects];
float *draw_pixel_y=new float[num_of_Objects];
float *draw_pixel_z=new float[num_of_Objects];
int i,j;
char temp[10];
//输出各个点的坐标值
/*例如: x = [52 43 45];
y = [1 4 4];
z= [0 2 2];
x1=[62 51 54 46]
y1=[3 0 0 0]
z1=[3 0 0 0]
*/
for(i=0;i<num_of_Clusters;i++)
{
int index=0;
for(j=0;j<num_of_Objects;j++)
{
if(cluster_num[j]==i)
{
draw_pixel_x[index]=x[j];
draw_pixel_y[index]=y[j];
draw_pixel_z[index]=z[j];
cout<<draw_pixel_x[index]<<endl;
index++;
}
}
//输出X坐标的值
str="x";
itoa(i,temp,10);
//使str的值为x0,x1...
str.append(temp);
str.append(" =[");
outfile<<str<<" ";
for(j=0;j<objects_in_Cluster[i];j++)
{
//float转换成string
sprintf(temp,"%f",draw_pixel_x[j]);
str=temp;
//X坐标上的最后一个元素
if(j==objects_in_Cluster[i]-1)
outfile<<str<<"];\n";
//其它元素
else
outfile<<str<<" ";
}
//输出Y坐标的值
str="y";
itoa(i,temp,10);
//使str的值为y0,y1...
str.append(temp);
str.append(" =[");
outfile<<str<<" ";
for(j=0;j<objects_in_Cluster[i];j++)
{
//float转换成string
sprintf(temp,"%f",draw_pixel_y[j]);
str=temp;
//Y坐标上的最后一个元素
if(j==objects_in_Cluster[i]-1)
outfile<<str<<"];\n";
//其它元素
else
outfile<<str<<" ";
}
//输出Z坐标的值
str="z";
itoa(i,temp,10);
//使str的值为z0,z1...
str.append(temp);
str.append(" =[");
outfile<<str<<" ";
for(j=0;j<objects_in_Cluster[i];j++)
{
//float转换成string
sprintf(temp,"%f",draw_pixel_z[j]);
str=temp;
//Z坐标上的最后一个元素
if(j==objects_in_Cluster[i]-1)
outfile<<str<<"];\n";
//其它元素
else
outfile<<str<<" ";
}
}
//输出“hold on”
str="hold on\n";
outfile<<str;
//画点的样式
string pattern[100]={"k.","r^","o","b+","r*","ks","x","+",">","<",
"k.","r^","go","b+","*","s","x","+",">","<",
"k.","r^","go","b+","*","s","x","+",">","<",
"k.","r^","go","b+","*","s","x","+",">","<",
"k.","r^","go","b+","*","s","x","+",">","<",
"k.","r^","go","b+","*","s","x","+",">","<",
"k.","r^","go","b+","*","s","x","+",">","<",
"k.","r^","go","b+","*","s","x","+",">","<",
"k.","r^","go","b+","*","s","x","+",">","<",
"k.","r^","go","b+","*","s","x","+",">","<"};
//画三维点
/*例如:
plot3(x0,y0,z0,'.')
plot3(x1,y1,z1,'^')
plot3(x2,y2,z2,'*')
plot3(x3,y3,z3,'.')
plot3(x4,y4,z4,'+')
plot3(x5,y5,z5,'*')
*/
for(i=0;i<num_of_Clusters;i++)
{
str="plot3(";
outfile<<str;
str="x";
itoa(i,temp,10);
str.append(temp);
str.append(",y");
str.append(temp);
str.append(",z");
str.append(temp);
str.append(",'");
str.append(pattern[i]);
str.append("')\n");
outfile<<str;
}
str="hold off\nbox on\ngrid on\n";
outfile<<str;
//输出坐标轴定义
str="axis([30 90 0 6 0 6])\nxlabel('age')\nylabel('BIRADS-L')\n";
outfile<<str;
str="zlabel('Focus_Num')";
outfile<<str;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -