1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2019-2020 ARM Limited. All rights reserved.
3 *
4 * $Date:         09. December 2019
5 * $Revision:     V1.0.0
6 *
7 * Project:       CMSIS DSP Library
8 * Title:         arm_bayes_example_f32.c
9 *
10 * Description:   Example code demonstrating how to use Bayes functions.
11 *
12 * Target Processor: Cortex-M/Cortex-A
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 *   - Redistributions of source code must retain the above copyright
18 *     notice, this list of conditions and the following disclaimer.
19 *   - Redistributions in binary form must reproduce the above copyright
20 *     notice, this list of conditions and the following disclaimer in
21 *     the documentation and/or other materials provided with the
22 *     distribution.
23 *   - Neither the name of ARM LIMITED nor the names of its contributors
24 *     may be used to endorse or promote products derived from this
25 *     software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 * -------------------------------------------------------------------- */
40 
41 /**
42  * @addtogroup groupExamples Examples
43  * @{
44  *
45  * @defgroup BayesExample Bayes Example
46  *
47  * \par Description:
48  * \par
49  * Demonstrates the use of Bayesian classifier functions. It is complementing the tutorial
50  * about classical ML with CMSIS-DSP and python scikit-learn:
51  * https://developer.arm.com/solutions/machine-learning-on-arm/developer-material/how-to-guides/implement-classical-ml-with-arm-cmsis-dsp-libraries
52  *
53  * \example arm_bayes_example_f32.c
54  *
55  * @} */
56 
57 #include <math.h>
58 #include <stdio.h>
59 #include "arm_math.h"
60 
61 /*
62 Those parameters can be generated with the python library scikit-learn.
63 */
64 arm_gaussian_naive_bayes_instance_f32 S;
65 
66 #define NB_OF_CLASSES 3
67 #define VECTOR_DIMENSION 2
68 
69 const float32_t theta[NB_OF_CLASSES*VECTOR_DIMENSION] = {
70   1.4539529436590528f, 0.8722776016801852f,
71   -1.5267934452462473f, 0.903204577814203f,
72   -0.15338006360932258f, -2.9997913665803964f
73 }; /**< Mean values for the Gaussians */
74 
75 const float32_t sigma[NB_OF_CLASSES*VECTOR_DIMENSION] = {
76   1.0063470889514925f, 0.9038018246524426f,
77   1.0224479953244736f, 0.7768764290432544f,
78   1.1217662403241206f, 1.2303890106020325f
79 }; /**< Variances for the Gaussians */
80 
81 const float32_t classPriors[NB_OF_CLASSES] = {
82   0.3333333333333333f, 0.3333333333333333f, 0.3333333333333333f
83 }; /**< Class prior probabilities */
84 
main(void)85 int32_t main(void)
86 {
87   /* Array of input data */
88   float32_t in[2];
89 
90   /* Result of the classifier */
91   float32_t result[NB_OF_CLASSES];
92   float32_t temp[NB_OF_CLASSES];
93   float32_t maxProba;
94   uint32_t index;
95 
96   S.vectorDimension = VECTOR_DIMENSION;
97   S.numberOfClasses = NB_OF_CLASSES;
98   S.theta = theta;
99   S.sigma = sigma;
100   S.classPriors = classPriors;
101   S.epsilon=4.328939296523643e-09f;
102 
103   in[0] = 1.5f;
104   in[1] = 1.0f;
105 
106   index = arm_gaussian_naive_bayes_predict_f32(&S, in, result,temp);
107 
108   maxProba = result[index];
109 
110 #if defined(SEMIHOSTING)
111   printf("Class = %d\n", index);
112   printf("Max proba = %f\n", (double)maxProba);
113 #endif
114 
115   in[0] = -1.5f;
116   in[1] = 1.0f;
117 
118   index = arm_gaussian_naive_bayes_predict_f32(&S, in, result,temp);
119 
120   maxProba = result[index];
121 
122 #if defined(SEMIHOSTING)
123   printf("Class = %d\n", index);
124   printf("Max proba = %f\n", (double)maxProba);
125 #endif
126 
127   in[0] = 0.0f;
128   in[1] = -3.0f;
129 
130   index = arm_gaussian_naive_bayes_predict_f32(&S, in, result,temp);
131 
132   maxProba = result[index];
133 
134 #if defined(SEMIHOSTING)
135   printf("Class = %d\n", index);
136   printf("Max proba = %f\n", (double)maxProba);
137 #endif
138 
139 #if !defined(SEMIHOSTING)
140   while (1); /* main function does not return */
141 #endif
142 }
143 
144 
145 
146