0% found this document useful (0 votes)
288 views

DWM Experiments

The document discusses K-nearest neighbors (KNN) algorithm for classification. It includes code to: 1) Read in a dataset with attributes of gender, height, and classification (tall, short, medium); 2) Calculate distances between a new data point and all points in the dataset; 3) Sort points by distance and select the K nearest neighbors; 4) Count the classifications of the K nearest neighbors and output the majority classification. The code is run on sample data and outputs the K nearest neighbors and predicts the new point has a medium classification.

Uploaded by

Ashish Mathew
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
288 views

DWM Experiments

The document discusses K-nearest neighbors (KNN) algorithm for classification. It includes code to: 1) Read in a dataset with attributes of gender, height, and classification (tall, short, medium); 2) Calculate distances between a new data point and all points in the dataset; 3) Sort points by distance and select the K nearest neighbors; 4) Count the classifications of the K nearest neighbors and output the majority classification. The code is run on sample data and outputs the K nearest neighbors and predicts the new point has a medium classification.

Uploaded by

Ashish Mathew
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 21

#include<iostream.

h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int m1,m2,i,j,n,x,y,sum1,sum2,k;
int d[100],k1[50],k2[50];
cout<<"Enter the no. of items in cluster:";
cin>>n;
cout<<"Enter the items of the cluster:";
for(i=0;i<n;i++)
cin>>d[i];
for(i=0;i<n;i++)
{
k1[i]=k2[i]=0;
}
m1=d[0];
m2=d[1];
k=1;
while(1)
{
x=0;
y=0;
for(i=0;i<n;i++)
{
if(abs(m1-d[i]) <= abs(m2-d[i]))
{
k1[x++]=d[i];
}
else
k2[y++]=d[i];
}
cout<<"\n\n\n** Iteration:"<<k<<" **\n"<<endl;
cout<<"cluster1={";
for(i=0;i<x;i++)
{
cout<<k1[i]<<",";
}
cout<<"}"<<endl;
cout<<"cluster2={";
for(i=0;i<y;i++)
{
cout<<k2[i]<<",";
}
cout<<"}"<<endl;
sum1=sum2=0;
for(i=0;i<x;i++)
sum1=sum1+k1[i];
sum1=sum1/x;
for(i=0;i<y;i++)
sum2=sum2+k2[i];
sum2=sum2/y;
if(sum1==m1 && sum2==m2)
break;
else
{
m1=sum1;
m2=sum2;
}
k++;
}
getch();
}
/*Apriori */

#include<stdio.h>
#include<conio.h>

