for(intj=0;j
try{
data[i][j]=Double.parseDouble(colData[i][j]);
//System.out.print(data[i][j]+"");
}catch(Exceptione1){
JOptionPane.showMessageDialog(null,"数据输入有误!
请修正之后再导入。
");
return;
}
}
}
}
}
8.2<功能块2>
【功能名称】
导出文件
【功能描述】
将程序运行结果导出文件。
【业务规则和操作说明】
程序运行结束,用户点击系统界面上的“保存结果”按钮,直接生成元数据文件名与算法相应的文件(*.txt)。
【算法】
publicvoiddaooutIFrame(){
if(Text1.getText()==null||Text1.getText().equals("")){
JOptionPane.showMessageDialog(null,"请导入源文件");
return;
}
if(Text2.getText()==null||Text2.getText().equals("")){
JOptionPane.showMessageDialog(null,"请输入k值");
return;
}
if(Option1.isSelected()){
try{
Filefile1=newFile(file.getPath().substring(0,file.getPath().lastIndexOf("."))+"--means_result.txt");
daochu(file1);
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}catch(NullPointerExceptione){
Filefile1=newFile("F:
\\文档\\实习\\result-medoids_result.txt");
try{
daochu(file1);
}catch(IOExceptione1){
e1.printStackTrace();
}
}
}
else{
try{
Filefile1=newFile(file.getPath().substring(0,file.getPath().lastIndexOf("."))+"--medoids_result.txt");
daochu(file1);
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}catch(NullPointerExceptione){
Filefile1=newFile("F:
\\文档\\实习\\result--means_result.txt");
try{
daochu(file1);
}catch(IOExceptione1){
e1.printStackTrace();
}
}
}
}
8.3<功能块3>
【功能名称】
K-Means算法。
【功能描述】
将用户导入的文件数据用K-Meams算法运行处理。
。
【业务规则和操作说明】
用户选择系统界面上的“K-Meams”单选按钮,然后输入分组数,点击的“确定”按钮。
【算法】
publicvoiddeterIFrame(){
if(Text1.getText()==null||Text1.getText().equals("")){
JOptionPane.showMessageDialog(null,"请导入源文件");
return;
}
if(data==null){
JOptionPane.showMessageDialog(null,"请输入数据");
return;
}
if(Text2.getText()==null||Text2.getText().equals("")){
JOptionPane.showMessageDialog(null,"请输入k值");
return;
}
k=Integer.parseInt(Text2.getText());
if(Option1.isSelected()){
means=newrunK_means(data,k);
Text1.setText(means.stringBuffer());
}
if(Option2.isSelected()){
medoids=newrunK_medoids(data,k);
Text1.setText(medoids.stringBuffer());
}
}
8.4<功能块4>
【功能名称】
K-Medoids算法。
【功能描述】
将用户导入的文件数据用K-Medoids算法运行处理。
。
【业务规则和操作说明】
用户选择系统界面上的“K-Medoids”单选按钮,然后输入分组数,点击的“确定”按钮。
【算法】
与8.3算法一致
9.数据处理
9.1K-Means算法处理
1)选取数据的初始中心点的坐标(firstCentre())
2)计算所有点到中心点的距离,并生成距离矩阵(distance(double[][]centre,double[][]data))
3)标记处个点到哪个中心距离最小,得出分类向量(ouType())
4)遍历分类向量,得出每个类别的个数(process())
5)利用各类数据加和取平均,得出新的中心坐标(findMeansNcentre())
6)判断新旧中心坐标是否一致,若不一致新中心坐标取代就中心坐标(isequelMatrix(double[][]matrix1,double[][]matrix2)),继续2——6;若两者一致,则输出计算结果(printout())
9.1K-Medoids算法处理
1)选取数据的初始中心点的坐标(firstCentre())
2)计算所有点到中心点的距离,并生成距离矩阵(distance(double[][]centre,double[][]data))
3)标记处个点到哪个中心距离最小,得出分类向量(ouType())
4)遍历分类向量,得出每个类别的个数(process())
5)取出某类数据,得出各点之间的距离,并找出距离和的最小值,最小值所对应坐标即为新的中心坐标(findMedoidsNcentre())
6)判断新旧中心坐标是否一致,若不一致新中心坐标取代就中心坐标(isequelMatrix(double[][]matrix1,double[][]matrix2)),继续2——6;若两者一致,则输出计算结果(printout())
【算法】
◆K-means
publicrunK_means(double[][]dat,intkk){
data=dat;
k=kk;
row=data.length;
col=data[0].length;
centre=newdouble[k][col];
Ncentre=newdouble[k][col];
type=newint[row];
firstCentre();
do{
times++;
process();
findMeansNcentre();
}while(!
isequelMatrix(centre,Ncentre));
printout();
}
◆K-medoids
publicrunK_medoids(double[][]dat,intkk){
data=dat;
k=kk;
row=data.length;
col=data[0].length;
centre=newdouble[k][col];
Ncentre=newdouble[k][col];
firstCentre();
do{
times++;
process();
findMedoidsNcentre();
}while(!
isequelMatrix(centre,Ncentre));
printout();
}
9.3具体方法
◆firstCentre();
publicvoidfirstCentre(){
for(inti=0;ifor(intj=0;j
centre[i][j]=data[i][j];
}
}
◆ouTpye();
publicvoidouType(){
distance(centre,data);
type=newint[row];
for(intj=0;jdoublemin=dis[0][j];
for(inti=1;iif(dis[i][j]<=min){
min=dis[i][j];
type[j]=i;
}
}
}
◆Process();
publicvoidprocess(){
outType();
count=newint[k];
for(inti=0;iintm=type[i];
count[m]++;
}
}
◆findMeansNcentre();
publicvoidfindMeansNcentre(){
double[][]Nicentre=newdouble[k][col];
Ncentre=Nicentre;
for(inti=0;iintm=type[i];
for(intj=0;j
Ncentre[m][j]+=data[i][j]/count[m];
}
}
◆findMedoidsNcentre();
publicvoidfindMedoidsNcentre(){
int[]Ncount=newint[k];//zubiexiabiao
double[][]Ndata=newdouble[row][col];//zhuanhuanhoudejuzhen
for(inti=1;iNcount[i]=Ncount[i-1]+count[i-1];
}
for(inti=0;iintm=type[i];
intn=Ncount[m];
for(intj=0;j
Ndata[n][j]=data[i][j];
Ncount[m]++;
}
for(inti=0;iNcount[i]=Ncount[i]-count[i];
}
for(intm=0;mdouble[][]Nidata=newdouble[count[m]][col];
for(inti=0;ifor(intj=0;j
Nidata[i][j]=Ndata[i+Ncount[m]][j];
}
//Ncentre[m]=findNcentre();
distance(Nidata,Nidata);
double[]addis=newdouble[count[m]];
for(inti=0;ifor(intj=0;jaddis[i]+=dis[i][j];
}
doublemin=addis[0];
intorder=0;
for(inti=0;iif(addis[i]min=addis[i];
order=i;
}
Ncentre[m]=Nidata[order];
}
}
◆isequelMatirx();
publicbooleanisequelMatrix(double[][]matrix1,double[][]matrix2){
booleanresult=true;
if((matrix1.length==matrix2.length)&&(matrix1[0].length==matrix2[0].length)){
for(inti=0;ifor(intj=0;jif(matrix1[i][j]!
=matrix2[i][j])
result=false;
matrix1[i][j]=matrix2[i][j];
}
}
else{
result=false;
}
returnresult;
}
◆Printout();
publicvoidprintout(){
sb=newStringBuffer();
sb.append("寻找中心的次数为:
"+times).append("\n");
for(inti=0;iintorder=i+1;
sb.append("第"+order+"中心点坐标为:
").append("\n");
for(intj=0;j
sb.append(centre[i][j]+"");
}
sb.append("\n");
sb.append("对应的点为:
").append("\n");
for(intim=0;imif(type[im]==i){
sb.append("["+im+"]");
for(intjm=0;jm
sb.append(data[im][jm]+"");
}
sb.append("\n");
}
}
sb.append("\n");
}
sb.append("\n");
}
|
|
|
|
|
|