-
Notifications
You must be signed in to change notification settings - Fork 0
/
kmeans.java
145 lines (133 loc) · 2.37 KB
/
kmeans.java
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//K-means clustering
import java.io.*;
class kmeans
{
public static void main(String args[])throws IOException
{
kmean k=new kmean();
k.accept();
}
}
class kmean
{
int n; int a[];int c;double m[];int cluster[][];
void accept()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the no of elements");
n=Integer.parseInt(br.readLine());
a=new int[n];
System.out.println("Enter the elements");
for(int i=0;i<n;i++)
a[i]=Integer.parseInt(br.readLine());
System.out.println("Enter the no of clusters");
c=Integer.parseInt(br.readLine());
m=new double[c];
cluster=new int[c][n];
System.out.println("Enter the initial means");
for(int i=0;i<c;i++)
m[i]=Integer.parseInt(br.readLine());
cal();
}
void cal()
{
double diff;int b;int k[]=new int[c];int iter=1;int sum=0;
int temp[][]=new int[c][n];
int no[]=new int[c];
while(true)
{
for(int i=0;i<n;i++)
{
diff=Math.abs(a[i]-m[0]);
b=0;
for(int j=1;j<c;j++)
{
if(Math.abs(a[i]-m[j])<diff)
{
diff=Math.abs(a[i]-m[j]);
b=j;
}
}
cluster[b][k[b]++]=a[i];
no[b]+=1;
}
int flag=0;
for(int j=0;j<c;j++)
for(int i=0;i<n;i++)
{
if(cluster[j][i]!=temp[j][i])
{flag=1;break;}
}
if(flag==0)
break;
for(int j=0;j<c;j++)
{
for(int i=0;i<no[j];i++)
sum+=cluster[j][i];
m[j]=(double)sum/no[j];
sum=0;
}
System.out.println("Iteration "+(iter++));
for(int j=0;j<c;j++)
System.out.println("Mean "+(j+1)+":"+m[j]);
for(int j=0;j<c;j++)
{
System.out.print("Cluster "+(j+1)+":");
for(int i=0;i<no[j];i++)
{
System.out.print(cluster[j][i]+" ");
temp[j][i]=cluster[j][i];
}
System.out.println();
}
for(int j=0;j<c;j++)
for(int i=no[j];i<n;i++)
temp[j][i]=0;
System.out.println();
for(int j=0;j<c;j++)
for(int i=0;i<n;i++)
cluster[j][i]=0;
for(int i=0;i<c;i++)
{no[i]=0;k[i]=0;}
}
}
}
/*OUTPUT
Enter the no of elements
9
Enter the elements
2
4
10
12
3
20
30
11
25
Enter the no of clusters
2
Enter the initial means
3
4
Iteration 1
Mean 1:2.5
Mean 2:16.0
Cluster 1:2 3
Cluster 2:4 10 12 20 30 11 25
Iteration 2
Mean 1:3.0
Mean 2:18.0
Cluster 1:2 4 3
Cluster 2:10 12 20 30 11 25
Iteration 3
Mean 1:4.75
Mean 2:19.6
Cluster 1:2 4 10 3
Cluster 2:12 20 30 11 25
Iteration 4
Mean 1:7.0
Mean 2:25.0
Cluster 1:2 4 10 12 3 11
Cluster 2:20 30 25
*/