int item[5][5]={{1,3,4,6},{2,3,5,7},{1,2,3,5,8},{2,5,9,10},{1,4}};
int fn(int a,int b);
int main()
{
int tid[5]={1,2,3,4,5} ,i,j,ms,k,count[11]={0};
int p,a,b,c;
printf("\n\tTrans_id\t\tItems\n\n");
for(i=0;i<5;i++)
{
printf("\t%d \t\t\t",tid[i]);
for(j=0;j<5;j++)
{
if(item[i][j]!=0)
printf("%d",item[i][j]);
}
printf("\n");
}
printf("\n Assume minimum support:");
scanf("%d",&ms);
for(k=1;k<=10;k++)
{
for(i=0;i<5;i++)
for(j=0;j<5;j++)
if(item[i][j]==k&&item[i][j]!=0)
count[k]++;
}
printf("\nGenerating c1 from the data");
for(k=1;k<=10;k++)
printf("\n\t\t%d\t\t%d",k,count[k]);
for(k=1;k<=10;k++)
{
if(count[k]<ms)
count[k]=0;
}
printf("\nGenerating l1 from c1");
for(k=1;k<=10;k++)
{
a=2;
if(count[k]!=0)
printf("\n\t\t%d\t\t%d",k,count[k]);
}
printf("\nGenerating l2 from c2");
p=2;
b=3;
for(i=1;i<4;i++)
{
for(j=p++;j<=5;j++)
{
if(fn(i,j)>=ms)
printf("\n\t\t%d%d\t\t%d",i,j,fn(i,j));
c=5;
}
}
printf("\n\t\tGenerating l3");
printf("\n\n\t\t\t\t%d %d %d",a,b,c);
getch();
return 0;
}
int fn(int a,int b)
{
int i,j,cnt=0,aa,flag=0;
for(i=0;i<5;i++)
{
flag=0;
for(j=0;j<5;j++)
{
aa=j;
if(flag==1)
goto ab;
if(a==item[i][j])
{
flag=1;
j++;
ab:
if(b==item[i][j])
{
cnt++;
break;
}
}
j=aa;
}
}
return cnt;
}
/************OUTPUT***********

Trans_id Items

1 1346
2 2357
3 12358
4 25910
5 14

Assume minimum support:2

Generating c1 from the data


1 3
2 3
3 3
4 2
5 3
6 1
7 1
8 1
9 1
10 1
Generating l1 from c1
1 3
2 3
3 3
4 2
5 3
Generating l2 from c2
13 2
14 2
23 2
25 3
35 2
Generating l3

2 3 5*/
/*** KNN **/
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<string.h>
void main()
{
clrscr();
int i,j,th,count1=0,count2=0,count3=0,indx[20];
char ch;
float h,temp,diff[20];
float ht[20]={1.6f,2.0f,1.9f,1.88f,1.7f,1.85f,1.6f,1.7f,2.2f,2.1f,1.8f,1.95f,1.9f,1.8f,1.75f};
char g[20]={'F','M','F','F','F','M','F','M','M','M','F','M','F','F','F'};
char op[20]
[20]={"short","tall","medium","medium","short","medium","short","short","tall","tall","medium","
medium","medium","medium","medium"};
for(i=0;i<15;i++)
indx[i]=i;
cout<<"Given table is as follows:\n\n\n";
cout<<" "<<"Gender"<<" "<<"Hieght"<<" "<<"Output\n";
for(i=0;i<15;i++)
{
cout<<" "<<g[i]<<" "<<ht[i]<<" "<<op[i]<<"\n";
}
cout<<"\n\nEnter the tuple to be processed:\n<gender,height>=";
cin>>ch>>h;
for(i=0;i<15;i++)
{
diff[i]=fabs(ht[i]-h);
}
for(i=0;i<(15-1);i++)
for(j=0;j<(15-i-1);j++)
{
if(diff[j]>diff[j+1])
{
temp=diff[j];
diff[j]=diff[j+1];
diff[j+1]=temp;
temp=indx[j];
indx[j]=indx[j+1];
indx[j+1]=temp;
}
}
cout<<"Enter teh threshold values:";
cin>>th;
cout<<"The nearest neihbours of given tuple are:\n\n";
cout<<" "<<"Gender"<<" "<<"Hieght"<<" "<<"Output\n";
for(i=0;i<th;i++)
{
cout<<" "<<g[indx[i]]<<" "<<ht[indx[i]]<<" "<<op[indx[i]]<<"\n";
if(strcmp(op[indx[i]],"tall")==0)
count1++;
if(strcmp(op[indx[i]],"short")==0)
count2++;
if(strcmp(op[indx[i]],"medium")==0)
count3++;
}
cout<<"\n Tall="<<count1<<"\n short="<<count2<<"\n medium="<<count3;
if(count1>count2 && count1>count3)
{
cout<<endl;
cout<<"Answer is TALL";
}
if(count2>count1 && count2>count3)
{
cout<<endl;
cout<<"Answer is SHORT";
}
if(count3>count1 && count3>count2)
{
cout<<endl;
cout<<"Answer is MEDIUM";
}
getch();
}
/*Output:
Gender Hieght Output
F 1.6 short
M 2 tall
F 1.9 medium
F 1.88 medium
F 1.7 short
M 1.85 medium
F 1.6 short
M 1.7 short
M 2.2 tall
M 2.1 tall
F 1.8 medium
M 1.95 medium
F 1.9 medium
F 1.8 medium
F 1.75 medium
Enter the tuple to be processed:
<gender,height>=M 1.7
Enter teh threshold values:5
The nearest neihbours of given tuple are:
Gender Hieght Output
F 1.7 short
M 1.7 short
F 1.75 medium
F 1.8 medium
F 1.8 medium

Tall=0
short=2
medium=3
Answer is MEDIUM */
create table Book999
(
book_id varchar(10) primary key,
book_name varchar(30),
author varchar(20),
price number(5)
);

insert into Book999 values('B001','Operating System','M. N. Godse',450);


insert into Book999 values('B002','Micro Processor','P. R. Akhil',580);
insert into Book999 values('B003','Data Mining','Shiv Kumar',370);
insert into Book999 values('B004','Computer Network','M. J. Gupta',520);
insert into Book999 values('B005','Visual Basic 6.0','Vikrant Mane',330);

SQL> select * from Book999;


BOOK_ID BOOK_NAME AUTHOR PRICE
---------- ------------------------------ -------------------- ----------
B001 Operating System M. N. Godse 450
B002 Micro Processor P. R. Akhil 580
B003 Data Mining Shiv Kumar 370
B004 Computer Network M. J. Gupta 520
B005 Visual Basic 6.0 Vikrant Mane 330

create table Student999


(
student_id varchar(10) primary key,
student_name varchar(20),
address varchar(30),
phone number(10)
);

insert into Student999 values('S001','Pravin','Ghatkopar',9664295909);


insert into Student999 values('S002','Akhil','Thane',9619000211);
insert into Student999 values('S003','Babu','Sion',9773595602);
insert into Student999 values('S004','Omkar','Nerul',9876543210);
insert into Student999 values('S005','Chandan','Dadar',9888666444);

SQL> select * from Student999;


STUDENT_ID STUDENT_NAME ADDRESS PHONE
---------- -------------------- ------------------------------ ----------
S001 Pravin Ghatkopar 9664295909
S002 Akhil Thane 9619000211
S003 Babu Sion 9773595602
S004 Omkar Nerul 9876543210
S005 Chandan Dadar 9888666444

create table Librarian999


(
lib_id varchar(10) primary key,
lib_name varchar(20),
age number(5)
);

insert into Librarian999 values('L001','Mohan',32);


insert into Librarian999 values('L002','Sameer',28);
insert into Librarian999 values('L003','Shweta',24);

SQL> select * from Librarian999;


LIB_ID LIB_NAME AGE
---------- -------------------- ----------
L001 Mohan 32
L002 Sameer 28
L003 Shweta 24

create table FactBSL999


(
book_id references Book999(book_id),
student_id references Student999(student_id),
lib_id references Librarian999(lib_id),
location varchar(20)
);

insert into FactBSL999 values('B001','S005','L002','LOC202');


insert into FactBSL999 values('B004','S002','L002','LOC105');
insert into FactBSL999 values('B003','S003','L003','LOC202');
insert into FactBSL999 values('B001','S004','L001','LOC202');
insert into FactBSL999 values('B005','S002','L001','LOC105');
insert into FactBSL999 values('B004','S001','L003','LOC105');
insert into FactBSL999 values('B003','S001','L002','LOC202');

SQL> select * from FactBSL999;


BOOK_ID STUDENT_ID LIB_ID LOCATION
---------- ---------- ---------- --------------------
B001 S005 L002 LOC202
B004 S002 L002 LOC105
B003 S003 L003 LOC202
B001 S004 L001 LOC202
B005 S002 L001 LOC105
B004 S001 L003 LOC105
B003 S001 L002 LOC202

SQL> select * from FactBSL999 where LOCATION='LOC202';


BOOK_ID STUDENT_ID LIB_ID LOCATION
---------- ---------- ---------- --------------------
B001 S005 L002 LOC202
B003 S003 L003 LOC202
B001 S004 L001 LOC202
B003 S001 L002 LOC202

SQL> select count(BOOK_ID) from FactBSL999 where LOCATION='LOC105';


COUNT(BOOK_ID)
--------------
3
SQL> select Book999.BOOK_NAME from Book999,FactBSL999 where LIB_ID='L002' and
Book999.BOOK_ID=FactBS
L999.BOOK_ID;

BOOK_NAME
------------------------------
Operating System
Computer Network
Data Mining

SQL> select Student999.STUDENT_NAME from FactBSL999,Student999 where


Student999.STUDENT_ID=FactBSL99
9.STUDENT_ID;
STUDENT_NAME
--------------------
Akhil
Babu
Omkar
Akhil
Pravin
Pravin
6 rows selected.
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<string.h>
void main()
{
clrscr();
int i,j;
float indx[15],count1=0,count2=0,count3=0,gen[2][3],r[6][3],temp[6][3];
char ch;
float h,ps,pm,pt,mt=0,ms=0,mm=0,fm=0,ft=0,fs=0,ptt[3],lss[3],fl,as,am,at;
float ht[20]={1.6f,2.0f,1.9f,1.88f,1.7f,1.85f,1.6f,1.7f,2.2f,2.1f,1.8f,1.95f,1.9f,1.8f,1.75f};
char g[20]={'F','M','F','F','F','M','F','M','M','M','F','M','F','F','F'};
char op[20]
[20]={"short","tall","medium","medium","short","medium","short","short","tall","tall","medium","
medium","medium","medium","medium"};
for(i=0;i<15;i++)
indx[i]=i;
cout<<"Input table is as follows:\n\n";
cout<<" "<<"Gender"<<" "<<"Hieght"<<" "<<"Output\n";
for(i=0;i<15;i++)
{
cout<<" "<<g[i]<<" "<<ht[i]<<" "<<op[i]<<"\n";
}
for(i=0;i<15;i++)
{
if(strcmp(op[i],"tall")==0)
count1++;
if(strcmp(op[i],"short")==0)
count2++;
if(strcmp(op[i],"medium")==0)
count3++;
}
ps=count2/15;
pt=count1/15;
pm=count3/15;
for(i=0;i<15;i++)
{
if(strcmp(op[i],"tall")==0 && g[i]=='M')
mt++;
if(strcmp(op[i],"short")==0 && g[i]=='M')
ms++;
if(strcmp(op[i],"medium")==0 && g[i]=='M')
mm++;
}
ft=count1-mt;
fm=count3-mm;
fs=count2-ms;
gen[0][0]=ms/count2;
gen[0][1]=mm/count3;
gen[0][2]=mt/count1;
gen[1][0]=fs/count2;
gen[1][1]=fm/count3;
gen[1][2]=ft/count1;
for(i=0;i<6;i++)
{
for(j=0;j<3;j++)
{
r[i][j]=0;
temp[i][j]=0;
}
}
for(i=0;i<15;i++)
{
if(strcmp(op[i],"tall")==0 && ht[i]<1.61)
temp[0][2]++;
if(strcmp(op[i],"short")==0 && ht[i]<1.61)
temp[0][0]++;
if(strcmp(op[i],"medium")==0 && ht[i]<1.61)
temp[0][1];

if(strcmp(op[i],"tall")==0 && ht[i]>1.61 && ht[i]<1.71)


temp[1][2]++;
if(strcmp(op[i],"short")==0 && ht[i]>1.61 && ht[i]<1.71)
temp[1][0]++;
if(strcmp(op[i],"medium")==0 && ht[i]>1.61 && ht[i]<1.71)
temp[1][1];

if(strcmp(op[i],"tall")==0 && ht[i]>1.71 && ht[i]<1.81)


temp[2][2]++;
if(strcmp(op[i],"short")==0 && ht[i]>1.71 && ht[i]<1.81)
temp[2][0]++;
if(strcmp(op[i],"medium")==0 && ht[i]>1.71 && ht[i]<1.81)
temp[2][1]++;

if(strcmp(op[i],"tall")==0 && ht[i]>1.81 && ht[i]<1.91)


temp[3][2]++;
if(strcmp(op[i],"short")==0 && ht[i]>1.81 && ht[i]<1.91)
temp[3][0]++;
if(strcmp(op[i],"medium")==0 && ht[i]>1.81 && ht[i]<1.91)
temp[3][1]++;

if(strcmp(op[i],"tall")==0 && ht[i]>1.91 && ht[i]<2.01)


temp[4][2]++;
if(strcmp(op[i],"short")==0 && ht[i]>1.91 && ht[i]<2.01)
temp[4][0]++;
if(strcmp(op[i],"medium")==0 && ht[i]>1.91 && ht[i]<2.01)
temp[4][1]++;

if(strcmp(op[i],"tall")==0 && ht[i]>2.01)


temp[5][2]++;
if(strcmp(op[i],"short")==0 && ht[i]>2.01)
temp[5][0]++;
if(strcmp(op[i],"medium")==0 && ht[i]>2.01)
temp[5][1]++;
}
for(i=0;i<6;i++)
{
r[i][0]=temp[i][0]/count2;
r[i][1]=temp[i][1]/count3;
r[i][2]=temp[i][2]/count1;
}
cout<<"\n\n"<<"Baysian table:\n\n";
cout<<"Gender\t\tS\tM\tT\tP(S)\tP(M)\tP(T)\n\n";
cout<<" M\t\t"<<ms<<"\t"<<mm<<"\t"<<mt<<"\t"<<gen[0][0]<<"\t"<<gen[0][1]<<"\t"<<gen[0]
[2]<<"\n";
cout<<" F\t\t"<<fs<<"\t"<<fm<<"\t"<<ft<<"\t"<<gen[1][0]<<"\t"<<gen[1][1]<<"\t"<<gen[1]
[2]<<"\n\n";
cout<<"Height(below)\n";
float m=1.61;
for(i=0;i<5;i++)
{
cout<<" "<<m<<" \t"<<temp[i][0]<<"\t"<<temp[i][1]<<"\t"<<temp[i][2]<<"\t"<<r[i]
[0]<<"\t"<<r[i][1]<<"\t"<<r[i][2]<<"\n";
m=m+0.1;
}
cout<<" 2.2 \t"<<temp[i][0]<<"\t"<<temp[i][1]<<"\t"<<temp[i][2]<<"\t"<<r[i][0]<<"\t"<<r[i]
[1]<<"\t"<<r[i][2]<<"\n";

cout<<"\nEnter the tuple (gender,height):";


cin>>ch>>h;
if(h<1.61)
j=0;
if(h>1.61 && h<1.71)
j=1;
if(h>1.71 && h<1.81)
j=2;
if(h>1.81 && h<1.91)
j=3;
if(h>1.91 && h<2.01)
j=4;
if(h>2.01)
j=5;
for(i=0;i<3;i++)
{
if(ch=='M')
ptt[i]=gen[0][i]*r[j][i];
if(ch=='F')
ptt[i]=gen[1][i]*r[j][i];
}
lss[0]=ptt[0]*ps;
lss[1]=ptt[1]*pm;
lss[2]=ptt[2]*pt;
fl=lss[0]+lss[1]+lss[2];
as=lss[0]/fl;
am=lss[1]/fl;
at=lss[2]/fl;
cout<<"\nP(t/S)="<<ptt[0]<<"\nP(t/M)="<<ptt[1]<<"\nP(t/T)="<<ptt[2];
cout<<"\n\nLikelyhood of being short="<<lss[0];
cout<<"\nLikelyhood of being medium="<<lss[1];
cout<<"\nLikelyhood of being tall="<<lss[2];
cout<<"\n\nTotal Likelyhood="<<fl;
cout<<"\n\nActual probability of being short="<<as;
cout<<"\nActual probability of being short="<<am;
cout<<"\nActual probability of being short="<<at;
getch();
}

/** output:

F 3 6 0 0.75 0.75 0

Height(below)
1.61 2 0 0 0.5 0 0
1.71 2 0 0 0.5 0 0
1.81 0 3 0 0 0.375 0
1.91 0 4 0 0 0.5 0
2.01 0 1 1 0 0.125 0.333333
2.2 0 0 2 0 0 0.666667

Enter the tuple (gender,height):M 2.2

P(t/S)=0
P(t/M)=0
P(t/T)=0.666667

Likelyhood of being short=0


Likelyhood of being medium=0
Likelyhood of being tall=0.133333

Total Likelyhood=0.133333

Actual probability of being short=0


Actual probability of being short=0
Actual probability of being short=1
**/
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int m,i,j,n,t,p,l,iflag,jflag;
int a[10][10];
char k[10],k1[10],k2[10];
cout<<"Enter the no. of tuples:";
cin>>n;
cout<<"Enter the name of the nodes:";
for(i=0;i<n;i++)
{
cin>>k[i];
}
cout<<"Enter the adjacent matrix:\n\n";
cout<<" ";
for(i=0;i<n;i++)
cout<<" "<<k[i];
cout<<endl;
for(i=0;i<n;i++)
{
cout<<endl<<k[i]<<" ";
for(j=0;j<n;j++)
cin>>a[i][j];
}
cout<<endl;
t=1;
while(1)
{
cout<<"** Threshold="<<t<<" **\n\n";
m=0;
iflag=1;
jflag=1;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(a[i][j]<=t && a[i][j]>0 && i<j)
{
cout<<k[i]<<" is connected to "<<k[j]<<" having distance of "<<a[i]
[j]<<endl;
for(l=0;l<m;l++)
{
if(k[i]==k1[l])
{
iflag=0;
break;
}
else
{
iflag=1;
}
}
for(l=0;l<m;l++)
{
if(k[j]==k1[l])
{
jflag=0;
break;
}
else
{
jflag=1;
}
}
if(iflag==1)
k1[m++]=k[i];
if(jflag==1)
k1[m++]=k[j];
}
}
p=0;
for(i=0;i<n;i++)
{
if(k[i]!=k1[i])
{
k2[p++]=k[i];
}
}
cout<<"k1={";
for(i=0;i<m;i++)
cout<<k1[i]<<",";
cout<<"}\n";
cout<<"k2={";
for(i=0;i<p;i++)
cout<<k2[i]<<",";
cout<<"}\n";
if(m==n)
break;
t++;
cout<<"\n\n";
}
getch();
}

/** output:
** Threshold=2 **

A is connected to B having distance of 1


A is connected to C having distance of 2
A is connected to D having distance of 2
B is connected to C having distance of 2
C is connected to D having distance of 1
k1={A,B,C,D,}
k2={E,}

** Threshold=3 **

A is connected to B having distance of 1


A is connected to C having distance of 2
A is connected to D having distance of 2
A is connected to E having distance of 3
B is connected to C having distance of 2
B is connected to E having distance of 3
C is connected to D having distance of 1
D is connected to E having distance of 3
k1={A,B,C,D,E,}
k2={}

*/

You might also like