KMeans.java
/**************************************************************************************************************************
(c) Copyright: Kevin Chen , No.79912001, 2010~2012. All rights reserved.
Author: : Kevin Chen
Desc :
Change History : -
Class used in :
Change History : Level Changed By Date Change description
-------------------------------------------------------------------------------------------------------
(Initial) Kevin Chen 20100112 (Initial)
'**************************************************************************************************************************/
package e9912001;
public class KMeans {
double[][] m_TestData;
int m_NumOfClusters;
int m_TestCases;
int m_TestVariables;
double[][] m_Centroid;
int[][] m_aryResultClusters;
int m_MAXLOOP = 100;
public int getM_NumOfClusters() {
return m_NumOfClusters;
}
public void setM_NumOfClusters(int mNumOfClusters) {
m_NumOfClusters = mNumOfClusters;
m_ClusterNo = new int[m_NumOfClusters];
}
public double[][] getM_TestData() {
return m_TestData;
}
public int getM_TestCases() {
return m_TestCases;
}
public int getM_TestVariables() {
return m_TestVariables;
}
public int[] getM_ClusterNo() {
return m_ClusterNo;
}
public double[][] getM_Centroid() {
return m_Centroid;
}
int[] m_ClusterNo ;
public KMeans(double[][] input_data){
m_TestData = input_data;
m_TestCases = m_TestData.length;
m_TestVariables = m_TestData[0].length;
//m_NumOfGroups = (int)Math.Sqrt(m_TestCases) + 1;
m_NumOfClusters = 3;
m_ClusterNo = new int[m_NumOfClusters];
}
public double[] getGrandCentroid(){
double[] ret = new double[this.m_TestVariables];
int sum;
for(int i=0; i < this.m_TestData.length;i++){
sum =0;
for(int j=0; j < this.m_TestData[i].length;j++){
sum += this.m_TestData[i][j];
}
ret[i] = sum / this.m_TestCases;
}
return ret;
}
private int quickSortPartitions(double[][] list,int low,int high) {
double prvotkey = list[low][1];
double t0, t1;
int i, j;
i = low; j = high+1;
while(true) {
do ++i; while( list[i][1] <= prvotkey && i <= high );
do --j; while( list[j][1] > prvotkey );
if( i >= j ) break;
t0 = list[i][0]; list[i][0] = list[j][0]; list[j][0] = t0;
t1 = list[i][1]; list[i][1] = list[j][1]; list[j][1] = t1;
}
t0 = list[low][0]; list[low][0] = list[j][0]; list[j][0] = t0;
t1 = list[low][1]; list[low][1] = list[j][1]; list[j][1] = t1;
return j;
}
private void quicksort(double[][] list,int low,int high){
int prvotloc;
if(low < high){
prvotloc = quickSortPartitions(list, low, high); //將第一次排序的结果作為樞軸
quicksort(list, low, prvotloc - 1); //遞迴調用排序 由low 到 prvotloc - 1
quicksort(list, prvotloc + 1, high); //遞迴調用排序 由 prvotloc + 1到 high
}
}
/* public int[] getStartingPointArrayIndex(){
double[][] testDataDistance = new double[m_TestCases][2];
double Distance2Zero = 0.0; //點到群中心距離(群中心也是四維的點)
for (int m = 0; m < this.m_TestCases;m++) {
Distance2Zero = 0.0;
for (int n = 0; n < m_TestVariables; n++) {//4個變數
Distance2Zero += Math.pow((m_TestData[m][n] - 0), 2);
}
testDataDistance[m][0] = m;
testDataDistance[m][1] = Distance2Zero;
}
for(int i= 0; i < testDataDistance.length;i++){
System.out.format("%d \n",(int) testDataDistance[i]);
}
System.out.println("======================================");
quicksort(testDataDistance, 0, testDataDistance.length-1);
for(int i= 0; i < testDataDistance.length;i++){
System.out.format("%d=%f \n",(int)testDataDistance[i][0], testDataDistance[i][1]);
}
int x1 = Math.round(((m_TestCases-1-0)/(1-0))/3);
int x2 = Math.round((((m_TestCases-1-0)/(1-0))*2)/3);
double[] avgDist = new double[this.m_NumOfClusters];
int[] retPos = new int[this.m_NumOfClusters];
double sum = 0.0;
double tmpAvg = 0.0;
for (int i = 0; i < x1+1 ; i++)
sum += testDataDistance[i][1];
avgDist[0]= sum/(x2+1);
double min = Double.MAX_VALUE;
int pos =0;
double tmp;
for (int i = 0; i < x1+1 ; i++){
tmp = Math.abs( avgDist[0] - testDataDistance[i][1] );
if (tmp < min) { min = tmp; pos = i;}
}
retPos[0] = pos;
//*************************************
sum = 0;
for (int i = x1+1; i < x2+1 ; i++)
sum += testDataDistance[i][1];
avgDist[1] = sum/(x2+1);
min = Double.MAX_VALUE;
pos =0;
for (int i = 0; i < x1+1 ; i++){
tmp = Math.abs( avgDist[1] - testDataDistance[i][1] );
if (tmp < min) { min = tmp; pos = i;}
}
retPos[1] = pos;
//*************************************
sum = 0;
for (int i = x2+1; i < testDataDistance.length ; i++)
sum += testDataDistance[i][1];
avgDist[2] = sum/(x2+1);
min = Double.MAX_VALUE;
pos =0;
for (int i = 0; i < x1+1 ; i++){
tmp = Math.abs( avgDist[2] - testDataDistance[i][1] );
if (tmp < min) { min = tmp; pos = i;}
}
retPos[2] = pos;
return retPos;
}*/
public int[] getStartingPointArrayIndex(){
double[][] testDataDistance = new double[m_TestCases][2];
double SumData = 0.0; //點到群中心距離(群中心也是四維的點)
for (int m = 0; m < this.m_TestCases;m++) {
SumData = 0.0;
for (int n = 0; n < m_TestVariables; n++) {//4個變數
SumData += Math.pow((m_TestData[m][n] - 0), 2);
}
testDataDistance[m][0] = m;
testDataDistance[m][1] = SumData;
}
/*for(int i= 0; i < testDataDistance.length;i++){
System.out.format("%d \n",(int) testDataDistance[i]);
}*/
System.out.println("======================================");
//quicksort(testDataDistance, 0, testDataDistance.length-1);
for(int i= 0; i < testDataDistance.length;i++){
System.out.format("%d=%f \n",(int)testDataDistance[i][0], testDataDistance[i][1]);
}
int x1 = Math.round(((m_TestCases-1-0)/(1-0))/3);
int x2 = Math.round((((m_TestCases-1-0)/(1-0))*2)/3);
double[] avgDist = new double[this.m_NumOfClusters];
int[] retPos = new int[this.m_NumOfClusters];
double sum = 0.0;
double tmpAvg = 0.0;
for (int i = 0; i < x1+1 ; i++)
sum += testDataDistance[i][1];
avgDist[0]= sum/(x2+1);
double min = Double.MAX_VALUE;
int pos =0;
double tmp;
for (int i = 0; i < x1+1 ; i++){
tmp = Math.abs( avgDist[0] - testDataDistance[i][1] );
if (tmp < min) { min = tmp; pos = i;}
}
retPos[0] = pos;
//*************************************
sum = 0;
for (int i = x1+1; i < x2+1 ; i++)
sum += testDataDistance[i][1];
avgDist[1] = sum/(x2+1);
min = Double.MAX_VALUE;
pos =0;
for (int i = 0; i < x1+1 ; i++){
tmp = Math.abs( avgDist[1] - testDataDistance[i][1] );
if (tmp < min) { min = tmp; pos = i;}
}
retPos[1] = pos;
//*************************************
sum = 0;
for (int i = x2+1; i < testDataDistance.length ; i++)
sum += testDataDistance[i][1];
avgDist[2] = sum/(x2+1);
min = Double.MAX_VALUE;
pos =0;
for (int i = 0; i < x1+1 ; i++){
tmp = Math.abs( avgDist[2] - testDataDistance[i][1] );
if (tmp < min) { min = tmp; pos = i;}
}
retPos[2] = pos;
return retPos;
}
/* public int[] getStartingPointArrayIndex(){
int[] retPos = new int[this.m_NumOfClusters];
double[][] testDataDistance = new double[m_TestCases][2];
double SumData = 0.0; //點到群中心距離(群中心也是四維的點)
double min = Double.MAX_VALUE;
int minPos = 0;
double max = Double.MIN_VALUE;
int maxPos = 0;
for (int m = 0; m < this.m_TestCases;m++) {
SumData = 0.0;
for (int n = 0; n < m_TestVariables; n++) {//4個變數
SumData += Math.pow((m_TestData[m][n] - 0), 2);
}
testDataDistance[m][0] = m;
testDataDistance[m][1] = SumData;
if ( SumData < min) { min = SumData; minPos = m;}
if ( SumData > max) { max = SumData; maxPos = m;}
}
int width = (int)(max - min)/ m_NumOfClusters;
double[] distributedData = new double[m_TestCases];
for (int m = 0; m < this.m_TestCases;m++) {
}
return retPos;
}*/
private void EuclideanDistance( double[][] centroid){
//計算資料點到群中心的距離,將點放到不同的组中
//每個資料點依據所給的變數 (例如: iris 有四個變數)的多維資料產生一個點
//與目前群中心的距離
for (int i = 0; i < m_TestCases; i++){ //iris 150 case
double tmpDis = 0.0;
int idxCluster = 0; //目前m_ClusterNo陣列位置
for (int j = 0; j < m_NumOfClusters; j++) //3個分群數
{
double Distance2Centroid = 0.0; //點到群中心距離(群中心也是四維的點)
for (int m = 0; m < m_TestVariables; m++) //4個變數
Distance2Centroid += Math.pow((m_TestData[i][m] - centroid[j][m]), 2);
//如果目前的cluster編號是0, 重設m_ClusterNo的陣列索引值
//目前點的距離暫存做比對
if (j == 0){
tmpDis = Distance2Centroid;
idxCluster = 0;
} else { //目前的cluster編號非0, 代表cluster =1,2...etc(端視分群數),
if (tmpDis > Distance2Centroid) {//將資料點指定給距離最近的群中心,這裡記錄群的索引值
tmpDis = Distance2Centroid;
idxCluster = j;
}
//這裡可以掠過判斷 tmpDis < Distance2Centroid
}
}
int ClusterNo = m_ClusterNo[idxCluster];
m_aryResultClusters[idxCluster][ClusterNo] = i + 1;
m_ClusterNo[idxCluster]++; //紀錄各(3)群集中的資料個數
}
}
private void EuclideanDistanceSquar( double[][] centroid){
//計算資料點到群中心的距離,將點放到不同的组中
//每個資料點依據所給的變數 (例如: iris 有四個變數)的多維資料產生一個點
//與目前群中心的距離
for (int i = 0; i < m_TestCases; i++){ //iris 150 case
double tmpDis = 0.0;
int idxCluster = 0; //目前m_ClusterNo陣列位置
for (int j = 0; j < m_NumOfClusters; j++) //3個分群數
{
double Distance2Centroid = 0.0; //點到群中心距離(群中心也是四維的點)
for (int m = 0; m < m_TestVariables; m++) //4個變數
Distance2Centroid += Math.pow((m_TestData[i][m] - centroid[j][m]), 2);
Distance2Centroid =Math.sqrt(Distance2Centroid);
//如果目前的cluster編號是0, 重設m_ClusterNo的陣列索引值
//目前點的距離暫存做比對
if (j == 0){
tmpDis = Distance2Centroid;
idxCluster = 0;
} else { //目前的cluster編號非0, 代表cluster =1,2...etc(端視分群數),
if (tmpDis > Distance2Centroid) {//將資料點指定給距離最近的群中心,這裡記錄群的索引值
tmpDis = Distance2Centroid;
idxCluster = j;
}
//這裡可以掠過判斷 tmpDis < Distance2Centroid
}
}
int ClusterNo = m_ClusterNo[idxCluster];
m_aryResultClusters[idxCluster][ClusterNo] = i + 1;
m_ClusterNo[idxCluster]++; //紀錄各(3)群集中的資料個數
}
}
private void ManhattanDistance( double[][] centroid){
//計算資料點到群中心的距離,將點放到不同的组中
//每個資料點依據所給的變數 (例如: iris 有四個變數)的多維資料產生一個點
//與目前群中心的距離
for (int i = 0; i < m_TestCases; i++){ //iris 150 case
double tmpDis = 0.0;
int idxCluster = 0; //目前m_ClusterNo陣列位置
for (int j = 0; j < m_NumOfClusters; j++) //3個分群數
{
double Distance2Centroid = 0.0; //點到群中心距離(群中心也是四維的點)
for (int m = 0; m < m_TestVariables; m++) //4個變數
Distance2Centroid += Math.abs((m_TestData[i][m] - centroid[j][m]));
//如果目前的cluster編號是0, 重設m_ClusterNo的陣列索引值
//目前點的距離暫存做比對
if (j == 0){
tmpDis = Distance2Centroid;
idxCluster = 0;
} else { //目前的cluster編號非0, 代表cluster =1,2...etc(端視分群數),
if (tmpDis > Distance2Centroid) {//將資料點指定給距離最近的群中心,這裡記錄群的索引值
tmpDis = Distance2Centroid;
idxCluster = j;
}
//這裡可以掠過判斷 tmpDis < Distance2Centroid
}
}
int ClusterNo = m_ClusterNo[idxCluster];
m_aryResultClusters[idxCluster][ClusterNo] = i + 1;
m_ClusterNo[idxCluster]++; //紀錄各(3)群集中的資料個數
}
}
public int[][] DoPartition() {
double[][] centroid = new double[m_NumOfClusters][m_TestVariables];
int[] grandCent = getStartingPointArrayIndex();
//群重心
/* for (int i = 0; i < m_NumOfClusters; i++)
for (int j = 0; j < m_TestVariables; j++)
centroid[i][j] = m_TestData[grandCent[i]][j];*/
//群前3點
/* for (int i = 0; i < m_NumOfClusters; i++)
for (int j = 0; j < m_TestVariables; j++)
centroid[i][j] = m_TestData[i][j];*/
for (int j = 0; j < m_TestVariables; j++)
centroid[0][j] = m_TestData[0][j];
for (int j = 0; j < m_TestVariables; j++)
centroid[1][j] = m_TestData[50][j];
for (int j = 0; j < m_TestVariables; j++)
centroid[2][j] = m_TestData[148][j];
double[][] preCenter = new double[m_NumOfClusters][m_TestVariables];
while (true){
m_aryResultClusters = new int[m_NumOfClusters][m_TestCases];
//清空群編號
for (int i = 0; i < m_NumOfClusters; i++)
m_ClusterNo[i] = 0;
//計算資料點到群中心的距離,將點放到不同的组中
//每個資料點依據所給的變數 (例如: iris 有四個變數)的多維資料產生一個點
//與目前群中心的距離
/*for (int i = 0; i < m_TestCases; i++){ //iris 150 case
double tmpDis = 0.0;
int idxCluster = 0; //目前m_ClusterNo陣列位置
for (int j = 0; j < m_NumOfClusters; j++) //3個分群數
{
double Distance2Centroid = 0.0; //點到群中心距離(群中心也是四維的點)
for (int m = 0; m < m_TestVariables; m++) //4個變數
Distance2Centroid += Math.pow((m_TestData[i][m] - centroid[j][m]), 2);
//如果目前的cluster編號是0, 重設m_ClusterNo的陣列索引值
//目前點的距離暫存做比對
if (j == 0){
tmpDis = Distance2Centroid;
idxCluster = 0;
} else { //目前的cluster編號非0, 代表cluster =1,2...etc(端視分群數),
if (tmpDis > Distance2Centroid) {//將資料點指定給距離最近的群中心,這裡記錄群的索引值
tmpDis = Distance2Centroid;
idxCluster = j;
}
//這裡可以掠過判斷 tmpDis < Distance2Centroid
}
}
int ClusterNo = m_ClusterNo[idxCluster];
m_aryResultClusters[idxCluster][ClusterNo] = i + 1;
m_ClusterNo[idxCluster]++; //紀錄各(3)群集中的資料個數
}*/
EuclideanDistance( centroid);
//EuclideanDistanceSquar( centroid);
//ManhattanDistance( centroid);
//將目前群中心暫存,以做比對用
for (int i = 0; i < m_NumOfClusters; i++)
for (int j = 0; j < m_TestVariables; j++)
preCenter[i][j] = centroid[i][j];
//產生新群中心(群重心)
for (int i = 0; i < m_NumOfClusters; i++)
{
int kNum = m_ClusterNo[i];
if (kNum > 0)
{
for (int j = 0; j < m_TestVariables; j++){
double sum = 0.0;
for (int m = 0; m < kNum; m++){
int groupidxCluster = m_aryResultClusters[i][m] - 1;
sum += m_TestData[groupidxCluster][j];
}
centroid[i][j] = sum / kNum;
}
}
}
//比對新舊群中心是否不同
boolean bFinish = true;
for (int i = 0; i < m_NumOfClusters; i++){
for (int j = 0; j < m_TestVariables; j++)
{
bFinish = bFinish && (preCenter[i][j] == centroid[i][ j]);
}
}
if (bFinish)
//複製群中心
for (int i =0; i < centroid.length; i++){
for (int j=0; j< centroid[i].length; j++)
m_Centroid[i][j] = centroid[i][j];
}
break;
}
return m_aryResultClusters;
}
public double[] getSumOfSqrByClusters() {
double[] ret = new double[0];
return ret;
}
public double[] getAvgDistanceFromCentroid(){
double[] ret = new double[0];
return ret;
}
public double[] getMaxDistanceFromCentroid(){
double[] ret = new double[0];
return ret;
}
public int[] getNumOfObservations() {
int[] ret = new int[this.m_NumOfClusters];
for (int i=0; i< ret.length; i++)
ret[i] =0;
for (int i=0;i < this.m_aryResultClusters.length; i++ ){
for (int j=0;j < this.m_aryResultClusters[i].length; j++ ){
if ((int)this.m_aryResultClusters[i][j] != 0)
ret[i]++;
}
}
return ret;
}
public int[][] getM_aryResultClusters() {
return m_aryResultClusters;
}
}
.........................................................................................................
KMeansEval.java
/**************************************************************************************************************************
(c) Copyright: Kevin Chen , No.79912001, 2010~2012. All rights reserved.
Author: : Kevin Chen
Desc :
Change History : -
Class used in :
Change History : Level Changed By Date Change description
-------------------------------------------------------------------------------------------------------
(Initial) Kevin Chen 20100112 (Initial)
'**************************************************************************************************************************/
package e9912001;
import e9912001.KMeans;
public class KMeansEval {
/**
* @param args
*/
public static void main(String[] args) {
double[][] test_data = {
{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},
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.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}};
KMeans kms = new KMeans(test_data);
kms.setM_NumOfClusters(3);
int[][] results = kms.DoPartition();
for (int i =0; i< results.length ;i++){
for (int j = 0; j < results[i].length ; j++){
System.out.format("%d, ", results[i][j]);
}
System.out.println();
}
//System.out.println("Observations, Sum of squares, Avg Distance, Max Distance ")
int[] DataInCluster = kms.getNumOfObservations();
for(int i =0; i< DataInCluster.length; i++)
System.out.println(DataInCluster[i]);
}
}
留言列表