Sie sind auf Seite 1von 7

COMSATS - Lancaster Dual Degree Programme

COMSATS Institute of Information Technology Lahore


Assignment 1 SPRING 2016
Course Title:
Course
Instructor/s:
Semester:
Time Allowed:

Student
Name

Artificial Intelligence

Course Code:

Dr. Wajahat Mahmood Qazi

Programme Name: BS Computer Science

7th

Batch:

Section:

Umer Sharif

CSC475 Credit Hours: 3(2,1)

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

COUNT(*) AS Expr1 FROM customer WHERE (class =

'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

COUNT(*) AS Expr1 FROM customer WHERE (class =

'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())

COUNT(*) AS Expr1 FROM customer ";

//Count of total cases in training set


n = Integer.parseInt(rs.getString(1));

float pc1 = (float)c1/n; //General probability for class c1


float pc2 = (float)c2/n; //General probability for class c2

System.out.println("c1= " +c1 +"\nc2="+c2+"\ntotal="+n);


System.out.println("p(c1)="+pc1);
System.out.println("p(c2)="+pc2);

Scanner sc = new Scanner(System.in);

String age,income,student,credit_rating,class1;

// Accept the parameter values for which class is to be predicted

System.out.println("Enter age: (youth/middle/senior)");


age = sc.next();

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");

pinc1 = pinc1 * pc1;


pinc2 = pinc2 * pc2;

// 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);
}

public static float pfind(String age,String income,String student,String credit_rating,String class1)


{
float ans = 0;
try{
Scanner sc = new Scanner(System.in);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:ecompanyplay");
Statement s = con.createStatement();
String query = null;
ResultSet rs = null;
int a=0 , b=0 , c=0 , d=0 , total=0;
query ="SELECT COUNT(*) AS Expr1
'"+ age + "' ) AND (class = '" +class1 +"') ";

FROM customer WHERE (age =

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

query ="SELECT COUNT(*) AS Expr1


( income = '"+ income + "' ) AND (class = '" +class1 +"') ";

FROM customer WHERE

s.execute(query);
rs= s.getResultSet();
if(rs.next())
b=Integer.parseInt(rs.getString(1));

// b = count of values in training set having income , class same as passed in


argument

query ="SELECT COUNT(*) AS Expr1


( student = '"+ student + "' ) AND (class = '" +class1 +"') ";

FROM customer WHERE

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

query ="SELECT COUNT(*) AS Expr1 FROM customer WHERE


( credit_rating = '"+ credit_rating + "' ) AND (class = '" +class1 +"')";
s.execute(query);
rs= s.getResultSet();
if(rs.next())
d=Integer.parseInt(rs.getString(1));
// d = count of values in training set having credit_rating , class same as passed in
argument
query ="SELECT COUNT(*) AS Expr1

FROM customer WHERE (class

= '" +class1 +"') ";


s.execute(query);
rs= s.getResultSet();
if(rs.next())
total=Integer.parseInt(rs.getString(1)); //total no resuults

ans = (float)a / (float)total * (float)b /(float)total * (float)c /(float)total *


(float)d /(float)total ;
//calculating total probability by naive bayes

s.close();
con.close();
}
catch(Exception e)
{ System.out.println("Exception:"+ e);
} return ans;

}}
OUTPUT

Enter outlook: (sunny/overcast/rain)


rain
Enter Temp:(hot/mild/cool)
mild
Enter Humidity:(high/normal)
normal
Enter Wind:(weak/strong)
weak
He will play

Das könnte Ihnen auch gefallen