183 views

Uploaded by sagar

- Incremental Discretization for Naive Bayes Learning using FIFFD
- Benchmarking Sampling Techniques for Imbalance Learning in Churn Prediction
- Comerma 2010 Land Capability Suitability and Vocation in Venezuela
- Weka
- bachneragisc9216d2
- Presentation
- IDRISI Selva GIS Image Processing Specifications
- A Bayesian Approach to Measurement Bias in Networking Studies
- SCProj.pdf
- Edward Greenberg - Introduction to Bayesian Econometrics-Cambridge University Press (2007).pdf
- Probability Theory
- l 13 Naive Bayes Classifier
- chopra-05.pdf
- 1812.10889
- 1. Image Processing c9a262d58ba876b3be2e1a0472a2dc799277
- 7Vol59No2.pdf
- myw9_report_201212130613.pdf
- 3+scki+kit+Machine+learning
- Exam DUT 070816 Ans
- sensors-14-18837

You are on page 1of 9

Aim

Implement Naive Bayes to predict the work type for a person.

Pre-requisite

1. Probability.

2. Scikit-Learn Python Library.

3. Programming language basics.

Objective

1. To Learn basic concepts of Naive Bayes Classifier.

2. To implement application using Naive Bayes Classifier for given problem.

Problem Statement

Implement Naive Bayes to predict the work type for a person with following parameters:

age: 30, Qualification: MTech, Experience: 8

Following table provides the details of the available data:

Work Type

Consultancy

Service

Research

Service

Consultancy

Research

Research

Service

Consultancy

Research

Age

30

21

26

28

40

35

27

32

45

36

Qualification

Ph.D.

MTech.

MTech.

BTech.

MTech.

Ph.D.

Btech.

MTech.

Btech.

Ph.D.

Experience

9

1

2

10

14

10

6

9

17

7

1. Python

2. Scikit-Learn Library

Mathematical Model

M = { s, e, X, Y, DD, NDD, Fme , M emshared , success, f ailure, CP UCoreCount }

2. e = End State - Work type predicted.

3. X = Input - Given Dataset.

4. Y = Output - Prediction of Work type for given problem.

5. DD = Deterministic Data - Work Type, Age, Qualification, Experience.

6. NDD = Non-Deterministic Data - Truncation or rounding errors in calculations.

7. Fme = GaussianNB(), fit(input,output), predict([30,2,8]).

8. Memshared = No shared Memory is used.

9. Success = Work type predicted.

10. Failure = Work type not predicted.

11. CPUCoreCount = 1.

Theory

The objective of classification is to analyze the input data and to develop an accurate description or model for each class using the features present in the data. This model is used to

classify test data for which the class descriptions are not known. The input data, also called

the training set, consists of multiple records each having multiple attributes or features.Each

record is tagged with a class label.

The data analysis task is called as classification, where a model or classifier is constructed to

predict categorical labels. (e.g. safe or risky, yes or no)

Data classification is a two-step process 1. Learning 2. Classification

1. Learning: In this step, a classifier is built describing a predetermined set of data classes or

concepts. This first step of the classification process can also be viewed as the learning of a

mapping or function, y = f(X), that can predict the associated class label y of a given tuple X.

This mapping is represented in the form of classification rules, decision trees or mathematical

formula.

2. Classification: Model created in previous steps are used for classification. Test data are used

to estimate the accuracy of the classification rules. If the accuracy is considered acceptable,

the rules can be applied to the classification of new data tuples.

Bayesian classifiers are statistical classifiers. They can predict class membership probabilities,

such as the probability that a given tuple belongs to a particular class.Bayesian classification is

based on Bayes theorem, Bayesian classifiers have also exhibited high accuracy and speed when

applied to large databases. Bayesian classifiers assume that the effect of an attribute value on

a given class is independent of the values of the other attributes. This assumption is called

class conditional independence. It is made to simplify the computations involved and, in this

sense, is considered naive. (Hence it is Bayesian classifier is also called as Naive Bayes Classifier)

Bayes theorem

Bayes theorem is named after Thomas Bayes, who did early work in probability and decision

theory during the 18th century. Let X be a data tuple. In Bayesian terms, X is considered

evidence.

Let H be some hypothesis, such as that the data tuple X belongs to a specified class C.

For classification problems, we want to determine P(H|X), the probability that the hypothesis

H holds given the evidence or observed data tuple X.

In other words, we are looking for the probability that tuple X belongs to class C, given that

we know the attribute description of X.

1 P(H) is the posterior probability, or a posteriori probability, of H conditioned on X.

i.e. P(H|X) : Probability of H given X

P(H|X) reflects the probability that customer X will buy a computer given that we know

the customers age and income. 2) P(X|H) is the posterior probability of X conditioned on H.

i.e P(X|H) : Probability of X given H

That is, it is the probability that customers, X, is 35 years old and earns $40,000, given

that we know the customer will buy a computer.

3) P(H) is the prior probability, or a priori probability, of H.

i.e. P(H) : Prior probability of hypothesis H

For example, this is the probability that any given customer will buy a computer, regardless

of age, income, or any other information, for that matter.

4) P(X) is the prior probability of X.

i.e. P(X) : Prior probability of data tuple X

For example, it is the probability that a person from our set of customers is 35 years old

and earns $40,000.

Bayes theorem is useful in that it provides a way of calculating the posterior probability,

P(HX), from P(H), P(XH), and P(X). Bayes theorem is given below:

P (H|X)=P(X |H)P(H)/P(X)

Bayesian classifier

The nave Bayesian classifier, or simple Bayesian classifier, works as follows:

1) Let D be a training set of tuples and their associated class labels, and each tuple is represented by an n-D attribute vector X = (x1, x2, , xn)

2) Suppose there are m classes C1, C2, , Cm. Given a tuple, X, the classifier will predict that

X belongs to the class having the highest posterior probability, conditioned on X. That is, the

nave Bayesian classifier predicts that tuple X belongs to the class Ci if and only if,

Thus we maximize P(Ci X). The class Ci for which P(Ci X) is maximized is called the

maximum posteriori hypothesis. This can be derived from Bayes theorem :

As P(X) is constant for all classes, only P(XCi)*P(Ci ) need be maximized. If the class prior

probabilities are not known, then it is commonly assumed that the classes are equally likely,

that is, P(C1 ) = P(C2 ) = = P(Cm ), and we would therefore maximize P(X| Ci ). Otherwise, we maximize P(X| Ci)*P(Ci ).

4) Given data sets with many attributes, it would be extremely computationally expensive to

compute P(X| Ci ). Thus,

Here xk refers to the value of attribute Ak for tuple X. For each attribute, we look at whether

the attribute is categorical or continuous-valued.

For instance, to compute P(X|Ci ), we consider the following:

a) If Ak is categorical, then P(xk |Ci ) is the number of tuples of class Ci in D having the value

xk for Ak , divided by Ci,D , the number of tuples of class Ci in D.

b) If Ak is continuous-valued, then A continuous-valued attribute is typically assumed to have

a Gaussian distribution with a mean and standard deviation , defined by

5) In order to predict the class label of X, P(X| Ci )P(Ci ) is evaluated for each class Ci . The

classifier predicts that the class label of tuple X is the class Ci if and only if

P(X|Ci )P(Ci ) > P(X|C j )P(C j ) for 1 j m, j = i.

In other words, the predicted class label is the class Ci for which P(X | Ci )P(Ci ) is the maximum.

Following formulas are used for predicting the accurate worktype for a person.

1. Probability density formula.

Using the Naive Bayes classifier we can solve the given problem as below :

1. Calculate prior Probabilities of class to be predicted:

Mean of age consultancy = 38.33

Standard deviation of age consultancy = 7.63

Mean of age service = 27

Standard deviation of age service = 7.87

Mean of age research = 31

Standard deviation of age research = 5.23

Mean of experience consultancy = 13.33

Standard deviation of experience consultancy = 4.04

Mean of experience service = 6.66

Standard deviation of experience service = 4.93

Mean of experience research = 6.25

Standard deviation of experience research = 3.30

2. Calculate conditional probabilities:

Prior probability of Work Type : Service = 0.3

Prior probability of Work Type : Consultancy = 0.3

Prior probability of Work Type : Research = 0.4

conditional probability P( middle age Consultancy )= 0.011

conditional probability P( middle age Service )= 0.047

conditional probability P( middle age Research )= 0.075

Conditional probability P( exp Consultancy )= 0.041

Conditional probability P( exp Service )= 0.077

6

Conditional probability P( Qual Consultancy )= 0.33

Conditional probability P( Qual Service )= 0.66

Conditional probability P( Qual Research )= 0.25

Multiplication of above probabilities to give Posterior Probability of Work

Type :

(a) Consultancy = 0.011 * 0.041 * 0.33 * 0.3 = 0.00044

(b) Service = 0.047 * 0.77 * 0.66 * 0.3 = 0.0007165

(c) Research = 0.075 * 0.104 * 0.25 * 0.4 = 0.00078

The predicted work type is = RESEARCH

Procedure

Execution of Program: python NaiveBays.py

Conclusion

Thus we have implemented the Naive Bayes classifier in python using sklearn.

Program

=================================================

GROUP A

Assignment No : A5

Title : Implement Naive Bayes to predict the work type for a person.

Roll No :

Batch : B

Class : BE ( Computer )

=================================================

import csv

import sys

import time

import os

from collections import Counter

import numpy as np

from sklearn.naive bayes import GaussianNB

data=[]

x=[]

y=[]

raw file=open(A5 nb data.csv,r)

lines=raw file.readlines()

for line in lines:

splitted=line.split(,)

splitted[3]=splitted[3].replace( n,)

data.append(splitted)

for i in data:

temp=[]

for a in i[:3]:

temp.append(int(a))

x.append(temp)

y.append(i[3])

input=np.array(x)

output=np.array(y)

c=GaussianNB()

c.fit(input,output)

print c.predict([30,2,8])

Output

administrator@administrator-OptiPlex-390:~/Desktop/CL2$ python A5_nb.py

[Research]

Plagiarism Score

- Incremental Discretization for Naive Bayes Learning using FIFFDUploaded byInternational Journal for Scientific Research and Development - IJSRD
- Benchmarking Sampling Techniques for Imbalance Learning in Churn PredictionUploaded byAndrés Orozco
- Comerma 2010 Land Capability Suitability and Vocation in VenezuelaUploaded bylefebvreciro
- WekaUploaded byKirubhakar Gurusamy
- bachneragisc9216d2Uploaded byapi-215481567
- PresentationUploaded byhpankush
- IDRISI Selva GIS Image Processing SpecificationsUploaded byjgstorm
- A Bayesian Approach to Measurement Bias in Networking StudiesUploaded byScott Robinson
- SCProj.pdfUploaded byjyoti prakash sahoo
- Edward Greenberg - Introduction to Bayesian Econometrics-Cambridge University Press (2007).pdfUploaded byme hahaha
- Probability TheoryUploaded byGaurav Agarwal
- l 13 Naive Bayes ClassifierUploaded bypiyaliroy
- chopra-05.pdfUploaded byPurohit Manish R
- 1812.10889Uploaded byMarcio De Andrade Vogt
- 1. Image Processing c9a262d58ba876b3be2e1a0472a2dc799277Uploaded byLitapuspita Rizka Perdana
- 7Vol59No2.pdfUploaded byTasTenunKulitBali
- myw9_report_201212130613.pdfUploaded byJ.Gowri Shankar
- 3+scki+kit+Machine+learningUploaded byDSunte Wilson
- Exam DUT 070816 AnsUploaded byEdward Baleke Ssekulima
- sensors-14-18837Uploaded byMiftahul Ulum
- Analysis of DSM and Orthophoto (Module 9)Uploaded byAbsolitudeHeart
- ML Studio OverviewUploaded byFulga Bogdan
- Class1Uploaded bymohsinwazir
- 2008 - A 3D Face Recognition System for Expression and Occlusion InvarianceUploaded byluongxuandan
- 38485-131307-1-SM.pdfUploaded byAndreea
- Handout BITS-C464 Machine Learning - 2013Uploaded byAnkit Agarwal
- Artificial Intelligence: Chapter 5 - Machine LearningUploaded byZeeshan Bhatti
- NASA_Bayesian.pdfUploaded byAlexNeumann
- When Brain Beats Behavior-NeuroforecastingUploaded byRiad Rifai
- A Comparison of Two Data Intensive Methods for Fault Location in Distribution Systems a V1.2Uploaded byAbel

