0% found this document useful (0 votes)
48 views24 pages

PROGRAM: (K-Nearest Neighbour Algorithm)

The document describes an ID3 algorithm implementation in Java. It contains methods to calculate entropy of the training data, calculate information gain for each attribute, and find the attribute with maximum information gain. The entropy method calculates entropy of the target attribute. The atto method calculates information gain of a given attribute. It calls entropy and atto methods to find the attribute with maximum information gain and prints it. The code takes weather data with outlook, temperature, humidity attributes and a target play attribute to demonstrate the ID3 algorithm.

Uploaded by

RajaRam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views24 pages

PROGRAM: (K-Nearest Neighbour Algorithm)

The document describes an ID3 algorithm implementation in Java. It contains methods to calculate entropy of the training data, calculate information gain for each attribute, and find the attribute with maximum information gain. The entropy method calculates entropy of the target attribute. The atto method calculates information gain of a given attribute. It calls entropy and atto methods to find the attribute with maximum information gain and prints it. The code takes weather data with outlook, temperature, humidity attributes and a target play attribute to demonstrate the ID3 algorithm.

Uploaded by

RajaRam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 24

PROGRAM : (K- Nearest Neighbour Algorithm)

import java.util.ArrayList;
import java.util.HashMap;
public class NearestNeighbour{
public static void main(String[] args){
ArrayList<NearestNeighbour.DataEntry> data = new
ArrayList<NearestNeighbour.DataEntry>();
data.add(new DataEntry(new double[]{175,80}, "Male"));
data.add(new DataEntry(new double[]{193.5,110}, "Male"));
data.add(new DataEntry(new double[]{183,92.8}, "Male"));
data.add(new DataEntry(new double[]{160,60}, "Male"));
data.add(new DataEntry(new double[]{177,73.1}, "Male"));
data.add(new DataEntry(new double[]{175,80}, "Female"));
data.add(new DataEntry(new double[]{150,55}, "Female"));
data.add(new DataEntry(new double[]{159,63.2}, "Female"));
data.add(new DataEntry(new double[]{180,70}, "Female"));
data.add(new DataEntry(new double[]{163,110}, "Female"));
NearestNeighbour nn = new NearestNeighbour(data, 3); //3 neighbours
System.out.println("Classified as: "+nn.classify(new DataEntry(new
double[]{170, 60},"Ignore")));
}
private int k;
private ArrayList<Object> classes;
private ArrayList<DataEntry> dataSet;
public NearestNeighbour(ArrayList<DataEntry> dataSet, int k){
this.classes = new ArrayList<Object>();
this.k = k;
this.dataSet = dataSet;
for(DataEntry entry : dataSet){
if(!classes.contains(entry.getY())) classes.add(entry.getY());
}
}
private DataEntry[] getNearestNeighbourType(DataEntry x){
DataEntry[] retur = new DataEntry[this.k];
double fjernest = Double.MIN_VALUE;
int index = 0;
for(DataEntry tse : this.dataSet){
double distance = distance(x,tse);
if(retur[retur.length-1] == null){
int j = 0;
while(j < retur.length){
if(retur[j] == null){

retur[j] = tse; break;


}
j++;
}
if(distance > fjernest){
index = j;
fjernest = distance;
}
}
else{
if(distance < fjernest){
retur[index] = tse;
double f = 0.0;
int ind = 0;
for(int j = 0; j < retur.length; j++){
double dt = distance(retur[j],x);
if(dt > f){
f = dt;
ind = j;
}
}
fjernest = f;
index = ind;
}
}
}
return retur;
}
private static double convertDistance(double d){
return 1.0/d;
}
public static double distance(DataEntry a, DataEntry b){
double distance = 0.0;
int length = a.getX().length;
for(int i = 0; i < length; i++){
double t = a.getX()[i]-b.getX()[i];
distance = distance+t*t;
}
return Math.sqrt(distance);
}
public Object classify(DataEntry e){
HashMap<Object,Double> classcount = new
HashMap<Object,Double>();
DataEntry[] de = this.getNearestNeighbourType(e);
for(int i = 0; i < de.length; i++){

double distance =
NearestNeighbour.convertDistance(NearestNeighbour.distance(de[i], e));
if(!classcount.containsKey(de[i].getY())){
classcount.put(de[i].getY(), distance);
}
else{
classcount.put(de[i].getY(), classcount.get(de[i].getY())
+distance);
}
}
//Find right choice
Object o = null;
double max = 0;
for(Object ob : classcount.keySet()){
if(classcount.get(ob) > max){
max = classcount.get(ob);
o = ob;
}
}
return o;
}
public static class DataEntry{
private double[] x;
private Object y;
public DataEntry(double[] x, Object y){
this.x = x;
this.y = y;
}
public double[] getX(){
return this.x;
}
public Object getY(){
return this.y;
}
}
}

