Beruflich Dokumente
Kultur Dokumente
Student
Name
Artificial Intelligence
Course Code:
7th
Batch:
Section:
Umer Sharif
Sp13-bcs-b
Date:
Maximum Marks:
Roll No.
Sp11-bcs-040
Assignment 3
Nave Bayes (Source Code)
import java.sql.*;
import java.util.*;
class NaiveBayes
{
public static void main(String args[])
{
try
{
/*
c1 and c2 denote class 1 & class 2.
class1 :- Customer plays yes
class2 :- Customer plays no
*/
20
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:ecompanyplay");
Statement s = con.createStatement();
String query = null;
ResultSet rs = null;
int c1=0 ,c2=0 ,n=0;
query ="SELECT
'yes') ";
s.execute(query);
rs= s.getResultSet();
if(rs.next())
//Count of cases when computer was bought n training set
c1=Integer.parseInt(rs.getString(1));
query ="SELECT
'no') ";
s.execute(query);
rs= s.getResultSet();
if(rs.next())
//Count of cases when computer was not bought in training set
c2=Integer.parseInt(rs.getString(1));
query = "SELECT
s.execute(query);
rs= s.getResultSet();
if(rs.next())
String age,income,student,credit_rating,class1;
System.out.println("Enter income:(low/medium/high)");
income = sc.next();
System.out.println("Enter student:(yes/no)");
student = sc.next();
System.out.println("Enter credit_rating:(fair/excellent)");
credit_rating = sc.next();
float pinc1=0,pinc2=0;
//pinc1 = probability of prediction to be class1 (will play)
//pinc2 = probability of prediction to be class2 (will not play)
pinc1 = pfind(age,income,student,credit_rating,"yes");
pinc2 = pfind(age,income,student,credit_rating,"no");
// compare pinc1 & pinc2 and predict the class that user will or won't buy
if(pinc1 > pinc2)
System.out.println("He will play");
else
System.out.println("He will not play");
s.close();
con.close();
}
catch(Exception e)
{
System.out.println("Exception:"+ e);
}
s.execute(query);
rs= s.getResultSet();
if(rs.next())
a=Integer.parseInt(rs.getString(1));
// a = count of values in training set having age , class same as passed in
argument
s.execute(query);
rs= s.getResultSet();
if(rs.next())
b=Integer.parseInt(rs.getString(1));
s.execute(query);
rs= s.getResultSet();
if(rs.next())
c=Integer.parseInt(rs.getString(1));
// c = count of values in training set having student , class same as passed in
argument
s.close();
con.close();
}
catch(Exception e)
{ System.out.println("Exception:"+ e);
} return ans;
}}
OUTPUT