0% found this document useful (0 votes)
65 views7 pages

Assignment 3: Naïve Bayes (Source Code)

This document describes a dual degree program between COMSATS Institute of Information Technology Lahore and Lancaster University. The program allows students to earn a bachelor's degree from both universities over the course of their studies. Some key details include that the program is for students in the computer science program, it is offered in the spring 2016 semester, and students take courses from instructors at both institutions to complete the dual degree.

Uploaded by

Umer Chaudary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views7 pages

Assignment 3: Naïve Bayes (Source Code)

This document describes a dual degree program between COMSATS Institute of Information Technology Lahore and Lancaster University. The program allows students to earn a bachelor's degree from both universities over the course of their studies. Some key details include that the program is for students in the computer science program, it is offered in the spring 2016 semester, and students take courses from instructors at both institutions to complete the dual degree.

Uploaded by

Umer Chaudary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy