Pacific-Design.com

    
Home Index

1. Machine Learning

2. 5 Matlab Naive Bayes

+ Categorize

+ Definition

Machine Learning / 5 Matlab Naive Bayes /

Matlab - Naïve Bayes Classifier

%-------------------------------------------------------------------------%
%                   Naïve Bayes Classifier
%-------------------------------------------------------------------------%
clc
fprintf  ( 'Travel   - Age  -  Sex   - Survived')                       
class01 = { 'First'  'Adult'  'Male'    'Yes' };                                
class02 = { 'First'  'Adult'  'Male'    'No'  };                                
class03 = { 'First'  'Adult'  'Female'  'Yes' };                                
class04 = { 'Second' 'Adult'  'Female'  'Yes' };                                
class05 = { 'Second' 'Adult'  'Male'    'No'  };                                
class06 = { 'Second' 'Child'  'Female'  'Yes' };
class07 = { 'Third'  'Child'  'Female'  'Yes' };
class08 = { 'Third'  'Adult'  'Male'    'No'  };
class09 = { 'First'  'Child'  'Female'  'Yes' };

%--- Predict whether A child from second class will survive in titanic
class10 = { 'Second' 'Child'  'Male'    '?'   };
matrix = [class01;class02;class03;class04;class05;class06;class07;class08;class09;class10]

fprintf('P( Survived=Yes |Class=Second, Age=Child, Sex=Male )\n\n')

fprintf('  = P(Class=Second | Survived=Yes)  * \n')
fprintf('    P(Age=Child    | Survived=Yes)  * \n')
fprintf('    P(Sex=Male     | Survived=Yes)  * \n')
fprintf('    P(Survived=Yes)               / \n')
fprintf('    P(Class=Second, Age=Child, Sex=Male)\n\n')

%----------------------------------------------------------%
Total        = 9;
SurviveYes   = 6;
SurviveNo    = 3;
%----------------------------------------------------------%
TravelTypes   = 3; % first, second, third
AgeTypes      = 2; % adult, child
SurviveTypes  = 2; % male,  female
%----------------------------------------------------------%
SecondYes   = 2;
SecondNo    = 1;
%----------------------------------------------------------%
ChildYes   = 3;
ChildNo    = 0;;
%----------------------------------------------------------%
MaleYes    = 1;
MaleNo     = 3;
%----------------------------------------------------------%

Survived = ((SecondYes + 1)  / (SurviveYes + TravelTypes))  * ...
           ((ChildYes  + 1)  / (SurviveYes + AgeTypes))     * ...
           ((MaleYes   + 1)  / (SurviveYes + SurviveTypes)) * ...
           (SurviveYes/Total)

%----------------------------------------------------------%

NotSurvive = ((SecondNo + 1)  / (SurviveNo + TravelTypes))  * ...
             ((ChildNo  + 1)  / (SurviveNo + AgeTypes))     * ...
             ((MaleNo   + 1)  / (SurviveNo + SurviveTypes)) * ...
             (SurviveNo/Total)

         
%--------- Determine Survival Chance ---------%
if Survived > NotSurvive
   fprintf('Male child from second class survived.\n\n') 
else
   fprintf('Male child from second class did not survived.\n\n') 
end

Results:

Travel - Age   -  Sex  - Survived
matrix = 

    'First'     'Adult'    'Male'      'Yes'
    'First'     'Adult'    'Male'      'No' 
    'First'     'Adult'    'Female'    'Yes'
    'Second'    'Adult'    'Female'    'Yes'
    'Second'    'Adult'    'Male'      'No' 
    'Second'    'Child'    'Female'    'Yes'
    'Third'     'Child'    'Female'    'Yes'
    'Third'     'Adult'    'Male'      'No' 
    'First'     'Child'    'Female'    'Yes'
    'Second'    'Child'    'Male'      '?'  

P( Survived=Yes |Class=Second, Age=Child, Sex=Male )

  = P(Class=Second | Survived=Yes)  * 
    P(Age=Child    | Survived=Yes)  * 
    P(Sex=Male     | Survived=Yes)  * 
    P(Survived=Yes)               / 
    P(Class=Second, Age=Child, Sex=Male)


Survived =

    0.0278


NotSurvive =

    0.0178

Male child from second class survived.