Created
June 2, 2017 08:14
-
-
Save tomrockdsouza/0a64d80ba293d9652e96b2d22c28411a to your computer and use it in GitHub Desktop.
Java implementation of the Naive-Bayes algorithm ( Play-Tennis Example )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** The class encapsulates an implementation of the Naive-Bayes algorithm | |
* | |
* First argument is for Outlook | |
* Sunny=2 Overcast=1 Rain=0 | |
* | |
* Second argument is for Temperature | |
* Hot=2 Mild=1 Cool=0 | |
* | |
* Third argument is for Humidity | |
* High=1 Normal=0 | |
* | |
* Fourth argument is for Wind Strength | |
* Strong=1 Weak=0 | |
* | |
* | |
* Usage with the command line : | |
* For <rain, hot, high, false> | |
* >java Naive 0 2 1 0 | |
* | |
* | |
* @author Tomrock D'souza, St. Francis Institute Of Technology, University of Mumbai, 2017 | |
* @copyright GNU General Public License v3 | |
* No reproduction in whole or part without maintaining this copyright notice | |
* and imposing this condition on any subsequent users. | |
*/ | |
public class Naive{ | |
public static void main(String args[]) throws Exception { | |
int i; | |
if(args.length!=4) | |
{ | |
System.out.println("Wrong number of Arguments, Need :4"); | |
System.exit(0); | |
} | |
int[] argsi=new int[4]; | |
for(i=0;i<4;i++) | |
{ | |
argsi[i]=Integer.parseInt(args[i]); | |
} | |
int p=0,n=0,m; | |
double winProbablity,loseProbablity; | |
int[] outLook={0,0,0,0,0,0}; | |
int[] temp={0,0,0,0,0,0}; | |
int[] humid={0,0,0,0}; | |
int[] wind={0,0,0,0}; | |
int[][] multi = new int[][]{ | |
/* | |
* This is the Training Set Made up of 14 rows and 5 columns Respectively | |
* | |
* First column is for Outlook | |
* Sunny=2 Overcast=1 Rain=0 | |
* | |
* Second column is for Temperature | |
* Hot=2 Mild=1 Cool=0 | |
* | |
* Third column is for Humidity | |
* High=1 Normal=0 | |
* | |
* Fourth column is for Wind Strength | |
* Strong=1 Weak=0 | |
* | |
* Fifth column is for Outcome | |
* Play=1 Not-Play=0 | |
* | |
*/ | |
{2,2,1,0,0}, | |
{2,2,1,1,0}, | |
{1,2,1,0,1}, | |
{0,1,1,0,1}, | |
{0,0,0,0,1}, | |
{0,0,0,1,0}, | |
{1,0,0,1,1}, | |
{2,1,1,0,0}, | |
{2,0,0,0,1}, | |
{0,1,0,0,1}, | |
{2,1,0,1,1}, | |
{1,1,1,1,1}, | |
{1,2,0,0,1}, | |
{0,1,1,1,0} | |
}; | |
m=multi.length; | |
//This For loop Uses The Training Set to Generate Probabilities | |
for(i=0;i<m;i++){ | |
if(multi[i][0]==2) | |
{if(multi[i][4]==1){outLook[5]++;}else{outLook[4]++;}} | |
else if(multi[i][0]==1) | |
{if(multi[i][4]==1){outLook[3]++;}else{outLook[2]++;}} | |
else{if(multi[i][4]==1){outLook[1]++;}else{outLook[0]++;}} | |
if(multi[i][1]==2) | |
{if(multi[i][4]==1){temp[5]++;}else{temp[4]++;}} | |
else if(multi[i][1]==1) | |
{if(multi[i][4]==1){temp[3]++;}else{temp[2]++;}} | |
else{if(multi[i][4]==1){temp[1]++;}else{temp[0]++;}} | |
if(multi[i][2]==1) | |
{if(multi[i][4]==1){humid[3]++;}else{humid[2]++;}} | |
else | |
{if(multi[i][4]==1){humid[1]++;}else{humid[0]++;}} | |
if(multi[i][3]==1) | |
{if(multi[i][4]==1){wind[3]++;p++;}else{wind[2]++;n++;}} | |
else | |
{if(multi[i][4]==1){wind[1]++;p++;}else{wind[0]++;n++;}} | |
} | |
//This Piece of Code Displays the Data After Training | |
for(i=0;i<outLook.length;i+=2){ | |
System.out.println("Variable Outlook["+i/2+"]: "+outLook[i+1]+"/"+p+" "+outLook[i]+"/"+n+"\n"); | |
} | |
for(i=0;i<temp.length;i+=2){ | |
System.out.println("Variable Temperate["+i/2+"]: "+temp[i+1]+"/"+p+" "+temp[i]+"/"+n+"\n"); | |
} | |
for(i=0;i<humid.length;i+=2){ | |
System.out.println("Variable Humidity["+i/2+"]: "+humid[i+1]+"/"+p+" "+humid[i]+"/"+n+"\n"); | |
} | |
for(i=0;i<wind.length;i+=2){ | |
System.out.println("Variable Windy["+i/2+"]: "+wind[i+1]+"/"+p+" "+wind[i]+"/"+n+"\n"); | |
} | |
System.out.println("P(n):"+n+" P(p):"+p+"\nTotal Rows Parsed:"+m+"\n"); | |
winProbablity=(double)(outLook[argsi[0]*2+1]*temp[argsi[1]*2+1]*humid[argsi[2]*2+1]*wind[argsi[3]*2+1])/(p*p*p*m); | |
loseProbablity=(double)(outLook[argsi[0]*2]*temp[argsi[1]*2]*humid[argsi[2]*2]*wind[argsi[3]*2])/(n*n*n*m); | |
System.out.printf("P(p|X)= %f\nP(n|X)= %f\n",winProbablity,loseProbablity); | |
if(winProbablity<loseProbablity) {System.out.println("Don't Play");} | |
else {System.out.println("Must Play");} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment