nb_classifier_write
Writes a Naive Bayes Classifier to an ASCII file for later retrieval using imsls_f_nb_classifier_read.
Synopsis
#include <imsls.h>
void imsls_f_nb_classifier_write (Imsls_f_nb_classifier *nb_classifier, char *filename, …, 0)
The type double function is imsls_d_nb_classifier_write.
Required Arguments
Imsls_f_nb_classifier *nb_classifier (Input)
A trained Naive Bayes Classifier.
char *filename (Input)
The name of an ASCII file to be created. A full or relative path can be given. If this file exists, it is replaced with the Naive Bayes Classifier. If it does not exist, it is created. If the optional argument IMSLS_FILE is used, filename is ignored.
Synopsis with Optional Arguments
#include <imsls.h>
void imsls_f_nb_classifier_write (Imsls_f_nb_classifier *nb_classifier, char *filename,
IMSLS_PRINT,
IMSLS_FILE, FILE *file,
0)
Optional Arguments
IMSLS_PRINT, (Input)
Prints status of file opening, writing and closing.
Default: No printing.
IMSLS_FILE, FILE *file (Input/Output)
A FILE pointer to a file opened for writing. This file is written but not closed. If this option is provided, filename is ignored. This option allows users to write additional data and multiple classifiers to the same file (see Example 2). To ensure the file is opened and closed with the same C run-time library used by the product, open and close this file using imsls_fopen and imsls_fclose.
Description
This function stores an Imsls_f_nb_classifier data structure containing a trained Naive Bayes Classifier into an ASCII file. If the optional argument IMSLS_FILE is provided, imsls_f_nb_classifier_write writes the file and returns without closing the file. If this argument is not provided, imsls_f_nb_classifier_write creates a file using the path and name provided in filename, writes the data structure to that file and then closes the file before returning.
Examples
Example 1
This example trains a classifier using Fisher’s Iris data. These data consist of 150 patterns. The input attributes consist of four continuous attributes and one classification attribute with three classes. The classifier is stored into four lines of an ASCII file named NB_Classifier_Ex1.txt.
#include <imsls.h>
#include <stdio.h>
int main()
{
char *filename = "NB_Classifier_Ex1.txt";
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;
float *pred_class_prob, continuous[4*150] ;
float *irisData; /* Fishers Iris Data */
char *classLabel[3] = {"Setosa ", "Versicolour", "Virginica "};
Imsls_f_nb_classifier *nb_classifier;
irisData = imsls_f_data_sets(3,0);
/* setup the required input arrays from the data matrix */
for(i=0; i<n_patterns; i++){
classification[i] = (int) irisData[i*5]-1;
for(j=1; j<=n_continuous; j++)
continuous[i*n_continuous+j-1] = irisData[i*5+j];
}
classErrors = imsls_f_naive_bayes_trainer(n_patterns,
n_classes, classification,
IMSLS_CONTINUOUS, n_continuous, continuous,
IMSLS_NB_CLASSIFIER, &nb_classifier, 0);
printf(" Iris Classification Error Rates\n");
printf("----------------------------------------------\n");
printf(" Setosa Versicolour Virginica | TOTAL\n");
printf(" %d/%d %d/%d %d/%d | %d/%d\n",
classErrors[0], classErrors[1],
classErrors[2], classErrors[3],
classErrors[4], classErrors[5],
classErrors[6], classErrors[7]);
printf("----------------------------------------------\n\n");
imsls_f_nb_classifier_write(nb_classifier, filename,
IMSLS_PRINT, 0);
}
Output
===============
Iris Classification Error Rates
----------------------------------------------
Setosa Versicolour Virginica | TOTAL
0/50 3/50 3/50 | 6/150
----------------------------------------------
Opening NB_Classifier_Ex1.txt for writing Naive Bayes data structure
Writing Naive Bayes data structure... 5 Lines written.
File NB_Classifier_Ex1.txt closed
Example 2
This example illustrates the use of the optional argument IMSLS_FILE to store multiple classifiers into one file. Two Naive Bayes classifiers are trained using Fisher’s Iris data. These data consist of 150 patterns. The input attributes consist of four continuous attributes and one classification attribute. The first classifier is trained using all four inputs and the second using only the first two. The networks are stored into 10 lines of an ASCII file named NB_Classifier_Ex2.txt.
#include <imsls.h>
#include <stdio.h>
extern FILE* imsls_fopen(char* filename, char* mode);
extern void imsls_fclose(FILE* file);
int main()
{
FILE *file;
char *filename = "NB_Classifier_Ex2.txt";
int i, j;
int n_patterns =150; /* 150 training patterns */
int n_cont4 =4; /* four continuous input attributes */
int n_cont2 =2; /* two continuous input attributes */
int n_classes =3; /* three classification categories */
int n_classifiers =2; /* two classifiers in this example */
int classification[150], *classErrors;
float cont4[4*150], cont2[2*150] ;
float *irisData; /* Fishers Iris Data */
char *classLabel[3] = {"Setosa ", "Versicolour", "Virginica "};
Imsls_f_nb_classifier *nb_classifier;
irisData = imsls_f_data_sets(3,0);
/* setup the required input arrays from the data matrix */
for(i=0; i<n_patterns; i++){
classification[i] = (int) irisData[i*5]-1;
for(j=1; j<=n_cont4; j++) {
cont4[i*n_cont4+j-1] = irisData[i*5+j];
if(j<3) cont2[i*n_cont2+j-1] = irisData[i*5+j];
}
}
printf("Opening file %s\n\n", filename);
file = imsls_fopen(filename, "w");
fprintf(file, "%d\n", 2);
classErrors = imsls_f_naive_bayes_trainer(n_patterns,
n_classes, classification,
IMSLS_CONTINUOUS, n_cont4, cont4,
IMSLS_NB_CLASSIFIER, &nb_classifier, 0);
printf("Iris Classification Error Rates - Classifier 1\n");
printf("----------------------------------------------\n");
printf(" Setosa Versicolour Virginica | TOTAL\n");
printf(" %d/%d %d/%d %d/%d | %d/%d\n",
classErrors[0], classErrors[1],
classErrors[2], classErrors[3],
classErrors[4], classErrors[5],
classErrors[6], classErrors[7]);
printf("----------------------------------------------\n\n");
imsls_free(classErrors);
/* write first classifier */
imsls_f_nb_classifier_write(nb_classifier, NULL, IMSLS_PRINT,
IMSLS_FILE, file, 0);
imsls_f_nb_classifier_free(nb_classifier);
classErrors = imsls_f_naive_bayes_trainer(n_patterns,
n_classes, classification,
IMSLS_CONTINUOUS, n_cont2, cont2,
IMSLS_NB_CLASSIFIER, &nb_classifier, 0);
printf("Iris Classification Error Rates - Classifier 2\n");
printf("----------------------------------------------\n");
printf(" Setosa Versicolour Virginica | TOTAL\n");
printf(" %d/%d %d/%d %d/%d | %d/%d\n",
classErrors[0], classErrors[1],
classErrors[2], classErrors[3],
classErrors[4], classErrors[5],
classErrors[6], classErrors[7]);
printf("----------------------------------------------\n\n");
imsls_f_nb_classifier_write(nb_classifier, NULL, IMSLS_PRINT,
IMSLS_FILE, file, 0);
imsls_free(classErrors);
printf("Closing Classifier File\n");
imsls_fclose(file);
return;
}
Output
Opening file NB_Classifier_Ex2.txt
Iris Classification Error Rates - Classifier 1
----------------------------------------------
Setosa Versicolour Virginica | TOTAL
0/50 3/50 3/50 | 6/150
----------------------------------------------
Writing Naive Bayes data structure to file stream.
Writing Naive Bayes data structure... 5 Lines written to file.
File not closed.
Iris Classification Error Rates - Classifier 2
----------------------------------------------
Setosa Versicolour Virginica | TOTAL
1/50 13/50 19/50 | 33/150
----------------------------------------------
Writing Naive Bayes data structure to file stream.
Writing Naive Bayes data structure... 5 Lines written to file.
File not closed.
Closing Classifier File
Fatal Errors
IMSLS_FILE_OPEN_FAILURE |
Unable to open file for writing network. |