Retrieves a Naive Bayes Classifier previously filed using imsls_f_nb_classifier_write.
#include <imsls.h>
Imsls_f_nb_classifier *imsls_f_nb_classifier_read (char *filename,…, 0)
The type double function is imsls_d_nb_classifier_read.
char *filename
(Input)
The name of an ASCII file containing a Naive
Bayes Classifier previously saved using imsls_f_nb_classifier_write.
A full or relative path can be given. If the optional argument IMSLS_FILE is used,
filename is
ignored.
A pointer to an Imsls_f_nb_classifier data structure containing a Naive Bayes Classifier previously stored using imsls_f_nb_classifier_write.
#include <imsls.h>
Imsls_f_nb_classifier
*imsls_f_nb_classifier_read
(char *filename,
IMSLS_PRINT,
IMSLS_FILE,
FILE
*file,
0)
IMSLS_PRINT
(Input)
Prints status of file opening, reading and
closing.
Default: No printing.
IMSLS_FILE, FILE* file
(Input)
A FILE pointer to a file
opened for reading. This file is read but not closed. If this option
is provided, filename is
ignored. This argument allows users to read additional user-defined data
and multiple classifiers from the same file (see Example
2 below). 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.
Function nb_classifier_read reads a classifier from an ASCII file previously stored using imsls_f_nb_classifier_write and returns a Naive Bayes Classifier in the form of an Imsls_f_nb_classifier data structure. If the optional argument IMSLS_FILE is provided, a classifier is read from the file and returned without closing the file. If this argument is not provided, imsls_f_nb_classifier_read opens the file using the path and name provided in filename, reads the classifier then closes the file and returns the data structure.
This example reads a classifier previously trained using Fisher’s Iris data (see Example 1 of imsls_f_nb_classifier_write). These data consist of 150 patterns, each with four continuous attributes and one dependent variable. The classifier is read from 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], *predictedClass;
int classErrors[8];
float 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];
}
nb_classifier = imsls_f_nb_classifier_read(filename, IMSLS_PRINT,0);
predictedClass = imsls_f_naive_bayes_classification(nb_classifier,
n_patterns, IMSLS_CONTINUOUS, continuous, 0);
for(i=0; i<6; i++) classErrors[i] = 0;
for(i=0; i<n_patterns; i++){
switch (classification[i])
{
case 0:
classErrors[1]++;
if(classification[i] != predictedClass[i])
classErrors[0]++;
break;
case 1:
classErrors[3]++;
if(classification[i] != predictedClass[i])
classErrors[2]++;
case 2:
classErrors[5]++;
if(classification[i] != predictedClass[i])
classErrors[4]++;
break;
}
}
classErrors[6] = classErrors[0]+classErrors[2]+classErrors[4];
classErrors[7] = classErrors[1]+classErrors[3]+classErrors[5];
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");
return;
}
Attempting to open NB_Classifier_Ex1.txt
for reading Naive Bayes data structure
File NB_Classifier_Ex1.txt Successfully Opened
File NB_Classifier_Ex1.txt closed
Iris Classification Error Rates
----------------------------------------------
Setosa Versicolour Virginica | TOTAL
0/50 3/50 3/50 | 6/150
----------------------------------------------
This example illustrates the use of the optional argument IMSLS_FILE to read multiple classifiers stored previously into a single file using imsls_f_nb_classifier_write (see Example 2 of imsls_f_nb_classifier_write). Two Naive Bayes classifiers were trained 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 first classifier was trained using all four inputs and the second using only the first two. The classifiers are read from 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 =0; /* number of classifiers */
int classification[150], *predictedClass;
int classErrors[8];
float cont4[4*150], cont2[2*150] ;
float *irisData; /* Fishers Iris Data */
char *classLabel[3] = {"Setosa ", "Versicolour", "Virginica "};
Imsls_f_nb_classifier *nb_classifier4, *nb_classifier2;
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, "r");
fscanf(file, "%d", &n_classifiers);
nb_classifier4 = imsls_f_nb_classifier_read(" ", IMSLS_PRINT,
IMSLS_FILE, file, 0);
predictedClass = imsls_f_naive_bayes_classification(nb_classifier4,
n_patterns, IMSLS_CONTINUOUS, cont4, 0);
for(i=0; i<6; i++) classErrors[i] = 0;
for(i=0; i<n_patterns; i++){
switch (classification[i])
{
case 0:
classErrors[1]++;
if(classification[i] != predictedClass[i])
classErrors[0]++;
break;
case 1:
classErrors[3]++;
if(classification[i] != predictedClass[i])
classErrors[2]++;
break;
case 2:
classErrors[5]++;
if(classification[i] != predictedClass[i])
classErrors[4]++;
break;
}
}
classErrors[6] = classErrors[0]+classErrors[2]+classErrors[4];
classErrors[7] = classErrors[1]+classErrors[3]+classErrors[5];
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_free(predictedClass);
nb_classifier2 = imsls_f_nb_classifier_read(" ", IMSLS_PRINT,
IMSLS_FILE, file, 0);
predictedClass = imsls_f_naive_bayes_classification(nb_classifier2,
n_patterns,
IMSLS_CONTINUOUS, cont2, 0);
for(i=0; i<6; i++) classErrors[i] = 0;
for(i=0; i<n_patterns; i++){
switch (classification[i])
{
case 0:
classErrors[1]++;
if(classification[i] != predictedClass[i])
classErrors[0]++;
break;
case 1:
classErrors[3]++;
if(classification[i] != predictedClass[i])
classErrors[2]++;
break;
case 2:
classErrors[5]++;
if(classification[i] != predictedClass[i])
classErrors[4]++;
break;
}
}
classErrors[6] = classErrors[0]+classErrors[2]+classErrors[4];
classErrors[7] = classErrors[1]+classErrors[3]+classErrors[5];
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_free(predictedClass);
printf("Closing Classifier File.\n");
imsls_fclose(file);
}
Opening file NB_Classifier_Ex2.txt
Naive Bayes Classifier restored from file. File not closed.
Iris Classification Error Rates
----------------------------------------------
Setosa Versicolour Virginica | TOTAL
0/50 3/50 3/50 | 6/150
----------------------------------------------
Naive Bayes Classifier restored from file. File not closed.
Iris Classification Error Rates
----------------------------------------------
Setosa Versicolour Virginica | TOTAL
1/50 13/50 19/50 | 33/150
----------------------------------------------
IMSLS_FILE_OPEN_FAILURE Unable to open file for reading neural network.