//使用最大最小距离算法
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class Cluster
{
public void readTxtToArray(float a[][]) throws IOException
{
File f = new File("iris.txt");
InputStream input = new FileInputStream(f);
BufferedReader b = new BufferedReader(new InputStreamReader(input));
StringBuffer buffer = new StringBuffer();
String value = b.readLine();
while(value != null)
{
buffer.append(" "+value);
value = b.readLine();
}
String[] temp = buffer.toString().replaceFirst(" ","").split("[url=file:s]//s[/url]+");
float[] number = new float[temp.length];
for(int i=0;i<temp.length;i++)
{
try
{
number[i] = Float.parseFloat(temp[i]);
//System.out.print(number[i]+" ");
}
catch(Exception e)
{
System.out.println("文件中存在非数字字符!");
}
}
int x=0;
for(int j=0;j<150;j++)
for(int k=0;k<4;k++)
{
a[j][k]=number[x];
x++;
}
}
public int seccenter(float a[][]) /*寻找第二个聚类中心*/
{
double max=0.0;
double dis;
int index=0;
for(int k=0;k<150;k++)
{
dis=(a[k][0]-a[0][0])*(a[k][0]-a[0][0])+(a[k][1]-a[0][1])*(a[k][1]-a[0][1])+(a[k][2]-a[0][2])*(a[k][2]-a[0][2])+(a[k][3]-a[0][3])*(a[k][3]-a[0][3]);
if(dis>max)
{
max=dis;
index=k;
}
}
return index;
}
public int thicenter(float a[][],int c) /*寻找第三个聚类中心*/
{
double max=0.0;
double dis;
double dis1,dis2;
int index=0;
for(int k=0;k<150;k++)
{
dis1=(a[k][0]-a[0][0])*(a[k][0]-a[0][0])+(a[k][1]-a[0][1])*(a[k][1]-a[0][1])+(a[k][2]-a[0][2])*(a[k][2]-a[0][2])+(a[k][3]-a[0][3])*(a[k][3]-a[0][3]);
dis2=(a[k][0]-a[c][0])*(a[k][0]-a[c][0])+(a[k][1]-a[c][1])*(a[k][1]-a[c][1])+(a[k][2]-a[c][2])*(a[k][2]-a[c][2])+(a[k][3]-a[c][3])*(a[k][3]-a[c][3]);
if(dis1<dis2)
dis=dis1;
else
dis=dis2;
if(dis>max)
{
max=dis;
index=k;
}
}
return index;
}
public void julei(float a[][],int c1,int c2,int c3) /*把样本聚类*/
{
double dis1,dis2,dis3;
for(int k=0;k<150;k++)
{
dis1=(a[k][0]-a[c1][0])*(a[k][0]-a[c1][0])+(a[k][1]-a[c1][1])*(a[k][1]-a[c1][1])+(a[k][2]-a[c1][2])*(a[k][2]-a[c1][2])+(a[k][3]-a[c1][3])*(a[k][3]-a[c1][3]);
dis2=(a[k][0]-a[c2][0])*(a[k][0]-a[c2][0])+(a[k][1]-a[c2][1])*(a[k][1]-a[c2][1])+(a[k][2]-a[c2][2])*(a[k][2]-a[c2][2])+(a[k][3]-a[c2][3])*(a[k][3]-a[c2][3]);
dis3=(a[k][0]-a[c3][0])*(a[k][0]-a[c3][0])+(a[k][1]-a[c3][1])*(a[k][1]-a[c3][1])+(a[k][2]-a[c3][2])*(a[k][2]-a[c3][2])+(a[k][3]-a[c3][3])*(a[k][3]-a[c3][3]);
if(dis1<=dis2)
{
if(dis1<=dis3)
System.out.printf("The line %d belongs to category 1./n",k);
else
System.out.printf("The line %d belongs to category 3./n",k);
}
else
{
if(dis2<=dis3)
System.out.printf("The line %d belongs to category 2./n",k);
else
System.out.printf("The line %d belongs to category 3./n",k);
}
}
}
}
//主类
public class ClusterTest {
public static void main(String[] args)
{
Cluster myCluster=new Cluster();
float iris[][];
iris=new float[150][4];
try
{
myCluster.readTxtToArray(iris);
}
catch(Exception e)
{
System.out.println();
}
for(int j=0;j<150;j++)
{
System.out.println(j);
for(int k=0;k<4;k++)
{
System.out.print(iris[j][k]+" ");
}
System.out.println();
}
//a={{4.8,3.0,1.4,0.1},{4.3,3.0,1.1,0.1},{7.0,3.2,4.7,1.4},{6.4,3.2,4.5,1.5},{6.3,3.3,6.0,2.5},{5.8,2.7,5.1,1.9},{7.1,3.0,5.9,2.1}};
int cen1,cen2,cen3;
cen1=0;
cen2=myCluster.seccenter(iris);
cen3=myCluster.thicenter(iris,cen2);
myCluster.julei(iris,cen1,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1.8
6.1 3.0 4.9 1.8
6.4 2.8 5.6 2.1
7.2 3.0 5.8 1.6
7.4 2.8 6.1 1.9
7.9 3.8 6.4 2.0
6.4 2.8 5.6 2.2
6.3 2.8 5.1 1.5
6.1 2.6 5.6 1.4
7.7 3.0 6.1 2.3
6.3 3.4 5.6 2.4
6.4 3.1 5.5 1.8
6.0 3.0 4.8 1.8
6.9 3.1 5.4 2.1
6.7 3.1 5.6 2.4
6.9 3.1 5.1 2.3
5.8 2.7 5.1 1.9
6.8 3.2 5.9 2.3
6.7 3.3 5.7 2.5
6.7 3.0 5.2 2.3
6.3 2.5 5.0 1.9
6.5 3.0 5.2 2.0
6.2 3.4 5.4 2.3
5.9 3.0 5.1 1.8
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class Cluster
{
public void readTxtToArray(float a[][]) throws IOException
{
File f = new File("iris.txt");
InputStream input = new FileInputStream(f);
BufferedReader b = new BufferedReader(new InputStreamReader(input));
StringBuffer buffer = new StringBuffer();
String value = b.readLine();
while(value != null)
{
buffer.append(" "+value);
value = b.readLine();
}
String[] temp = buffer.toString().replaceFirst(" ","").split("[url=file:s]//s[/url]+");
float[] number = new float[temp.length];
for(int i=0;i<temp.length;i++)
{
try
{
number[i] = Float.parseFloat(temp[i]);
//System.out.print(number[i]+" ");
}
catch(Exception e)
{
System.out.println("文件中存在非数字字符!");
}
}
int x=0;
for(int j=0;j<150;j++)
for(int k=0;k<4;k++)
{
a[j][k]=number[x];
x++;
}
}
public int seccenter(float a[][]) /*寻找第二个聚类中心*/
{
double max=0.0;
double dis;
int index=0;
for(int k=0;k<150;k++)
{
dis=(a[k][0]-a[0][0])*(a[k][0]-a[0][0])+(a[k][1]-a[0][1])*(a[k][1]-a[0][1])+(a[k][2]-a[0][2])*(a[k][2]-a[0][2])+(a[k][3]-a[0][3])*(a[k][3]-a[0][3]);
if(dis>max)
{
max=dis;
index=k;
}
}
return index;
}
public int thicenter(float a[][],int c) /*寻找第三个聚类中心*/
{
double max=0.0;
double dis;
double dis1,dis2;
int index=0;
for(int k=0;k<150;k++)
{
dis1=(a[k][0]-a[0][0])*(a[k][0]-a[0][0])+(a[k][1]-a[0][1])*(a[k][1]-a[0][1])+(a[k][2]-a[0][2])*(a[k][2]-a[0][2])+(a[k][3]-a[0][3])*(a[k][3]-a[0][3]);
dis2=(a[k][0]-a[c][0])*(a[k][0]-a[c][0])+(a[k][1]-a[c][1])*(a[k][1]-a[c][1])+(a[k][2]-a[c][2])*(a[k][2]-a[c][2])+(a[k][3]-a[c][3])*(a[k][3]-a[c][3]);
if(dis1<dis2)
dis=dis1;
else
dis=dis2;
if(dis>max)
{
max=dis;
index=k;
}
}
return index;
}
public void julei(float a[][],int c1,int c2,int c3) /*把样本聚类*/
{
double dis1,dis2,dis3;
for(int k=0;k<150;k++)
{
dis1=(a[k][0]-a[c1][0])*(a[k][0]-a[c1][0])+(a[k][1]-a[c1][1])*(a[k][1]-a[c1][1])+(a[k][2]-a[c1][2])*(a[k][2]-a[c1][2])+(a[k][3]-a[c1][3])*(a[k][3]-a[c1][3]);
dis2=(a[k][0]-a[c2][0])*(a[k][0]-a[c2][0])+(a[k][1]-a[c2][1])*(a[k][1]-a[c2][1])+(a[k][2]-a[c2][2])*(a[k][2]-a[c2][2])+(a[k][3]-a[c2][3])*(a[k][3]-a[c2][3]);
dis3=(a[k][0]-a[c3][0])*(a[k][0]-a[c3][0])+(a[k][1]-a[c3][1])*(a[k][1]-a[c3][1])+(a[k][2]-a[c3][2])*(a[k][2]-a[c3][2])+(a[k][3]-a[c3][3])*(a[k][3]-a[c3][3]);
if(dis1<=dis2)
{
if(dis1<=dis3)
System.out.printf("The line %d belongs to category 1./n",k);
else
System.out.printf("The line %d belongs to category 3./n",k);
}
else
{
if(dis2<=dis3)
System.out.printf("The line %d belongs to category 2./n",k);
else
System.out.printf("The line %d belongs to category 3./n",k);
}
}
}
}
//主类
public class ClusterTest {
public static void main(String[] args)
{
Cluster myCluster=new Cluster();
float iris[][];
iris=new float[150][4];
try
{
myCluster.readTxtToArray(iris);
}
catch(Exception e)
{
System.out.println();
}
for(int j=0;j<150;j++)
{
System.out.println(j);
for(int k=0;k<4;k++)
{
System.out.print(iris[j][k]+" ");
}
System.out.println();
}
//a={{4.8,3.0,1.4,0.1},{4.3,3.0,1.1,0.1},{7.0,3.2,4.7,1.4},{6.4,3.2,4.5,1.5},{6.3,3.3,6.0,2.5},{5.8,2.7,5.1,1.9},{7.1,3.0,5.9,2.1}};
int cen1,cen2,cen3;
cen1=0;
cen2=myCluster.seccenter(iris);
cen3=myCluster.thicenter(iris,cen2);
myCluster.julei(iris,cen1,cen2,cen3);
}
}
附:iris数据
5.1 3.5 1.4 0.2
4.9 3.0 1.4 0.2
4.7 3.2 1.3 0.2
4.6 3.1 1.5 0.2
5.0 3.6 1.4 0.2
5.4 3.9 1.7 0.4
4.6 3.4 1.4 0.3
5.0 3.4 1.5 0.2
4.4 2.9 1.4 0.2
4.9 3.1 1.5 0.1
5.4 3.7 1.5 0.2
4.8 3.4 1.6 0.2
4.8 3.0 1.4 0.1
4.3 3.0 1.1 0.1
5.8 4.0 1.2 0.2
5.7 4.4 1.5 0.4
5.4 3.9 1.3 0.4
5.1 3.5 1.4 0.3
5.7 3.8 1.7 0.3
5.1 3.8 1.5 0.3
5.4 3.4 1.7 0.2
5.1 3.7 1.5 0.4
4.6 3.6 1.0 0.2
5.1 3.3 1.7 0.5
4.8 3.4 1.9 0.2
5.0 3.0 1.6 0.2
5.0 3.4 1.6 0.4
5.2 3.5 1.5 0.2
5.2 3.4 1.4 0.2
4.7 3.2 1.6 0.2
4.8 3.1 1.6 0.2
5.4 3.4 1.5 0.4
5.2 4.1 1.5 0.1
5.5 4.2 1.4 0.2
4.9 3.1 1.5 0.2
5.0 3.2 1.2 0.2
5.5 3.5 1.3 0.2
4.9 3.6 1.4 0.1
4.4 3.0 1.3 0.2
5.1 3.4 1.5 0.2
5.0 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