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 * @ingroup groupExamples
43 */
44
45 /**
46 * @defgroup BayesExample Bayes Example
47 *
48 * \par Description:
49 * \par
50 * Demonstrates the use of Bayesian classifier functions. It is complementing the tutorial
51 * about classical ML with CMSIS-DSP and python scikit-learn:
52 * https://developer.arm.com/solutions/machine-learning-on-arm/developer-material/how-to-guides/implement-classical-ml-with-arm-cmsis-dsp-libraries
53 *
54 */
55
56
57 /** \example arm_bayes_example_f32.c
58 */
59
60 #include <math.h>
61 #include <stdio.h>
62 #include "arm_math.h"
63
64 /*
65 Those parameters can be generated with the python library scikit-learn.
66 */
67 arm_gaussian_naive_bayes_instance_f32 S;
68
69 #define NB_OF_CLASSES 3
70 #define VECTOR_DIMENSION 2
71
72 const float32_t theta[NB_OF_CLASSES*VECTOR_DIMENSION] = {
73 1.4539529436590528f, 0.8722776016801852f,
74 -1.5267934452462473f, 0.903204577814203f,
75 -0.15338006360932258f, -2.9997913665803964f
76 }; /**< Mean values for the Gaussians */
77
78 const float32_t sigma[NB_OF_CLASSES*VECTOR_DIMENSION] = {
79 1.0063470889514925f, 0.9038018246524426f,
80 1.0224479953244736f, 0.7768764290432544f,
81 1.1217662403241206f, 1.2303890106020325f
82 }; /**< Variances for the Gaussians */
83
84 const float32_t classPriors[NB_OF_CLASSES] = {
85 0.3333333333333333f, 0.3333333333333333f, 0.3333333333333333f
86 }; /**< Class prior probabilities */
87
main(void)88 int32_t main(void)
89 {
90 /* Array of input data */
91 float32_t in[2];
92
93 /* Result of the classifier */
94 float32_t result[NB_OF_CLASSES];
95 float32_t temp[NB_OF_CLASSES];
96 float32_t maxProba;
97 uint32_t index;
98
99 S.vectorDimension = VECTOR_DIMENSION;
100 S.numberOfClasses = NB_OF_CLASSES;
101 S.theta = theta;
102 S.sigma = sigma;
103 S.classPriors = classPriors;
104 S.epsilon=4.328939296523643e-09f;
105
106 in[0] = 1.5f;
107 in[1] = 1.0f;
108
109 index = arm_gaussian_naive_bayes_predict_f32(&S, in, result,temp);
110
111 maxProba = result[index];
112
113 #if defined(SEMIHOSTING)
114 printf("Class = %d\n", index);
115 printf("Max proba = %f\n", (double)maxProba);
116 #endif
117
118 in[0] = -1.5f;
119 in[1] = 1.0f;
120
121 index = arm_gaussian_naive_bayes_predict_f32(&S, in, result,temp);
122
123 maxProba = result[index];
124
125 #if defined(SEMIHOSTING)
126 printf("Class = %d\n", index);
127 printf("Max proba = %f\n", (double)maxProba);
128 #endif
129
130 in[0] = 0.0f;
131 in[1] = -3.0f;
132
133 index = arm_gaussian_naive_bayes_predict_f32(&S, in, result,temp);
134
135 maxProba = result[index];
136
137 #if defined(SEMIHOSTING)
138 printf("Class = %d\n", index);
139 printf("Max proba = %f\n", (double)maxProba);
140 #endif
141
142 #if !defined(SEMIHOSTING)
143 while (1); /* main function does not return */
144 #endif
145 }
146
147
148
149