User Documentation
 All Files Functions Groups
Naive Bayes Classification
+ Collaboration diagram for Naive Bayes Classification:
Warning
This MADlib method is still in early stage development. There may be some issues that will be addressed in a future version. Interface and implementation is subject to change.
About:

Naive Bayes refers to a stochastic model where all independent variables \( a_1, \dots, a_n \) (often referred to as attributes in this context) independently contribute to the probability that a data point belongs to a certain class \( c \). In detail, Bayes' theorem states that

\[ \Pr(C = c \mid A_1 = a_1, \dots, A_n = a_n) = \frac{\Pr(C = c) \cdot \Pr(A_1 = a_1, \dots, A_n = a_n \mid C = c)} {\Pr(A_1 = a_1, \dots, A_n = a_n)} \,, \]

and the naive assumption is that

\[ \Pr(A_1 = a_1, \dots, A_n = a_n \mid C = c) = \prod_{i=1}^n \Pr(A_i = a_i \mid C = c) \,. \]

Naives Bayes classification estimates feature probabilities and class priors using maximum likelihood or Laplacian smoothing. These parameters are then used to classifying new data.

A Naive Bayes classifier computes the following formula:

\[ \text{classify}(a_1, ..., a_n) = \arg\max_c \left\{ \Pr(C = c) \cdot \prod_{i=1}^n \Pr(A_i = a_i \mid C = c) \right\} \]

where \( c \) ranges over all classes in the training data and probabilites are estimated with relative frequencies from the training set. There are different ways to estimate the feature probabilities \( P(A_i = a \mid C = c) \). The maximum likelihood estimate takes the relative frequencies. That is:

