?? readfile.java
字號:
/*
* ReadFile.java
*
* Created on 2007年5月28日, 下午10:59
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package cindy;
import java.io.*;
import java.text.*;
import java.util.*;
/**
*
* @author njchenyi
*/
public class ReadFile {
private DecimalFormat df=new DecimalFormat("#.00");
private BufferedReader infile = null;
String filename ="glass7C數(shù)據(jù).txt";
private String inLine;
private Cluster cluster;
private int index=0;
private Vector all = new Vector(); //存放分類
private Vector temp = new Vector(); //存放所有的樣本
private int eachNo = 0;
//初始分類的個數(shù)
private static int CLUSTERNO=6;
/** Creates a new instance of FileReader */
public ReadFile() {
try{
infile = new BufferedReader(new FileReader(filename));
while((inLine=infile.readLine())!=null) {
index++;
//System.out.println(inLine);
//數(shù)據(jù)文件名
//Sample i=new iris3CSample(inLine,index);
Sample i = new glass7C(inLine, index);
temp.add(i); //將樣本存放進(jìn)一個堆棧
//((Cluster)all.elementAt(0)).addSample(i);
//System.out.println(df.format(((Cluster)all.elementAt(0)).getWeightPoint()[0]));
//System.out.println(((Cluster)all.elementAt(0)).getDistanceSum(i));
}
step();
}
catch(FileNotFoundException ex){
System.out.println("can not find file "+ filename);
} catch(IOException ex){
System.out.println(ex.getMessage());
} finally{
try {
if(infile!=null)
infile.close();
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
private void step(){
dividCluster();
calculateP();
//calculateJ();
}
private void dividCluster(){
for(int i =0; i< CLUSTERNO; i++){
//產(chǎn)生相應(yīng)個分類,存放進(jìn)堆棧
cluster = new Cluster();
all.add(cluster);
for( int j=(temp.size()/CLUSTERNO)*(i); j<(temp.size()/CLUSTERNO)*(i+1);j++){
//將堆棧中的樣本平均存放進(jìn)分類
((Cluster)all.elementAt(i)).addSample((Sample)temp.elementAt(j));
//System.out.println(df.format(((Cluster)all.elementAt(i)).getWeightPoint()[0]));
//System.out.println(((Cluster)all.elementAt(i)).getDistanceSum((Sample)temp.elementAt(j)));
}
}
//System.out.println(df.format(((Cluster)all.elementAt(0)).getWeightPoint()[0]));
//System.out.println(df.format(((Cluster)all.elementAt(1)).getWeightPoint()[0]));
//System.out.println(df.format(((Cluster)all.elementAt(2)).getWeightPoint()[0]));
}
public void calculateP(){
double p = 0.0;
double tmpP;
int tmpCluster;
int tmpSample;
int currentCluster;
for(int i = 0; i< temp.size(); i++){
//System.out.println(i);
tmpP =100.0;
tmpCluster=0;
tmpSample=0;
currentCluster=0;
for(int j = 0; j<all.size();j++){
int k =((Cluster)all.elementAt(j)).getSampleNo();
if(k!=1){
if(((Cluster)all.elementAt(j)).isInCluster((Sample)temp.elementAt(i))){
//System.out.println(i + " "+j);
currentCluster = j;
p = (k/(k-1))*((Cluster)all.elementAt(j)).getDistanceSum((Sample)temp.elementAt(i));
//System.out.println(p);
if(tmpP>p){
tmpP=p;
tmpCluster = j;
tmpSample = i;
}
} else{
p = ((double)k/(k+1))*(((Cluster)all.elementAt(j)).getDistanceSum((Sample)temp.elementAt(i)));
//System.out.println(p);
if(tmpP>p){
tmpP=p;
tmpCluster = j;
tmpSample = i;
}
}
}
}
//System.out.println(tmpP+" "+tmpCluster+" "+tmpSample);
changeSample(currentCluster, tmpCluster, tmpSample);
}
}
private void changeSample(int fromcluster, int tocluster, int sample){
//System.out.println(fromcluster+" "+tocluster+" "+sample);
((Cluster)all.elementAt(fromcluster)).removeSample((Sample)temp.elementAt(sample));
((Cluster)all.elementAt(tocluster)).addSample((Sample)temp.elementAt(sample));
//System.out.println(((Cluster)all.elementAt(fromcluster)).getSampleNo());
//System.out.println(((Cluster)all.elementAt(tocluster)).getSampleNo());
}
public double calculateJ(){
double Je=0.0;
for(int i = 0; i<all.size();i++){
Je=Je+((Cluster)all.elementAt(i)).getClusterDistanceSum();
//System.out.println(Je);
}
//System.out.println(Je);
return Je;
}
public Vector getClusterVector(){
return all;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -