# Detection of Bovine Mastitis in Raw Milk, Using a Low-Cost NIR Spectrometer and k-NN Algorithm
## 3 Results
### Data Preprocessing  
Data preprocessing is required to enhance the quality of the collected data. This improvement could lead to a reduction of white noise or to the calculation of descriptive features that can be used as inputs to the ML system, instead of raw signals, which can be inputs for a machine learning model.  
In this work, data preprocessing seeks to reduce the influence of any external phenomena which could have altered the spectra. Nevertheless, there is no universal approach and the most suitable one is chosen through an empirical process based on expertise [50,51]. Moreover, it is highly uncommon to use only a single preprocessing technique, due to each preprocessing technique providing complementary information. In [PERSON] et al. [52], some combinations of preprocessing techniques were proposed to improve the stability of the models, which has been recently backed by other studies [53, 54, 55, 56]. In this work, nine combinations of different techniques have been considered to preprocess the inputs. In Figure 2, the changes in the spectra for these nine combinations of preprocessing techniques are shown. The following are basic elements of these combinations:Figure 2: Changes in spectral signal of raw milk samples according to the preprocessing combinations used in this work (**a**) raw spectra, (**b**) detrended raw spectra, (**c**) Savitzky–Golay applied to raw spectra, (**d**) Beer–Lambert law (BL) applied to raw spectra, (**e**) detrending applied to BL, (**f**) Savitzky–Golay applied to BL, (**g**) standard normal variate (SNV) applied to raw spectra, (**h**) detrending applied to SNV, (**i**) Savitzky–Golay applied to SNV.  
* Raw reflectance spectra directly obtained from the NIR-reflectance instrument without further preprocessing. This is the primary source of information in any NIR spectra [50].
* The Beer-Lambert technique suggests a linear relationship between the concentration of a component and the absorbance of the spectra. The formula can be expressed as follows [50]: \[\text{A}\lambda=-\text{log}_{10}(\text{R})\cong\epsilon\lambda\text{c}\] (8) where \(\text{A}\lambda\) is the absorbance, R is the reflectance obtained by the NIR instrument, \(\epsilon\lambda\) is the wavelength-dependent molar absorptivity, \(\text{I}\) is the length of the light through the sample and c is the concentration of the component [50].
* The aim of the detrending technique is to remove the baseline and the curvilinearity of the spectra. This method models the baseline as a linear function, which is then subtracted from each spectrum value independently [57].
* [PERSON] and [PERSON] [58] popularized a method named after its authors, which consists of a smoothing function for the numerical derivation of a vector. In this method, a p-order polynomial is fitted and then the d-order derivative is calculated at center point i.
* Standard normal variate (SNV) is a technique used for scattering correction [59]. This process can be expressed in the form of the following equation: \[\text{Xi},\text{snv}=-\overline{\text{x}}\text{S}\] (9) where Xi,snv is the SNV at a wavelength i, \(\overline{\text{x}}\) is the spectrum average of the sample to be corrected, and S is the standard deviation of the spectrum sample.