DMBI Experiment no 5

Aim - Implementation of K-means using JAVA.

Implementation of K-means using JAVA.

import java.util.*;
class k_means{
static int count1,count2,count3;
static int d[];
static int k[][];
static int tempk[][];
static double m[];
static double diff[];
static int n,p;
static int cal_diff(int a) {
int temp1=0;
for(int i=0;i<p;++i) {
if(a>m[i])
diff[i]=a-m[i];
else
diff[i]=m[i]-a; }
int val=0;
double temp=diff[0];
for(int i=0;i<p;++i) {
if(diff[i]<temp) {
temp=diff[i];
val=i;  } }
return val; }
static void cal_mean()  {
for(int i=0;i<p;++i)
m[i]=0;  
int cnt=0;
for(int i=0;i<p;++i) {
cnt=0;
for(int j=0;j<n-1;++j) {
if(k[i][j]!=-1) {
m[i]+=k[i][j];
++cnt;
}}
m[i]=m[i]/cnt;
} }
static int check1()  {
for(int i=0;i<p;++i)
for(int j=0;j<n;++j)
if(tempk[i][j]!=k[i][j]) {
return 0; }
return 1; }
public static void main(String args[]) {
Scanner scr=new Scanner(System.in);
System.out.println("Enter the number of elements ");
n=scr.nextInt();
d=new int[n];
system.out.println("Enter "+n+" elements: ");
for(int i=0;i<n;++i)
d[i]=scr.nextInt();
System.out.println("Enter the number of clusters: ");
p=scr.nextInt();
k=new int[p][n];
tempk=new int[p][n];
m=new double[p];
diff=new double[p];
for(int i=0;i<p;++i)
m[i]=d[i];
int temp=0;
int flag=0;
do {
for(int i=0;i<p;++i)
for(int j=0;j<n;++j) {
k[i][j]=-1; }
for(int i=0;i<n;++i)  {
temp=cal_diff(d[i]);
if(temp==0)
k[temp][count1++]=d[i];
else
if(temp==1)
k[temp][count2++]=d[i];
else
if(temp==2)
k[temp][count3++]=d[i];  }
cal_mean();.
flag=check1();
if(flag!=1)
for(int i=0;i<p;++i)
for(int j=0;j<n;++j)
tempk[i][j]=k[i][j];
System.out.println("\n\nAt this step");
System.out.println("\nValue of clusters");
for(int i=0;i<p;++i) {
System.out.print("K"+(i+1)+"{ ");
for(int j=0;k[i][j]!=-1 && j<n-1;++j)
System.out.print(k[i][j]+" ");
System.out.println("}"); }
System.out.println("\nValue of m ");
for(int i=0;i<p;++i)
System.out.print("m"+(i+1)+"="+m[i]+"  ");
count1=0;count2=0;count3=0; }
while(flag==0);
System.out.println("\n\n\nThe Final Clusters By Kmeans are as follows: ");
for(int i=0;i<p;++i) {
System.out.print("K"+(i+1)+"{ ");
for(int j=0;k[i][j]!=-1 && j<n-1;++j)
System.out.print(k[i][j]+" ");
System.out.println("}");
}}}

OUTPUT :-

D:\TEIT>java k_means
Enter the number of elements-8
Enter 8 elements:
2 3 4 6 7 8 9 12
Enter the number of clusters:3
At this step
Value of clusters
K1{ 2 }
K2{ 3 }
K3{ 4 6 7 8 9 12 }
Value of m
m1=2.0  m2=3.0  m3=7.666666666666667
At this step
Value of clusters
K1{ 2 }
K2{ 3 4 }
K3{ 6 7 8 9 12 }
Value of m- m1=2.0  m2=3.5  m3=8.4
At this step Value of clusters
K1{ 2 }
K2{ 3 4 }
K3{ 6 7 8 9 12 }
Value of m-m1=2.0  m2=3.5  m3=8.4
The Final Clusters By Kmeans are as follows:
K1{ 2 }
K2{ 3 4 }

K3{ 6 7 8 9 12 }


DMBI Experiment no 5 DMBI Experiment no 5 Reviewed by akshay salve on 8:55 PM Rating: 5

No comments:

Powered by Blogger.