- b9.pdfUploaded bysagar
- b9.pdfUploaded bysagar
- a3.pdfUploaded bysagar
- a3.pdfUploaded bysagar
- a2.pdfUploaded bysagar
- a2.pdfUploaded bysagar
- a2.pdfUploaded bysagar
- a1.pdfUploaded bysagar
- a5.pdfUploaded bysagar
- a4.pdfUploaded bysagar
- a4.pdfUploaded bysagar
- b3.pdfUploaded bysagar
- b2.pdfUploaded bysagar
- a3.pdfUploaded bysagar
- b1.pdfUploaded bysagar
- a1.pdfUploaded bysagar
- a5.pdfUploaded bysagar
- b7.pdfUploaded bysagar
- b4.pdfUploaded bysagar
- b5.pdfUploaded bysagar
- a4.pdfUploaded bysagar

- VVOReport FinalUploaded byMuhammad Ishtiaq
- Bhel Rsc VaranasiUploaded byshobhan1984
- Performance Eval of VirtualizatinUploaded byAnkush
- Tdpsql in Cluster EnvironmentUploaded byRavindra Chowdary
- Fisher Globe Valve Selection GuideUploaded byAmiroucheBenlakehal
- 2016082412244400012269_7023T _ TK1 _ W4 _ S5 _ R1Uploaded byDiTz YuuKi
- lab01Uploaded byPhilip Salmony
- ~Mil Std 2073 1d (Appendix j)Uploaded bymtcengineering
- 2375-mainUploaded byAshish Sharma
- iMananger PRS V100R016 Main Slide for CVM.pdfUploaded byramos_lisandro
- Rr211001 Electrical TechnologyUploaded bySrinivasa Rao G
- Steel Work SectionUploaded bybijukoottala
- 2014 Rojas Et Al the Benefits of a Compositional Thermal Hydraulic IPM for Subsea ProcessingUploaded bymehul10941
- Bio 3Uploaded bydnjsgud
- Human resources managementUploaded byLuis Miguel Parra G
- RadioStack Microsoft Flight Simulator XUploaded byzeze1982
- Rb Hyperfine structureUploaded byKapila Wijayaratne
- N2kmQ5 - 0521644895_0521642779Engines.pdfUploaded byvalles_1168
- ch_7_even_e_and_p.docUploaded byBobby Patel
- 3.2TipsUploaded bymaheshmalaichamy
- Alpha Chiang - Elements of Dynamic ionUploaded byBodescu Daniel
- Triaxial Testing of Weak Rocks Including the Use of Triaxial Extension Tests_Peter J. MillarUploaded byShakil Iqbal
- Case Study Analysis2Uploaded byMaviel Maratas Sarsaba
- 118Uploaded byiwangunawanhsj
- rails gis hacks tutorial 170907Uploaded bypius
- Response to Dawkins SwinburneUploaded byBrian Loui
- Chapter 1 q Worksheet01Uploaded byKhairul Anwar
- A Project Report OnUploaded bySanjeev Manral
- Matrices inverses RevisionUploaded byMenaka
- Examination Format Answer 2Uploaded byRosmini Abdul Rahman