-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCentroid.java
More file actions
130 lines (130 loc) · 4.84 KB
/
Centroid.java
File metadata and controls
130 lines (130 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import java.io.*;
import java.util.*;
import java.lang.*;
import java.text.*;
public class Centroid
{
// finds the mean of the dataset for grouping
public void Grouping(double[] Cordx, double[] Cordy, int clustNumber)
{
int clusterNumber = clustNumber;
double[] ClustCordX = new double[clustNumber];
double[] ClustCordY = new double[clustNumber];
this.getMeansetCentroid(Cordx, Cordy, clustNumber);
DecimalFormat dec = new DecimalFormat("0.00");
for(int i = 0;i<Cordx.length;i++)
{
String result1 = dec.format(Cordx[i]);
String result2 = dec.format(Cordy[i]);
System.out.println("\n Cords are ( " + result1 + " , " + result2 + ")");
}
//setting random datasets as centroids
for(int i = 0; i<clustNumber;i++)
{
ClustCordX[i] = Cordx[i];
ClustCordY[i] = Cordy[i];
}
this.groupCordtoCluster(Cordx,Cordy,ClustCordX,ClustCordY);
}
public void groupCordtoCluster(double[] Cordx, double[] Cordy, double[] ClustCordX, double[] ClustCordY)
{
double temp ;
int size = Cordx.length;
int clustsize = ClustCordX.length;
int clusterComparison = clustsize;
int[] grouping = new int[size - clustsize];
double[] ClustgroupX = new double[size - clustsize];
double[] ClustgroupY = new double[size - clustsize];
int tempint = -1;
//grouping the dataset to respective clusters by comparing the distance
for(int i = clusterComparison; i < size;i++)
{
temp = 0;
for(int j = 0;j<clustsize;j++)
{
if (j == 0)
tempint++;
if(temp == 0)
{
temp = Math.sqrt(Math.pow((Cordx[i]-ClustCordX[j]),2) + Math.pow((Cordy[i]-ClustCordY[j]),2));
grouping[tempint] = j;
ClustgroupX[tempint] = Cordx[i];
ClustgroupY[tempint] = Cordy[i];
}
else if (temp > Math.sqrt(Math.pow((Cordx[i]-ClustCordX[j]),2) + Math.pow((Cordy[i]-ClustCordY[j]),2)))
{
temp = Math.sqrt(Math.pow((Cordx[i]-ClustCordX[j]),2) + Math.pow((Cordy[i]-ClustCordY[j]),2));
grouping[tempint] = j;
ClustgroupX[tempint] = Cordx[i];
ClustgroupY[tempint] = Cordy[i];
}
}
}
DecimalFormat dec = new DecimalFormat("0.00");
String result1, result2, result3, result4;
for(int i = 0; i<grouping.length;i++)
{
//for(int i = 1; i< clustNumber;i++) {
System.out.println("------------------------");
System.out.println("Clusters for group " + grouping[i]);
result1 = dec.format(Cordx[grouping[i]]);
result2 = dec.format(Cordy[grouping[i]]);
result3 = dec.format(ClustgroupX[i]);
result4 = dec.format(ClustgroupY[i]);
System.out.println("Cordinates are (" + result1 + " , " + result2 + ")");
System.out.println("------------------------");
System.out.println("Clusters for group " + grouping[i]);
System.out.println("Cordinates are (" + result3 + " , " + result4 + ")");
}
}
public void getMeansetCentroid(double[] Cordx, double[] Cordy, int ClustNumber)
{
double xCord, yCord;
double MAX=0,distance, tempd1, tempd2;
double[] Distances = new double[Cordx.length];
int reference, i, j, temp1, temp2, point, length;
reference = i = j = temp1 = temp2 = point =0;
int[] centroids;
for(j = 1; j < Cordx.length;j++)
{
Distances[j-1] = Math.sqrt(Math.pow((Cordx[j]-Cordx[0]),2) + Math.pow((Cordy[j]-Cordy[0]),2));
}
for(i=0;i<Cordx.length-1;i++)
{
for(j=0;j<Cordx.length-1-i;j++)
{
if(Distances[j+1] < Distances[j])
{
distance = Distances[j];
tempd1 = Cordx[j];
tempd2 = Cordy[j];
Distances[j] = Distances[j+1];
Cordx[j] = Cordx[j+1];
Cordy[j] = Cordy[j+1];
Distances[j+1] = distance;
Cordx[j+1] = tempd1;
Cordy[j+1] = tempd2;
}
}
}
//recalculation of centroids
point = Cordx.length;
do
{
if(Cordx.length % ClustNumber != 0)
point--;
}while(point % ClustNumber != 0);
length = point/ClustNumber;
for(i=0;i<Cordx.length;i=length+i)
{
if((i+length-1) > point)
break;
tempd1 = Cordx[i];
tempd2 = Cordy[i];
Cordx[i] = Cordx[i+length-1];
Cordy[i] = Cordy[i+length-1];
Cordx[i+length-1] = tempd1;
Cordy[i+length-1] = tempd2;
}
}
}