You are on page 1of 11

Ques 1 Data Summarization.

Ques 2 Data Visualization.

//Ques 3. Find Correlation.


//Ques 4.Intercluster and Intracluster Distance Mean.

package bi;

import java.io.*;

import java.util.*;

public class Dist

{ public static void main(String[] args)

Scanner in=new Scanner(System.in);

System.out.print("ENTER THE NO.OF POINTS :: ");

int c=in.nextInt();

int x[][]=new int[2][c];

int i,j,s=0;

double sx=0,d,m;

System.out.print("ENTER THE "+c*2+" ELEMENTS :: ");

for(i=0;i<2;i++)

{ for(j=0;j<c;j++)

int ele=in.nextInt();

x[i][j]=ele;

System.out.print("YOUR MATRIX IS ::\n");

for(i=0;i<2;i++)

{ for(j=0;j<c;j++)

System.out.print(x[i][j]+ "\t");

System.out.print("\n");

for(i=0;i<c;i++)

d=Math.sqrt(sx);

System.out.println("\nEUCLIDEAN DISTANCE IS :: "+d);

sx=0;
for(i=0;i<c;i++)

sx += Math.abs(x[0][i]-x[1][i]);

System.out.println("MANHATTAN DISTANCE IS :: "+sx);

double p[][]=new double[c][c];

double pt[][]=new double[c][c];

double pm[][]=new double [c][c];

double res[][]=new double [c][c];

//Subtracting xi-xj

for(i=0;i<c;i++)

p[0][i]=x[0][i]-x[1][i];

//Finding mean

for(i=0;i<c;i++)

s+=p[0][i];

m=(double)s/c;

//Normalised vector

for(i=0;i<c;i++)

pm[0][i]=p[0][i]-m;

//Transpose matrix

for(i=0;i<c;i++)

pt[i][0]=pm[0][i];

//Matrix multiplication res=pm'*pm (Covariance matrix)

for(i=0;i<c;i++)

{ for(j=0;j<c;j++)

res[i][j]=0;

for(int k=0;k<1;k++)

res[i][j]+=pt[i][k]*pm[k][j]; }

//M=p*res*p'

for(i=0;i<1;i++)
{

for(j=0;j<c;j++)

pt[i][j]=0;

for(int k=0;k<c;k++)

pt[i][j]+=p[i][k]*res[k][j];

for(i=0;i<1;i++)

for(j=0;j<1;j++)

res[i][j]=0;

for(int k=0;k<c;k++)

res[i][j]+=pt[i][k]*p[j][k];

double r=Math.sqrt(res[0][0]);

System.out.println("MAHANOBOLIS DISTANCE IS :: "+r);

OUTPUT:
// Ques 5. Compute Scatter Matrix.

#include<iostream.h>

#include<conio.h>

int main()

clrscr();

float a[20][20],sum,mean[10],b[20][20];

int r,c,j,k=0,l=0;

cout<<"enter the rows and columns \n";

cin>>r>>c;

cout<<"enter the matrix input \n";

for(int i=0;i<r;i++)

for(int j=0;j<c;j++)

cin>>a[i][j];

for(i=0;i<c;i++)

{ sum=0;

for(j=0;j<r;j++)

sum+=a[j][i];

mean[k]=sum/r;

k++;

cout<<endl;

for( i=0;i<c;i++)

for(j=0;j<r;j++)

a[j][i]=a[j][i]-mean[l];

l++;

for(i=0;i<r;i++)

for(j=0;j<c;j++)

b[i][j]=a[j][i];

for(i = 0; i < r; ++i)


for(j = 0; j < c; ++j)

for(k = 0; k < c; ++k)

a[i][j] += a[i][k] * b[k][j];

for(i=0;i<r;i++)

{ for(int j=0;j<c;j++)

{ cout<<" "<<a[i][j]<<" ";

if(j==c-1)

cout<<endl;

getch();

return 0;

OUTPUT:
//Ques 6. Implement Principle component analysis.

import java.io.*;
import java.util.*;

public class PCA


{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.print("ENTER THE NO.OF POINTS :: ");
int r=in.nextInt();
double x[][]=new double[r][2];
double res[][]=new double[r][r];
double e[][]=new double[r][r];

int i,j,sum=0;
double sx=0,sy=0,p,q;
System.out.print("ENTER THE "+r*2+" ELEMENTS :: ");
for(i=0;i<r;i++)
{
for(j=0;j<2;j++)
{
int ele=in.nextInt();
x[i][j]=ele;
}
}
System.out.print("YOUR MATRIX IS ::\n");
for(i=0;i<r;i++)
{
for(j=0;j<2;j++)
System.out.print(x[i][j]+ "\t");
System.out.print("\n");
}
for(i=0;i<r;i++)
{
sx += x[i][0];
sy += x[i][1];
}
for(i=0;i<r;i++)
{
x[i][0]=x[i][0]-(sx/r);
x[i][1]=x[i][1]-(sy/r);
}
System.out.print("\nCOVARIANCE MATRIX IS :: \n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
res[i][j]=0;
for(int k=0;k<r;k++)
res[i][j] += x[k][i]*x[k][j];
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)

System.out.print(res[i][j]+ "\t");
System.out.print("\n");
}

//Eigen values
double dis=(Math.pow(-res[0][0]-res[1][1],2)-
(4*(res[0][0]*res[1][1]-res[1][0]*res[0][1])));
double l1=((res[0][0]+res[1][1])+Math.sqrt(dis))/2;
double l2=((res[0][0]+res[1][1])-Math.sqrt(dis))/2;
System.out.print("\nEIGEN VALUES ARE :: "+l1+ "
"+l2+"\n");

//Eigen vector
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
if(i==j)
{
if(l1>l2)
res[i][j]-=l1;
else
res[i][j]-=l2;
}
else
continue;
}
}
p=res[0][1];
q=-res[0][0];
e[0][0]=p;
e[0][1]=q;
System.out.print("\nREDUCED DIMENSIONS ARE :: ");
for(i=0;i<1;i++)
{
for(j=0;j<r;j++)
{
for(int k=0;k<2;k++)
sum+=e[i][k]*x[j][k];
System.out.print(sum+"\t");
sum=0;
}
}
}
}
Output:
//Ques 7: Implement K-means Clustering.

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
float inp[20][20], d[20][20];
int i,j,n,k,p=0,c[10][10],flag=0;
int ctri=0,ctrj=0,min,g[20][20]={0};

cout<<"Enter the number of values:";


cin>>n;
cout<<"\nEnter the x and y values of data:";
for(i=0;i<n;i++)
{
for(j=0;j<2;j++)
cin>>inp[i][j];
}
cout<<"\nEnter the value of k:";
cin>>k;
for(i=0;i<k;i++)
{
for(j=0;j<2;j++)
c[i][j]=inp[i][j];
}
for(i=0;i<k;i++)
{
for(j=0;j<n;j++)
d[i][j]=sqrt(pow((inp[j][p]-c[i][p]),2)+pow((inp[j][p+1]-c[i][p+1]),2));
}

cout<<"\nD matrix is\n";


for(i=0;i<k;i++)

{
for(j=0;j<n;j++)
cout<<d[i][j]<<" ";
cout<<endl;
}
for(i=0;i<n;i++)
{
j=0;
min=d[j][i];
int b=j,z=i;
flag=0;
for(j=0;j<k-1;j++)
{
if(min>=d[j+1][i])
{
min=d[j+1][i];
ctri=j+1;
ctrj=i;
flag=1;
}
}
if(flag==0)
{
ctri=b;
ctrj=z;
g[ctri][ctrj]=1;
}
else
g[ctri][ctrj]=1;
}

cout<<"\nG matrix is\n";


for(i=0;i<k;i++)
{
for(j=0;j<n;j++)
cout<<g[i][j]<<" ";
cout<<endl;
}
float sumx=0,sumy=0,ctr=0;
for(i=0;i<k;i++)
{
ctr=0;
sumx=0;
sumy=0;
p=0;
for(j=0;j<n;j++)
{
if(g[i][j]==1)
{
sumx=sumx+inp[j][p];
sumy=sumy+inp[j][p+1];
ctr++;
}
}
cout<<"\nCluster "<<i+1<<" is:"<<"("<<sumx/ctr<<","<<sumy/ctr<<")";
}
getch();
}