MCPcopy Create free account
hub / github.com/arrayfire/arrayfire / naive_bayes_demo

Function naive_bayes_demo

examples/machine_learning/naive_bayes.cpp:104–141  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

102}
103
104void naive_bayes_demo(bool console, int perc) {
105 array train_images, train_labels;
106 array test_images, test_labels;
107 int num_train, num_test, num_classes;
108
109 // Load mnist data
110 float frac = (float)(perc) / 100.0;
111 setup_mnist<false>(&num_classes, &num_train, &num_test, train_images,
112 test_images, train_labels, test_labels, frac);
113
114 int feature_length = train_images.elements() / num_train;
115 array train_feats = moddims(train_images, feature_length, num_train);
116 array test_feats = moddims(test_images, feature_length, num_test);
117
118 // Get training parameters
119 array mu, sig2;
120 float *priors = new float[num_classes];
121 naive_bayes_train(priors, mu, sig2, train_feats, train_labels, num_classes);
122
123 // Predict the classes
124 array res_labels =
125 naive_bayes_predict(priors, mu, sig2, test_feats, num_classes);
126 delete[] priors;
127
128 // Results
129 printf("Trainng samples: %4d, Testing samples: %4d\n", num_train, num_test);
130 printf("Accuracy on testing data: %2.2f\n",
131 accuracy(res_labels, test_labels));
132
133 benchmark_nb(train_feats, test_feats, train_labels, num_classes);
134
135 if (!console) {
136 test_images = test_images.T();
137 test_labels = test_labels.T();
138
139 display_results<false>(test_images, res_labels, test_labels, 20);
140 }
141}
142
143int main(int argc, char **argv) {
144 int device = argc > 1 ? atoi(argv[1]) : 0;

Callers 1

mainFunction · 0.85

Calls 7

moddimsFunction · 0.85
naive_bayes_trainFunction · 0.85
naive_bayes_predictFunction · 0.85
benchmark_nbFunction · 0.85
TMethod · 0.80
accuracyFunction · 0.70
elementsMethod · 0.45

Tested by

no test coverage detected