OUTPUT:
Classified as: Female

PROGRAM: (BINNING)
package sample;
import java.util.Scanner;
public class Sample {
public static void binning(int a[], int m,int n)
{
for(int i=m+1;i<n;i++)
{
int x=Math.abs(a[m]-a[i]);
int y=Math.abs(a[n]-a[i]);
if(x<y)
{
a[i]=a[m];
}
else
a[i]=a[n];
}
System.out.println("Bins:");
for(int i=m;i<=n;i++)
System.out.println(a[i]);
}
public static void main(String[] args) {
int a,b;
Scanner in=new Scanner(System.in);
System.out.println("Enter n:");
int n=in.nextInt();
System.out.println("Enter boundaries:");
int temp[]=new int [n];
for(int i=0;i<n;i++)
temp[i]=in.nextInt();
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(temp[i]>temp[j])
{
int t=temp[i];
temp[i]=temp[j];
temp[j]=t;
}
}
}
System.out.println("Enter bins");
int bin=in.nextInt();

for(int i=0;i<n;)
{
int t=i+bin-1;
binning(temp,i,t);
i=t+1;
}
}
}

OUTPUT
Enter n:
6
Enter boundaries:
3
5
6
7
8
9
Enter bins
6
Bins:
3
3
9
9
9
9

PROGRAM: (Apriori Algorithm)


import java.util.*;
class Apriori
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
Apriori x=new Apriori();
Vector v=new Vector(), temp=new Vector(), prev=new Vector();
System.out.println("Enter no. of items:");
int p=1, n=sc.nextInt();
System.out.println("Enter no. of transactions:");
int t=sc.nextInt();
String s[]=new String[t];
for(int i=0;i<t;i++)
{
System.out.print("Transaction "+(i+1)+": ");
s[i]=sc.next();
}
System.out.println("Enter value for minimum support: ");
int minSupp=sc.nextInt();
System.out.println("\nInitial count:\nItem\tCount");
for(int i=0;i<n;i++)
{
System.out.println((i+1)+"\t"+x.count(s, (i+1)+""));
if(x.count(s, (i+1)+"")>=minSupp)
v.add((i+1)+"");
temp.add(i+"");
}
System.out.println("\nFrequent "+(p++)+" itemsets: "+v);
do{
temp.removeAllElements();
prev.removeAllElements();
for(int i=0;i<v.size();i++)
prev.add(v.elementAt(i));
for(int i=0;i<v.size();i++)

for(int j=i+1;j<v.size();j++)
{
String w= v.elementAt(i).toString();
String str=w+v.elementAt(j);
if(x.count(s, str)>=minSupp)
temp.add(x.removeDuplicates(str));
}
v.removeAllElements();
for(int i=0;i<temp.size();i++)
v.add(temp.elementAt(i));
x.removeRepeated(v);
v = new Vector(new LinkedHashSet(v));
if(!v.isEmpty())
System.out.println("Frequent "+(p++)+" itemsets: "+v);
}while(!v.isEmpty());
}
int count(String[] a, String s)
{
int count=0;
for(int i=0;i<a.length;i++)
if(intersect(s, a[i]))
count++;
return count;
}
boolean intersect(String s1, String s2)
{
boolean b=true;
for(int i=0;i<s1.length();i++)
if(s2.indexOf(s1.charAt(i)+"")==-1)
{
b=false;
break;
}
return b;
}
String removeDuplicates(String s)
{

StringBuilder noDupes = new StringBuilder();


for (int i = 0; i < s.length(); i++)
{
String si = s.substring(i, i+1);
if (!si.equals(",") && noDupes.indexOf(si) == -1)
noDupes.append(si);
}
return noDupes.toString();
}
Vector removeRepeated(Vector vect)
{
for(int i=0;i<vect.size();i++)
{
String r=vect.elementAt(i).toString();
char[] chars = r.toCharArray();
Arrays.sort(chars);
vect.removeElementAt(i);
vect.add(i, new String(chars));
}
return vect;
}
}

