PROGRAM: (K-Nearest Neighbour Algorithm)
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){
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
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)
{
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
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);
}
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);
}
{
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)
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