\[ P(A_i = a \mid C = c) = \frac{\#(c,i,a)}{\#c} \]

where

Since the maximum likelihood sometimes results in estimates of "0", you might want to use a "smoothed" estimate. To do this, you add a number of "virtual" samples and make the assumption that these samples are evenly distributed among the values assumed by attribute \( i \) (that is, the set of all values observed for attribute \( a \) for any class):

\[ P(A_i = a \mid C = c) = \frac{\#(c,i,a) + s}{\#c + s \cdot \#i} \]

where

The case \( s = 1 \) is known as "Laplace smoothing". The case \( s = 0 \) trivially reduces to maximum-likelihood estimates.

Note: (1) The probabilities computed on the platforms of PostgreSQL and Greenplum database have a small difference due to the nature of floating point computation. Usually this is not important. However, if a data point has

\[ P(C=c_i \mid A) \approx P(C=c_j \mid A) \]

for two classes, this data point might be classified into diferent classes on PostgreSQL and Greenplum. This leads to the differences in classifications on PostgreSQL and Greenplum for some data sets, but this should not affect the quality of the results.

(2) When two classes have equal and highest probability among all classes, the classification result is an array of these two classes, but the order of the two classes is random.

(3) The current implementation of Naive Bayes classification is only suitable for discontinuous (categorial) attributes.

For continuous data, a typical assumption, usually used for small datasets, is that the continuous values associated with each class are distributed according to a Gaussian distribution, and then the probabilities \( P(A_i = a \mid C=c) \) can be estimated. Another common technique for handling continuous values, which is better for large data sets, is to use binning to discretize the values, and convert the continuous data into categorical bins. These approaches are currently not implemented and planned for future releases.

(4) One can still provide floating point data to the naive Bayes classification function. Floating point numbers can be used as symbolic substitutions for categorial data. The classification would work best if there are sufficient data points for each floating point attribute. However, if floating point numbers are used as continuous data, no warning is raised and the result may not be as expected.

Input:

The training data is expected to be of the following form:

{TABLE|VIEW} trainingSource (
    ...
    trainingClassColumn INTEGER,
    trainingAttrColumn INTEGER[],
    ...
)

The data to classify is expected to be of the following form:

{TABLE|VIEW} classifySource (
    ...
    classifyKeyColumn ANYTYPE,
    classifyAttrColumn INTEGER[],
    ...
)
Usage:
  • Precompute feature probabilities and class priors:
    SELECT create_nb_prepared_data_tables(
        'trainingSource', 'trainingClassColumn', 'trainingAttrColumn',
        numAttrs, 'featureProbsName', 'classPriorsName'
        );
    This creates table featureProbsName for storing feature probabilities and table classPriorsName for storing the class priors.
  • Perform Naive Bayes classification:
    SELECT create_nb_classify_view(
        'featureProbsName', 'classPriorsName',
        'classifySource', 'classifyKeyColumn', 'classifyAttrColumn',
        numAttrs, 'destName'
        );
    This creates the view destName mapping classifyKeyColumn to the Naive Bayes classification:
    key | nb_classification
    ----+------------------
    ...
  • Compute Naive Bayes probabilities:
    SELECT create_nb_probs_view(
        'featureProbsName', 'classPriorsName',
        'classifySource', 'classifyKeyColumn', 'classifyAttrColumn',
        numAttrs, 'destName'
    );
    This creates the view destName mapping classifyKeyColumn and every single class to the Naive Bayes probability:
    key | class | nb_prob
    ----+-------+--------
    ...
  • Ad-hoc execution (no precomputation): Functions create_nb_classify_view and create_nb_probs_view can be used in an ad-hoc fashion without the above precomputation step. In this case, replace the function arguments
    'featureProbsName', 'classPriorsName'
    with
    'trainingSource', 'trainingClassColumn', 'trainingAttrColumn'
Examples:

The following is an extremely simplified example of the above option #1 which can by verified by hand.

  1. The training and the classification data:
    sql> SELECT * FROM training;
     id | class | attributes
    ----+-------+------------
      1 |     1 | {1,2,3}
      2 |     1 | {1,2,1}
      3 |     1 | {1,4,3}
      4 |     2 | {1,2,2}
      5 |     2 | {0,2,2}
      6 |     2 | {0,1,3}
    (6 rows)
    
    sql> select * from toclassify;
     id | attributes
    ----+------------
      1 | {0,2,1}
      2 | {1,2,3}
    (2 rows)
    
  2. Precompute feature probabilities and class priors
    sql> SELECT madlib.create_nb_prepared_data_tables(
    'training', 'class', 'attributes', 3, 'nb_feature_probs', 'nb_class_priors');
    
  3. Optionally check the contents of the precomputed tables:
    sql> SELECT * FROM nb_class_priors;
     class | class_cnt | all_cnt
    -------+-----------+---------
         1 |         3 |       6
         2 |         3 |       6
    (2 rows)
    
    sql> SELECT * FROM nb_feature_probs;
     class | attr | value | cnt | attr_cnt
    -------+------+-------+-----+----------
         1 |    1 |     0 |   0 |        2
         1 |    1 |     1 |   3 |        2
         1 |    2 |     1 |   0 |        3
         1 |    2 |     2 |   2 |        3
    ...
    
  4. Create the view with Naive Bayes classification and check the results:
    sql> SELECT madlib.create_nb_classify_view (
    'nb_feature_probs', 'nb_class_priors', 'toclassify', 'id', 'attributes', 3, 'nb_classify_view_fast');
    
    sql> SELECT * FROM nb_classify_view_fast;
     key | nb_classification
    -----+-------------------
       1 | {2}
       2 | {1}
    (2 rows)
    
  5. Look at the probabilities for each class (note that we use "Laplacian smoothing"):
    sql> SELECT madlib.create_nb_probs_view (
    'nb_feature_probs', 'nb_class_priors', 'toclassify', 'id', 'attributes', 3, 'nb_probs_view_fast');
    
    sql> SELECT * FROM nb_probs_view_fast;
     key | class | nb_prob
    -----+-------+---------
       1 |     1 |     0.4
       1 |     2 |     0.6
       2 |     1 |    0.75
       2 |     2 |    0.25
    (4 rows)
    
Literature:

[1] Tom Mitchell: Machine Learning, McGraw Hill, 1997. Book chapter Generativ and Discriminative Classifiers: Naive Bayes and Logistic Regression available at: http://www.cs.cmu.edu/~tom/NewChapters.html

[2] Wikipedia, Naive Bayes classifier, http://en.wikipedia.org/wiki/Naive_Bayes_classifier

See Also
File bayes.sql_in documenting the SQL functions.