PROGRAM : (K- Nearest Neighbour Algorithm)
import [Link];
import [Link];
public class NearestNeighbour{
public static void main(String[] args){
ArrayList<[Link]> data = new
ArrayList<[Link]>();
[Link](new DataEntry(new double[]{175,80}, "Male"));
[Link](new DataEntry(new double[]{193.5,110}, "Male"));
[Link](new DataEntry(new double[]{183,92.8}, "Male"));
[Link](new DataEntry(new double[]{160,60}, "Male"));
[Link](new DataEntry(new double[]{177,73.1}, "Male"));
[Link](new DataEntry(new double[]{175,80}, "Female"));
[Link](new DataEntry(new double[]{150,55}, "Female"));
[Link](new DataEntry(new double[]{159,63.2}, "Female"));
[Link](new DataEntry(new double[]{180,70}, "Female"));
[Link](new DataEntry(new double[]{163,110}, "Female"));
NearestNeighbour nn = new NearestNeighbour(data, 3); //3 neighbours
[Link]("Classified as: "+[Link](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){
[Link] = new ArrayList<Object>();
this.k = k;
[Link] = dataSet;
for(DataEntry entry : dataSet){
if()) [Link]([Link]());
}
}
private DataEntry[] getNearestNeighbourType(DataEntry x){
DataEntry[] retur = new DataEntry[this.k];
double fjernest = Double.MIN_VALUE;
int index = 0;
for(DataEntry tse : [Link]){
double distance = distance(x,tse);
if(retur[[Link]-1] == null){
int j = 0;
while(j < [Link]){
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 < [Link]; 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 = [Link]().length;
for(int i = 0; i < length; i++){
double t = [Link]()[i]-[Link]()[i];
distance = distance+t*t;
}
return [Link](distance);
}
public Object classify(DataEntry e){
HashMap<Object,Double> classcount = new
HashMap<Object,Double>();
DataEntry[] de = [Link](e);
for(int i = 0; i < [Link]; i++){
double distance =
[Link]([Link](de[i], e));
if()){
[Link](de[i].getY(), distance);
}
else{
[Link](de[i].getY(), [Link](de[i].getY())
+distance);
}
}
//Find right choice
Object o = null;
double max = 0;
for(Object ob : [Link]()){
if([Link](ob) > max){
max = [Link](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 [Link];
public class Sample {
public static void binning(int a[], int m,int n)
{
for(int i=m+1;i<n;i++)
{
int x=[Link](a[m]-a[i]);
int y=[Link](a[n]-a[i]);
if(x<y)
{
a[i]=a[m];
}
else
a[i]=a[n];
}
[Link]("Bins:");
for(int i=m;i<=n;i++)
[Link](a[i]);
}
public static void main(String[] args) {
int a,b;
Scanner in=new Scanner([Link]);
[Link]("Enter n:");
int n=[Link]();
[Link]("Enter boundaries:");
int temp[]=new int [n];
for(int i=0;i<n;i++)
temp[i]=[Link]();
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;
}
}
}
[Link]("Enter bins");
int bin=[Link]();
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 [Link].*;
class Apriori
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
Apriori x=new Apriori();
Vector v=new Vector(), temp=new Vector(), prev=new Vector();
[Link]("Enter no. of items:");
int p=1, n=[Link]();
[Link]("Enter no. of transactions:");
int t=[Link]();
String s[]=new String[t];
for(int i=0;i<t;i++)
{
[Link]("Transaction "+(i+1)+": ");
s[i]=[Link]();
}
[Link]("Enter value for minimum support: ");
int minSupp=[Link]();
[Link]("\nInitial count:\nItem\tCount");
for(int i=0;i<n;i++)
{
[Link]((i+1)+"\t"+[Link](s, (i+1)+""));
if([Link](s, (i+1)+"")>=minSupp)
[Link]((i+1)+"");
[Link](i+"");
}
[Link]("\nFrequent "+(p++)+" itemsets: "+v);
do{
[Link]();
[Link]();
for(int i=0;i<[Link]();i++)
[Link]([Link](i));
for(int i=0;i<[Link]();i++)
for(int j=i+1;j<[Link]();j++)
{
String w= [Link](i).toString();
String str=w+[Link](j);
if([Link](s, str)>=minSupp)
[Link]([Link](str));
}
[Link]();
for(int i=0;i<[Link]();i++)
[Link]([Link](i));
[Link](v);
v = new Vector(new LinkedHashSet(v));
if(![Link]())
[Link]("Frequent "+(p++)+" itemsets: "+v);
}while(![Link]());
}
int count(String[] a, String s)
{
int count=0;
for(int i=0;i<[Link];i++)
if(intersect(s, a[i]))
count++;
return count;
}
boolean intersect(String s1, String s2)
{
boolean b=true;
for(int i=0;i<[Link]();i++)
if([Link]([Link](i)+"")==-1)
{
b=false;
break;
}
return b;
}
String removeDuplicates(String s)
{
StringBuilder noDupes = new StringBuilder();
for (int i = 0; i < [Link](); i++)
{
String si = [Link](i, i+1);
if ( && [Link](si) == -1)
[Link](si);
}
return [Link]();
}
Vector removeRepeated(Vector vect)
{
for(int i=0;i<[Link]();i++)
{
String r=[Link](i).toString();
char[] chars = [Link]();
[Link](chars);
[Link](i);
[Link](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 [Link].*;
import [Link].*;
class Bayes
{
BufferedReader sc=new BufferedReader(new InputStreamReader([Link]));
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
{
[Link]("Enter the conditions to see whether can play or not");
[Link]("Enter outlook : ");
String x1=[Link]();
[Link]("Enter temperature : ");
String x2=[Link]();
[Link]("Enter humidity : ");
String x3=[Link]();
entropy();
double a1=atto(x1,0);
[Link]("P("+x1+"/y) = " +a1);
double a2=atto(x2,1);
[Link]("P("+x2+"/y) = " +a2);
double a3=atto(x3,2);
[Link]("P("+x3+"/y) = " +a3);
double b1=atto1(x1,0);
[Link]("P("+x1+"/n) = " +b1);
double b2=atto1(x2,1);
[Link]("P("+x2+"/n) = " +b2);
double b3=atto1(x3,2);
[Link]("P("+x3+"/n) = " +b3);
double p1=(a1*a2*a3);
[Link]("P(X/y) = " +p1);
double p2=(b1*b2*b3);
[Link]("P(X/n) = " +p2);
p1=(p1*y1);
[Link]("P(y/X) = " +p1);
p2=(p2*y2);
[Link]("P(n/X) = " +p2);
if(p1>p2)
{
[Link]("You Can Play");
}
else
{
[Link]("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);
[Link]("P(y):"+ y1);
[Link]("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 [Link].*;
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()
{
[Link]("Outlook
Temperature
for(int l=0;l<6;l++)
{
for(int j=0;j<4;j++)
{
[Link](a[l][j]+"\t\t");
}
[Link]();
}
}
Humidity
Play");
double entropy()
{
for(i=0;i<6;i++)
{
if(a[i][3]=='y')
c++;
}
c1=6-c;
double i1=(c/6.0)*(([Link](c/6.0))/([Link](2)));
double i2=(c1/6.0)*(([Link](c1/6.0))/([Link](2)));
double id=-(i1+i2);
[Link]("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)*(([Link](c5/c3))/([Link](2)));
double r2=(c6/c3)*(([Link](c6/c3))/([Link](2)));
double r=-(c3/6.0)*(r1+r2);
return(r);
}
void max(double outlook,double temperature,double hum)
{
if(hum>temperature)
{
if(hum>outlook)
{
[Link]("max is humidity:"+hum);
}
else
{
[Link]("max is outlook"+outlook);
}
}
else
{
[Link]("max is temperature:"+temperature);
}
}
}
class ID3main
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
ID3 d=new ID3();
[Link]();
double id=[Link]();
double a1=[Link]('r',0);
double a2=[Link]('s',0);
double a3=[Link]('o',0);
double outlook=id-(a1+a2+a3);
[Link]("outlook"+outlook);
double b1=[Link]('h',1);
double b2=[Link]('m',1);
double temperature=id-(b1+b2);
[Link]("temperature:"+temperature);
double c1=[Link]('n',2);
double c2=[Link]('h',2);
double humidity=id-(c1+c2);
[Link]("humidity:"+humidity);
[Link](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 [Link].*;
import [Link].*;
class cluster
{
public static void main (String args[])throws IOException
{
DataInputStream in= new DataInputStream([Link]);
float m1,m2,q=0.0f,w=0.0f;
int k_means;
int z=0,i=0;
int z1=0;
float avg1,avg2;
[Link](Enter no of elements in cluster);
k_means=[Link]([Link]());
int a[]=new int[k_means];
int c1[]=new int[10];
int c2[]=new int[10];
[Link](Enter elements in cluster);
for(i=0;i<k_means;i++)
{
a[i]=[Link]([Link]());
}
[Link](Enter value of m1 and m2);
m1=[Link]([Link]());
m2=[Link]([Link]());
operations op=new operations();
while(q!=m2&&w!=m2)
{
for(i=0;i<k_means;i++)
{
if([Link](a[i]-m1)<[Link](a[i]-m2))
{
c1[z]=a[i];
z++;
}
else
{
c2[z1]=a[i];
z1++;
}
}
z=0;z1=0;
[Link](Cluster 1\t);
[Link](c1,k_means);
[Link](Cluster 2\t);
[Link](c2,k_means);
q=m1;
w=m2;
m1=[Link](c1,k_means);
[Link](average of cluster1 +m1);
[Link]();
m2=[Link](c2,k_means);
[Link](average of cluster2 +m2);
[Link]();
}
}}
class operations
{
void display(int c[],int k_means)
{
for(int i=0;i<k_means;i++)
{
if(c[i]!=0)
{
[Link](c[i]+ );
}}
[Link]();
}
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