OUTPUT:
Enter no. of items:
5
Enter no. of transactions:
9
Transaction 1: 123
Transaction 2: 23
Transaction 3: 24
Transaction 4: 124
Transaction 5: 13
Transaction 6: 23
Transaction 7: 13
Transaction 8: 123
Transaction 9: 1234
Enter value for minimum support:
2
Initial count:
Item

Count

Frequent 1 itemsets: [1, 2, 3, 4]


Frequent 2 itemsets: [12, 13, 14, 23, 24]
Frequent 3 itemsets: [123, 124]

PROGRAM:
import java.io.*;
import java.math.*;
class Bayes
{
BufferedReader sc=new BufferedReader(new InputStreamReader(System.in));
int i;
double y1,y2;
double c=0.0,c1,c3=0.0,c4,c5=0.0,c6;
String a[][]= {{"r","m","n","y"}, {"s","m","n","y"},{"o","m","h","y"},
{"o","h","n","y"},
{"s","m","h","n"},{"r","m","h","n"}};
void start1() throws Exception
{
System.out.println("Enter the conditions to see whether can play or not");
System.out.println("Enter outlook : ");
String x1=sc.readLine();
System.out.println("Enter temperature : ");
String x2=sc.readLine();
System.out.println("Enter humidity : ");
String x3=sc.readLine();
entropy();
double a1=atto(x1,0);
System.out.println("P("+x1+"/y) = " +a1);
double a2=atto(x2,1);
System.out.println("P("+x2+"/y) = " +a2);
double a3=atto(x3,2);
System.out.println("P("+x3+"/y) = " +a3);
double b1=atto1(x1,0);
System.out.println("P("+x1+"/n) = " +b1);
double b2=atto1(x2,1);
System.out.println("P("+x2+"/n) = " +b2);
double b3=atto1(x3,2);
System.out.println("P("+x3+"/n) = " +b3);
double p1=(a1*a2*a3);
System.out.println("P(X/y) = " +p1);

double p2=(b1*b2*b3);
System.out.println("P(X/n) = " +p2);
p1=(p1*y1);
System.out.println("P(y/X) = " +p1);
p2=(p2*y2);
System.out.println("P(n/X) = " +p2);
if(p1>p2)
{
System.out.println("You Can Play");
}
else
{
System.out.println("You Can't Play");
}
}
void entropy()
{
for(i=0;i<6;i++)
{
if(a[i][3].equals("y"))
c++;
}
c1=6-c;
y1=(c/6.0);
y2=(c1/6.0);
System.out.println("P(y):"+ y1);
System.out.println("P(n):"+ y2);
}
double atto(String at,int z)
{
c3=0.0;
for(i=0;i<6;i++)
{
if(a[i][z].equals(at))
{
if(a[i][3].equals("y"))
c3++;

}
}
return((c3/c));
}
double atto1(String at,int z)
{
c4=0.0;
for(i=0;i<6;i++)
{
if(a[i][z].equals(at))
{
if(a[i][3].equals("n"))
c4++;
}
}
return((c4/c1));
}
}
class Bayesmain
{
public static void main(String args[]) throws Exception
{
Bayes d=new Bayes();
d.start1();
}
}

OUTPUT:
Enter the conditions to see whether can play or not
Enter outlook :
r
Enter temperature :
m
Enter humidity :
h
P(y):0.6666666666666666
P(n):0.3333333333333333
P(r/y) = 0.25
P(m/y) = 0.75
P(h/y) = 0.25
P(r/n) = 0.5
P(m/n) = 1.0
P(h/n) = 1.0
P(X/y) = 0.046875
P(X/n) = 0.5
P(y/X) = 0.03125
P(n/X) = 0.16666666666666666
You Can't Play

