資源簡(jiǎn)介
java程序,通過(guò)給定的源數(shù)據(jù),通過(guò)k-means實(shí)現(xiàn)聚類(lèi),有界面。

代碼片段和文件信息
package?Package;
import?java.io.*;
public?class?K_Means?{
int?Flags?=?0;
int[]?logo?=?new?int[100];?//?標(biāo)志位
double?center_A[]?=?new?double[2]?center_B[]?=?new?double[2]
center_C[]?=?new?double[2]?center_1[]?=?new?double[2]
center_2[]?=?new?double[2]?center_3[]?=?new?double[2];
double?train[][]?=?new?double[100][2];?//?將對(duì)象實(shí)體化
/***************************************************************************
?*?讀文件data0.txt中數(shù)據(jù),保存至train[][]二維數(shù)組中
?**************************************************************************/
public?void?readtxt()?throws?Exception?{
BufferedReader?br?=?new?BufferedReader(new?FileReader(“c:/data3.txt“));
String?s;
int?i?j;
i?=?0;
j?=?0;
while?(br.ready())//?判斷此流是否已準(zhǔn)備好被讀取
{
s?=?br.readLine();//?讀文件中一行
java.util.StringTokenizer?tokenizer?=?new?java.util.StringTokenizer(
s?“,“);
while?(tokenizer.hasMoreTokens())//?測(cè)試此?tokenizer
//?的字符串中是否還有更多的可用標(biāo)記
{
train[i][j]?=?Double.parseDouble(tokenizer.nextToken());//?將讀入的字符串轉(zhuǎn)化為雙精度型數(shù)據(jù)
j++;
if?(j?==?2)?{
j?=?0;
i++;
}
}
}
}
/***************************************************************************
?*?初始化中心點(diǎn)(任意)
?**************************************************************************/
public?void?initial()?{
for?(int?i?=?0;?i?3;?i++)
//?初始化簇(舊)中心,不妨以前三個(gè)為中心
for?(int?j?=?0;?j?2;?j++)?{
if?(i?==?0)?{
center_A[j]?=?train[i][j];
}?else?if?(i?==?1)?{
center_B[j]?=?train[i][j];
}?else?{
center_C[j]?=?train[i][j];
}
}
for?(int?j?=?0;?j?2;?j++)//?初始化簇(新)中心
{
center_1[j]?=?center_2[j]?=?center_3[j]?=?0;
}
for?(int?i?=?0;?i? logo[i]?=?0;//?初始化標(biāo)志位
}
}
/***************************************************************************
?*?比較差值,確定所屬類(lèi)
?**************************************************************************/
public?void?plus()//?求方差
{
for?(int?i?=?0;?i? double?a?b?c;
a?=?b?=?c?=?0;
for?(int?j?=?0;?j?2;?j++)?{
a?+=?Math.pow((train[i][j]?-?center_A[j])?2);
b?+=?Math.pow((train[i][j]?-?center_B[j])?2);
c?+=?Math.pow((train[i][j]?-?center_C[j])?2);
}
token(a?b?c?i);//?標(biāo)記logo[i]?,以logo=123表示所屬類(lèi)
}
}
/***************************************************************************
?*?更改標(biāo)志位(重新分配,看點(diǎn)屬于那個(gè)簇)
?**************************************************************************/
public?void?token(double?a?double?b?double?c?int?i)?{
double?temp?=?0;
if?(a?>=?b)?{
temp?=?b;
this.logo[i]?=?2;
}?else?{
temp?=?a;
this.logo[i]?=?1;
}
if?(temp?>=?c)?{
temp?=?c;
this.logo[i]?=?3;
}
}
/***************************************************************************
?*?每聚類(lèi)一次,求一次新的中心點(diǎn)
?**************************************************************************/
public?void?center()?{
int?num1?num2?num3;
num1?=?nu
?屬性????????????大小?????日期????時(shí)間???名稱(chēng)
-----------?---------??----------?-----??----
?????文件????????383??2010-05-03?19:04??K-means\.project
?????文件???????2088??2010-05-03?19:08??K-means\src\Package\Myfr
?????文件???????6330??2010-05-03?19:07??K-means\src\Package\K_Means.java
?????文件????????292??2009-05-27?20:28??K-means\src\data3.txt
?????文件???????4151??2010-05-03?19:07??K-means\bin\Package\K_Means.class
?????文件???????2345??2010-05-03?19:08??K-means\bin\Package\Myfr
?????文件????????292??2009-05-27?20:28??K-means\bin\data3.txt
?????文件????????629??2010-05-03?19:04??K-means\.settings\org.eclipse.jdt.core.prefs
?????文件????????299??2010-05-03?19:04??K-means\.classpath
?????目錄??????????0??2010-05-03?19:05??K-means\src\Package
?????目錄??????????0??2010-05-03?19:05??K-means\bin\Package
?????目錄??????????0??2010-05-03?19:04??K-means\src
?????目錄??????????0??2010-05-03?19:04??K-means\bin
?????目錄??????????0??2010-05-03?19:04??K-means\.settings
?????目錄??????????0??2010-05-03?19:04??K-means
-----------?---------??----------?-----??----
????????????????16809????????????????????15
評(píng)論
共有 條評(píng)論