# Detection of Bovine Mastitis in Raw Milk, Using a Low-Cost NIR Spectrometer and k-NN Algorithm
###### Abstract  
Among the bovine diseases, mastitis causes high economic losses in the dairy production system. Nowadays, detection under field conditions is mainly performed by the California Mastitis Test, which is considered the de facto standard. However, this method presents with problems of slowness and the expensiveness of the chemical-reactive process, which is deeply dependent on an expert's trained eye and, consequently, is highly imprecise. The aim of this work is to propose a new method for bovine mastitis detection under field conditions. The proposed method uses a low-cost, smartphone-connected NIR spectrometer which solves the aforementioned problems of slowness, expert dependency and disposability of the chemical methods. This method uses spectra in combination with two k-Nearest Neighbors models. The first model is used to detect the presence of mastitis while the second model classifies the positive cases into weak and strong. The resulting method was validated by using a leave-one-out technique where the ground truth was obtained by the California Mastitis Test. The detection model achieved an accuracy of 92.4%, while the one classifying the severity showed an accuracy of 95%. d  
a _Applied_  
Received: 9 September 2021  
Accepted: 8 November 2021  
Published: 15 November 2021  
**Published: 15 November 2021**
on the interaction of the reagent with somatic cell DNA [9; 10; 11]. However, this method requires a minimal degree of expertise to interpret the results [9; 12], and also the handling of reagents in the farm is a challenge for small farmers [13]. Therefore, to improve the productivity of cattle herds, more effective diagnostic methods are needed. These methods should put the spotlight on the prevention and control of mastitis with a special interest in easier, faster and cheaper methods [14]. In this sense, some studies have shown the potential of near infrared spectroscopy (NIR). This kind of device has been used in several studies for a variety of applications such as the detection of counterfeit tablets [15; 16], making an estimation of the age and expiration date of packaged chicken fillets [17], or to make gross estimates of the amount of fat and protein in milk by using statistical calculations [18]. Further yet, some works have proved the possibility of using this kind of device to determine the presence of contamination or illness, e.g., the presence of mastitis in raw milk [19; 20]. However, researchers have moved the spotlight not to lab-grade devices but to low-cost ones, such as in [PERSON]'s work [21], where a system based on the combination of two low-cost narrow-wavelength devices together with simple regression methods allow the performance of rough on-site estimations of fat and protein in milk samples. This latter analysis is based on changes in milk composition--especially in proteins and ionic concentration [22]. Moreover, in recent years, low-cost NIR spectrometers have become more widely available [16], providing rapid on-site sample analysis [15; 17; 23; 24; 25; 26]. Additionally, in order to bypass the use of expensive laboratory tests, portable diagnostic devices are much demanded in the healthcare, food, agriculture and livestock sectors. In this sense, mobile NIR devices have gained interest inside the research community. Even though this kind of device presents with the limitation of a narrower wavelength than their lab-grade counterparts or low-cost fixed-installed devices, the benefits of an even more affordable price together with the flexibility provided by their portability makes them an interesting alternative not only for researchers, but also for the general public [27]. However, the capture of the NIR spectra is only half of the process, because the analysis needs to be performed with the support of a chemometric technique. While other works on NIR spectrometry have used simple regression techniques such as PCA or linear regression [18; 21], machine learning techniques have proved to have a special place in this kind of system [25; 28; 29].