Classifies unknown patterns using a previously trained Naive Bayes classifier. The classifier is contained in an Imsls_f_nb_classifier data structure, which is optional output from imsls_f_naive_bayes_trainer.
IMSLS_NOMINAL, intnominal[] (Input) nominal is an array of size n_patterns by nb_classifier->n_nominal containing values for the nominal input attributes. The i-th row contains the nominal input attributes for the i-th pattern. The j-th column of this matrix contains the classifications for the j-th nominal attribute. They must be encoded with integers starting from 0 to nb_classifier->n_categories[i]-1. Any value outside this range is treated as a missing value. If nb_classifier->n_nominal=0, this array is ignored.
IMSLS_CONTINUOUS, floatcontinuous[] (Input) continuous is an array of size n_patterns by nb_classifier->n_continuous containing values for the continuous input attributes. The i-th row contains the input attributes for the i-th training pattern. The j-th column of this matrix contains the values for the j-th continuous attribute. Missing values should be set equal to imsls_f_machine(6)=NaN. Patterns with missing values are still used to train the classifier unless the IMSLS_IGNORE_MISSING_VALUES option is supplied. If nb_classifier->n_continuous=0, this matrix is ignored.
IMSLS_PRINT_LEVEL, intprint_level (Input) Print levels for printing data warnings and final results. print_level should be set to one of the following values:
print_level
Description
IMSLS_NONE
Printing of data warnings and final results is suppressed.
IMSLS_FINAL
Prints final summary of Naive Bayes classifier training.
IMSLS_DATA_WARNINGS
Prints information about missing values and PDF calculations equal to zero.
IMSLS_TRACE_ALL
Prints final summary plus all data warnings associated with missing values and PDF calculations equal to zero.
Default: IMSLS_NONE.
IMSLS_USER_PDF, floatpdf(intindex[], floatx) (Input) The user-supplied probability density function and parameters used to calculate the conditional probability density for continuous input attributes is required when the classifier was trained with selected_pdf[i]= IMSLS_USER.
When pdf is called, x will equal continuous[i*n_continuous+j], and index will contain the following values for i, j, and k:
Index
Value
index[0]
i = pattern index
index[1]
j = attribute index
index[2]
k = target classification
The pattern index ranges from 0 to n_patterns-1 and identifies the pattern index for x. The attributes index ranges from 0 to n_categories[i]-1, and k=classification[i].
This argument is ignored if n_continuous = 0. By default the Gaussian PDF is used for calculating the conditional probability densities using either the means and variances calculated from the training patterns or those supplied in IMSLS_GAUSSIAN_PDF.
IMSLS_USER_PDF_WITH_PARMS, floatpdf(intindex[], float x, void*parms), void*parms (Input) The user-supplied probability density function and parameters used to calculate the conditional probability density for continuous input attributes is required when selected_pdf[i]= IMSLS_USER.pdf also accepts a pointer to parms supplied by the user. The parameters pointed to by parms are passed to pdf each time it is called. For an explanation of the other arguments, see IMSLS_USER_PDF.
IMSLS_PREDICTED_CLASS_PROB, float**pred_class_prob, (Output) The address of a pointer to an array of size n_patterns by n_classes, where n_classes is the number of target classifications. The values in the i-th row are the predicted classification probabilities associated with the target classes. pred_class_prob[i*n_classes+j] is the estimated probability that the i-th pattern belongs to the j-th target classes.
IMSLS_PREDICTED_CLASS_PROB_USER, floatpred_class_prob[] (Output) Storage for array pred_class_prob is provided by the user. See IMSLS_PREDICTED_CLASS_PROB for a description.
IMSLS_RETURN_USER, intclassification[] (Output) An array of length n_patterns containing the predicted classifications for each pattern described by the input attributes in nominal and continuous.
Description
Function imsls_f_naive_bayes_classification estimates classification probabilities from a previously trained Naive Bayes classifier. Two arrays are used to describe the values of the nominal and continuous attributes used for calculating these probabilities. The predicted classification returned by this function is the class with the largest estimated classification probability. The classification probability estimates for each pattern can be obtained using the optional argument IMSLS_PREDICTED_CLASS_PROB.
Examples
Example 1
Fisher’s (1936) Iris data is often used for benchmarking classification algorithms. It is one of the IMSL data sets and consists of the following continuous input attributes and classification target:
Classification (Iris Type): Setosa, Versicolour or Virginica.
This example trains a Naive Bayes classifier using 150 training patterns from Fisher’s data then classifies ten unknown plants using their sepal and petal measurements.
#include <imsls.h>
#include <stdio.h>
int main(){
int i, j;
int n_patterns =150; /* 150 training patterns */
int n_continuous =4; /* four continuous input attributes */
int n_classes =3; /* three classification categories */
int classification[150], *classErrors, *predictedClass;
This example uses the spam benchmark data available from the Knowledge Discovery Databases archive maintained at the University of California, Irvine: http://archive.ics.uci.edu/ml/datasets/Spambase.
These data contain of 4601 patterns consisting of 57 continuous attributes and one classification. 41% of these patterns are classified as spam and the remaining as non-spam. The first 54 continuous attributes are word or symbol percentages. That is, they are percents scaled from 0 to 100% representing the percentage of words or characters in the email that contain a particular word or character. The last three continuous attributes are word lengths. For a detailed description of these data visit the KDD archive at the above link.
In this example, percentages are transformed using the arcsin/square root transformation . The last three attributes, word lengths, are transformed using square roots. Transformed percentages and the first word length attribute are modeled using the Gaussian distribution. The last two word lengths are modeled using the log normal distribution.
It is interesting to note that the classification error rates obtained by training a classifier from a random sample is slightly lower than those obtained from training a classifier with all 4601 patterns. When the classifier is trained using all 4601 patterns, the overall classification error rate was 12.9% (see Example 3 for imsls_f_naive_bayes_trainer). It is 12.4% for a random sample of 2000 patterns.
Number of Patterns = 4601 Number Classified as Spam = 1813
Classification Error Rates Reported by
Trainer from Training Dataset of 2000 Observations