PROGRAM: (ID3)
import java.util.*;
class ID3
{
int i;
double c=0.0,c1,c3=0.0,c4,c5=0.0,c6;;
char a[][]={{'r','m','n','y'},
{'s','m','n','y'},
{'o','m','h','y'},
{'o','h','n','y'},
{'s','m','h','n'},
{'r','m','h','n'}};

void display()
{
System.out.println("Outlook

Temperature

for(int l=0;l<6;l++)
{
for(int j=0;j<4;j++)
{
System.out.print(a[l][j]+"\t\t");
}
System.out.println();
}
}

Humidity

Play");

double entropy()
{
for(i=0;i<6;i++)
{
if(a[i][3]=='y')
c++;
}
c1=6-c;

double i1=(c/6.0)*((Math.log(c/6.0))/(Math.log(2)));
double i2=(c1/6.0)*((Math.log(c1/6.0))/(Math.log(2)));
double id=-(i1+i2);
System.out.println("Entropy:"+id);
return(id);
}

double atto(char at,int z)


{
c3=0.0;
c6=0.0;
c5=0.0;
for(i=0;i<6;i++)
{
if(a[i][z]==at)
{
c3++;
if(a[i][3]=='y')

c5++;
}
}

c6=c3-c5;

if(c5==0)
c5=c3;

if(c6==0)
c6=c3;

double r1=(c5/c3)*((Math.log(c5/c3))/(Math.log(2)));
double r2=(c6/c3)*((Math.log(c6/c3))/(Math.log(2)));
double r=-(c3/6.0)*(r1+r2);
return(r);
}

void max(double outlook,double temperature,double hum)


{
if(hum>temperature)
{
if(hum>outlook)
{
System.out.println("max is humidity:"+hum);
}
else

{
System.out.println("max is outlook"+outlook);
}
}
else
{
System.out.println("max is temperature:"+temperature);
}
}
}

class ID3main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
ID3 d=new ID3();
d.display();
double id=d.entropy();
double a1=d.atto('r',0);
double a2=d.atto('s',0);
double a3=d.atto('o',0);
double outlook=id-(a1+a2+a3);
System.out.println("outlook"+outlook);
double b1=d.atto('h',1);

double b2=d.atto('m',1);
double temperature=id-(b1+b2);
System.out.println("temperature:"+temperature);
double c1=d.atto('n',2);
double c2=d.atto('h',2);
double humidity=id-(c1+c2);
System.out.println("humidity:"+humidity);
d.max(outlook,temperature,humidity);
}
}

OUTPUT:
Outlook

Temperature

Humidity

Play

Entropy:0.9182958340544896
outlook0.2516291673878229
temperature:0.109170338675599
humidity:0.4591479170272448
max is humidity:0.4591479170272448
BUILD SUCCESSFUL (total time: 0 seconds)

PROGRAM: (K-Means Clustering)


import java.io.*;
import java.util.*;
class cluster
{
public static void main (String args[])throws IOException
{
DataInputStream in= new DataInputStream(System.in);
float m1,m2,q=0.0f,w=0.0f;
int k_means;
int z=0,i=0;
int z1=0;
float avg1,avg2;
System.out.println(Enter no of elements in cluster);
k_means=Integer.parseInt(in.readLine());
int a[]=new int[k_means];
int c1[]=new int[10];
int c2[]=new int[10];
System.out.println(Enter elements in cluster);
for(i=0;i<k_means;i++)
{
a[i]=Integer.parseInt(in.readLine());
}
System.out.println(Enter value of m1 and m2);
m1=Integer.parseInt(in.readLine());
m2=Integer.parseInt(in.readLine());
operations op=new operations();
while(q!=m2&&w!=m2)
{
for(i=0;i<k_means;i++)
{
if(Math.abs(a[i]-m1)<Math.abs(a[i]-m2))
{
c1[z]=a[i];
z++;
}
else
{
c2[z1]=a[i];
z1++;
}
}
z=0;z1=0;
System.out.print(Cluster 1\t);
op.display(c1,k_means);
System.out.print(Cluster 2\t);
op.display(c2,k_means);

q=m1;
w=m2;
m1=op.average(c1,k_means);
System.out.print(average of cluster1 +m1);
System.out.println();
m2=op.average(c2,k_means);
System.out.print(average of cluster2 +m2);
System.out.println();
}
}}
class operations
{
void display(int c[],int k_means)
{
for(int i=0;i<k_means;i++)
{
if(c[i]!=0)
{
System.out.print(c[i]+ );
}}
System.out.println();
}
float average(int c[],int k_means)
{
int count=0,sum=0;
float avg;
for(int i=0;i<k_means;i++)
{
if(c[i]!=0)
{
sum+=c[i];
count++;
}}
avg=(float)sum/count;
return(avg);
}}

OUTPUT:
Enter no of elements in cluster
10
Enter elements in cluster
1
6
4
5
8
9
25
35
4
56
Enter value of m1 and m2
10
11
Cluster 1 1 6 4 5 8 9 4
Cluster 2 25 35 56
average of cluster1 5.285714
average of cluster2 38.666668
Cluster 1 1 6 4 5 8 9 4
Cluster 2 25 35 56
average of cluster1 5.285714
average of cluster2 38.666668

You might also like