Real-Time Behavioral Recognition in Dairy Cows Based on Geomagnetism and Acceleration Information

The behaviors of dairy cows, such as feeding, ruminating, running, resting (standing, lying), head-shaking, drinking, and walking, can indicate their health status. In this study, a multi-sensor was used to collect data of cow’s multi-behaviors for research on behavior recognition. Firstly, a collar style data acquisition system equipped with geomagnetic and acceleration sensors to collect the behavioral data of dairy cows during their daily activities was designed. Secondly, the dairy cow behavioral recognition fusion model based on K-Nearest-Neighbors (KNN) and Random Forest (RF) models were used for behavior classification. To verify the accuracy of the fusion model, the algorithms of KNN, RF, Gradient Boosting Decision Tree (GBDT), Support Vector Machine (SVM), and Learning Vector Quantization (LVQ) were introduced for comparative recognition experiments with different algorithms. The KNN-RF fusion model had the highest average recognition accuracy of 98.51%, followed by the KNN model with an average recognition accuracy of 95.37%, and the LVQ model had the lowest average recognition accuracy of 80.81%. For the recognition and verification of each behavior, the KNN-RF fusion model had the most obvious improvement in the recognition of dairy cow feeding behavior, with a recognition accuracy of 99.34%, followed by the KNN model with a recognition accuracy of 95.07%. All six models had the lowest recognition accuracy for cow head-shaking behavior: a recognition accuracy of 89.11% with the KNN-RF model followed by the RF model with a recognition accuracy of 85.14%. The system can quickly and continuously collect cow behavior information, accurately recognize individual behaviors, and provide a scientific basis for the optimal design and efficient management of digital facilities and equipment for dairy cows.


I. INTRODUCTION
Dairy farming in China is developing rapidly towards largescale production. Therefore, the physical health, breeding status, feed intake, and physiological indicators of the individual dairy cow indicate the sustainable development of the The associate editor coordinating the review of this manuscript and approving it for publication was Noel Crespi . dairy industry and the economic interests of dairy farmers. The modernization level of individual cow behavior monitoring equipment in China is generally low. Rumination is an essential physiological activity of a dairy cow that is closely related to its milk production and reproductive performance, and this reflects the health status of a dairy cow to a certain extent [1]. Feeding and activities can indicate the nutritional status of cows. Traditional manual monitoring methods are VOLUME 9, 2021 This work is licensed under a Creative Commons Attribution 4.0 License. For more information, see https://creativecommons.org/licenses/by/4.0/ labor-intensive and will perturb dairy behavior. The combination of computer technology and intelligent algorithm analysis can improve detection accuracy by analyzing the geomagnetic and acceleration differences generated by cows under normal living conditions [2]. In addition, compared with traditional methods, such a monitoring method has the advantages of being non-contact and non-invasive [3], thereby promoting the development of dairy cow production without adverse effects. For distinguishing animal behaviors, Wang et al. [4] installed 12 legs-tags and six location sensors on dairy cows and classified the acceleration data using the Semi-Supervised Fuzzy C-Means (SS-FCM) algorithm to quantify behaviors such as feeding, lying, standing, walking, and running. Arcidiacono et al. [5] developed a threshold classifier based on cow behaviors, collecting acceleration data from wearable sensors attached to cow collars. Vázquez-Diosdado et al. [6] designed a mixed multi-level model to automatically detect the drinking behavior of dairy cows, with environmental temperature, parity, and average monthly milk production. Benaissa et al. [7] installed accelerometers on cows to identify their feeding, rumination, and other digestive activities and classifying the behaviors with a Decision Tree (DT) model. By installing an inertial measurement unit on the backs of cows, Achour et al. [8] monitored the standing, lying, and walking behaviors of cows. Tani et al. [9] simulated audio signals using a single-axis accelerometer and pattern recognition algorithms to recognize and correctly distinguish grazing and chewing motion. A real-time monitoring system for the individual behaviors of natural mating in the cage breeding chickens was established by Li et al. [10], using a nine-axis accelerometer. Guo [11] collected data through a three-axis accelerometer and used the K-means clustering algorithm to obtain a stable clustering center for the classification and recognition of typical goat daily behaviors and characteristics. The system provided a basis for determining the relationship between goat daily behaviors and diseases, increasing animal welfare, and establishing goat disease prediction models.
Wang et al. [4] used acoustic signals to monitor the feeding behavior of grazing cattle to evaluate the daily forage intake, and the true positive detection rate of cattle feeding events reached 95%. Milone et al. [13] adopted a hidden Markov chain model to segment and recognize the acoustic signals of cattle feeding for the evaluation of forage intake. Navon et al. [14] designed an audio processing algorithm to recognize the chewing motions of cows, goats, and sheep during outdoor feeding and verified the system through experiments in which the recognition accuracy of cow chewing reached 96%. Chelotti et al. [15] designed an acoustic monitoring system that employed directional widefrequency microphones mounted on the foreheads of cows. A real-time decision logic algorithm was used to analyze and measure the signal frequency and amplitude. The cow feeding behavior was detected and classified through the duration and energy iteration of the sound signals, thus realizing the automatic detection of feed intake. Ambriz-Vilchis et al. [16] mounted a RC microphone monitoring collars on the necks of cows. The reliability of the microphone collar was determined by comparing the methods of artificial visual observation and video analysis of cow rumination. Yao [17] designed a cow rumination information acquisition system that was composed of sensor nodes, a wireless transceiver module, an ANT network module, and a PC. The system collected cow rumination data through the acoustic sensor nodes and used an audio recognition algorithm to collect cow ruminating sounds. He [18] developed rumination monitoring equipment that integrated acoustic sensors to collect ruminating sound signals of the dairy cows. The linear prediction spectrum coefficients were used to analyze the ruminating sound signals in the frequency domain, and the recognition algorithm was imported into the device to verify the accuracy of the algorithm. Vandermeulen et al. [19] and Carpentier et al. [20] extracted the audio features using the audio processing algorithms to identify cow coughing, and the experiment verified that respiratory diseases could be identified in the early stages.
In the above-mentioned studies, there were few comprehensive analyses of the cow behaviors with the information fusion, and it is difficult to obtain the overall condition of the dairy cows only through rumination behavior or activity monitoring. The objectives of this study are (1) to identify cow activities by collecting the signals of the cow feeding, ruminating, running, resting (standing, lying), head-shaking, drinking, and walking behaviors with the geomagnetic and acceleration sensors; (2) to explore the best model for the cow behavior classification by introducing a classification model and inputting behavioral data. For the classification of the cow's behavior, both K-nearest neighbor (KNN) and Random Forest (RF) models were employed; and (3) to verify the effectiveness of the KNN-RF model, the recognition accuracy of the dairy cow behaviors by the KNN, RF, Gradient Boosting Decision Tree (GBDT), Support Vector Machine (SVM), and Learning Vector Quantization (LVQ) models were compared.

A. TEST DEVICE, TEST SITE, AND COW CONDITIONS
To obtain the data of the cow's behaviors such as feeding, ruminating (standing, lying), running, resting (standing, lying), head-shaking, drinking, and walking without disturbance, a collar data acquisition system was designed (FIGURE 1). Due to the complexity of the dairy farming environment, the volume of the data acquisition device should be reduced as far as possible to ensure that the dairy cows will not feel uncomfortable when wearing the data acquisition device. Therefore, the three-axis accelerometer MMA8451Q (±8 m/s 2 ) (Xiqi Technology Co., Ltd, Shenzhen, China) and the three-axis geomagnetic sensor HMC5883L (±8 Gauss) (Shanghai Bingyin Electronics Co., Ltd, Shanghai, China), were selected as the main components of the data acquisition system. The signals were converted by the four-channel logic level converter TXS0104EPWR and input into a single-chip microcomputer system STM32 1471. The data were collected at a sampling rate of 12.5 Hz and stored on an SD card with.DAT format. The data acquisition device was fixed in a waterproof sealed box. A counterweight was added to tightly attach the data acquisition device to the cow's neck. The counterweight was selected from different weight specifications and was hung below the cow's neck with a tie. The optimal counterweight worn by the cow was finally determined to be 500 g to ensure a stable position during movement [18].
After the data acquisition device was bound and tightly attached to the cow's neck with a tie, the three-axis indicator's directions of the geomagnetic and acceleration sensors would change: when the cow was resting standing and looking upward, the X-axis pointed to the cow's head along the cervical vertebrae; the Y-axis was perpendicular to the cervical vertebrae and pointed upward (toward the cow's back). The plane formed by the X-axis and the Y-axis (XOY) was parallel to the longitudinal symmetry plane of the cow's body, and the Z-axis was perpendicular to the XOY plane and pointed out of the cow's body. The indicator directions after the change are shown in FIGURE 1.
The behaviors data were collected at the Jinlan Dairy Cow Breeding Company in Tai'an City. The company has 2,000 high-yield Holstein cows. The collection time was from November 1st to December 30th, 2019, a total of 60 days. During collecting data, three cows were randomly selected from the cow house to wear the detect devices, and the data were collected continuously 24 hours a day for three consecutive days. After three days, the cows were replaced with another three cows for data collection. To verify the algorithm, during the process of the collected data, meanwhile, three assistants recorded the videos of the cows' activities from a distance from 8:00 to 12:00 and from 13:00 to 17:00 every day (milking began at 17:30). The cows were not disturbed during the experiments, and the total time for recording the videos was eight hours every day. The video recording of the cows' behavior was synchronous with the start of the data acquisition device.
B. TEST DATA PROCESSING 1) DAIRY COW BEHAVIOR CLASSIFICATION By observing and analyzing the videos together with an experienced staff working in the cow farm, the data of cows' seven main daily behaviors were recognized in this study.
The classification and description of the cow's seven main behaviors are shown in TABLE 1.
Ten cows were selected for the video analysis from 60 cows, and the behaviors' features were recorded. The relationship between the chewing numbers and the ruminating time for rumination is shown in TABLE 2. The shortest single chewing period during the cow rumination was about 0.8 s, and the longest was about 0.9 s. Assuming that the number of the cow chewing movements during rumination is a, the period of the cow chewing during rumination is t, and the period of one chewing movement during rumination is t 1 , then a = t/t 1 . To reduce the counting error of the rumination times(occurring number of ruminations), the single chewing period during the cow rumination was taken as t 1 = 0.84 s (TABLE 2). The behavioral data were randomly divided into a training set and a test set at a ratio of 8:2. The database contained seven sample categories: the cow feeding, ruminating, running, walking, resting (standing and lying), drinking, and head-shaking.

2) DATA NORMALIZATION
Since the dataset was collected from only one farm, to provide a reliable basis for replicating the results of this study, the dataset was converted through standardization. All the data were normalized to the range of [0, 1]. Denoting the new data value after normalization as w , the raw value of the fused data as w, the minimum value in the data as w min , and the maximum value as w max , then where a x , a y , a z represents respectively the normalized acceleration components of the three-axis accelerometer in three directions, and d = (d x , d y , d z ), where d x , d y , d z represents respectively the normalized geomagnetic components of the three-axis geomagnetic sensor in three directions. Because the collected cow behavior data in the X , Y , and Z axes are different, the sensor orientation is a necessary condition for determining the behaviors. Also, to increase the indicators for determining the behaviors and to better distinguish each behavior, the vectorial sum data of acceleration data and geomagnetic data θ = (θ x , θ y , θ z ) was adopted to represent the cows' behaviors in this study. The formula for calculating VeAG is: In the formula,

III. THE RECOGNITION MODEL
A. THE KNN ALGORITHM [21] The basic principle of the KNN algorithm is to select K training sample points closest to the test point and output the sample label with the largest number of K sample points, i.e., the VOLUME 9, 2021 TABLE 1. Description of seven main behaviors of dairy cows [4], [6], [7], [24], [25], [30]. majority voting principle, thereby obtaining the category of the test point. There were nine eigenvalues in the dataset of this study, so each sample could be expressed by a ninedimensional row vector: X = (X 1 , X 2 , X 3 , . . . . . . , X 9 ). The contents represented by each variable of X 1 -X 9 are shown in TABLE 3.
To implement the KNN algorithm, it is necessary to calculate the distance from each sample point to the test point, choose the K nearest samples, obtain the category label of each sample in the K samples, find the label with the largest number in the K samples, i.e., the majority voting principle, and return the label and obtain the final test sample category results.
The steps of the algorithm are as follows: 1) Input: the training dataset, the test dataset. 2) The distances between the test data and each training data were calculated. The methods to calculate the KNN search distance include Euclidean distance, Manhattan distance, and Mahalanobis distance. The Euclidean distance used in this paper is as follows: suppose calculation points A = (X 1 , X 2 , X 3 · · · X 9 ) and , then the Euclidean distance of AB is: 3) The distances are sorted in increasing order. 4) K points with the smallest distances are chosen. 5) The occurrence frequency of the category where the first K points were located is determined.
6) The category with the highest occurrence frequency in the first K points is returned as the predicted classification of the test data, and the predicted result is used as the softmax layer of the KNN model. 7) Output: classification results. The advantages of the KNN algorithm are as follows: 1) The KNN algorithm is intuitive, simple, and easy to realize.
2) The classification is determined by selecting K neighboring values and is less affected by noise [21]. 3) New data can be added directly without additional training. 4) Multi-classification is supported with high accuracy, and non-linear regression problems can also be solved. 5) The KNN algorithm directly uses data for classification, thus reducing the impact of improper selection of category features on the classification results and greatly reducing errors.
B. THE RF ALGORITHM [22], [23] RF is an ensemble learning method whose base predictors are decision trees. The final ensemble prediction result is produced by voting for the prediction values of each decision tree. In the forest, each decision tree is independent, and each decision node learns and classifies data independently, which is highly efficient. Among all current algorithms, RF has excellent accuracy. The RF algorithm works as follows: 1) k features (columns) are randomly selected from the dataset (table) with a total of m features (where k is less than or equal to m. In this study, the total number of features is 9, therefore, m = 9). Then, a decision tree is built according to the k features. 2) After repeating n times, the k features are randomly combined to build n decision trees (or different random samples of data, called bootstrapping samples). 3) Random variables are passed onto each decision tree to predict the results. All predicted results targets) are stored, and n results are obtained from n decision trees(In this study, when the number of decision trees n = 12, the fusion model has the highest accuracy). 4) The prediction target with the highest number of votes is taken as the final prediction of the RF algorithm [23] and is used as the softmax layer of the RF model.

C. THE KNN-RF WEIGHTED FUSION MODEL
The KNN algorithm and the RF algorithm were combined and fused for classification and recognition. The fusion steps of the KNN-RF model are as follows: × [ a * knn score + b * rf score ] (4) Formula (4) defines the pre-process of the entire model, where ζ is an indicator function, the value of which is 1 when the condition is satisfied and 0 when the condition is not satisfied, guiding the network to perform a binary judgment. The terms knn score and rf score are the final layers of the network output by the KNN and RF models, respectively, and they are one-dimensional vectors. knn score ∈ R c and rf score ∈ R c , where c is the number of labels for the dataset. The index corresponding to the largest value in the vectors is selected as the output label of the input that satisfies the following relationship: knn label = Argmax (knn score ) and rf label = Argmax (rf score ) The combined model fuses the probability layers that were output by the KNN and RF models, and the fusion process is similar to the ensemble learning process and can be divided into two parts: 1) If knn label = rf label , i.e., the output result of the KNN model was equal to that of the RF model, there was no need to fuse the probability layer of RF, and the result was directly output. The classification result of the KNN model is used as the final output. 2) If knn label = rf label , i.e., the output result of the KNN model was not equal to that of the RF model, the probability layers of the two models were fused. The fusion process is as follows: a * knn score + b * rf score , where a and b are trainable hyperparameters for dynamically adjusting the weights of the KNN and RF models in the fusion model, respectively. The index corresponding to the largest value in the final fusion probability layer is the final output label.

A. BEHAVIOR ANALYSIS
In order to compare the data collected by the different sensors with the captured videos, each signal of the 7-type of the behaviors of the cow was respectively selected from the data according to the one-to-one correspondence between the sensor signal and video signal. After that, the acceleration and the geomagnetic sensor data were combined to determine which judgment method was appropriate for improving the recognition accuracy. 1) As shown in FIGURE 2A, the vectorial sum data VeSAG changed periodically during the cows were eating. The curve segment displayed in the rectangle named a in FIGURE 2A (hereafter referred briefly to as FIGURE 2A-a) indicates that the cow was bowing its head for eating forage, and the curve segment displayed in the rectangle named b in FIGURE 2A(hereafter referred briefly to as FIGURE 2A-b) indicates that the cow was raising its head and swallowing the forage. 2) As shown in FIGURE 2C and FIGURE 2D, when the cows were running or walking, the curves of the vectorial sum data fluctuated dramatically, which indicates a clear difference from other activities. The curves of the accelerometer data changed periodically, but the curves of the geomagnetic sensor data changed non-periodically because when the cow was running, the height of its neck above the ground would change irregularly. The number of running and walking steps could be counted according to the acceleration data. FIGURE 2B, FIGURE 2E, and FIGURE 2G, when the cow was ruminating, drinking, or standing, the fluctuations of the curves of the data were more moderate than that for describing other behaviors. The curve segment displayed in the rectangle named c in FIGURE 2B(hereafter referred briefly to as FIGURE 2B-c) is the data of the cow's head-shaking during the rumination process. The curve segments displayed in the rectangle named e in FIGURE 2G (hereafter referred briefly to as FIGURE 2G-e) indicate that the cow raises its head 3 times during the drinking process. 4) As shown in FIGURE 2F, when the cow was shaking its head, the curves of the sensors data generated were similar to those of feeding, but with a higher peak. The curve segments displayed in the rectangle named d in FIGURE 2F (hereafter referred briefly to as FIGURE 2F-d) indicate that the cow shakes its head twice. The curves of cow's head-shaking and feeding both changed periodically, and the values of the VeSAG were relatively close, therefore, the vectorial sum of acceleration data was added to further distinguish between these behaviors. The formula for calculating the vectorial sum of acceleration VeSA is as follows:

3) As shown in
The difference between running and walking could be analyzed according to the curve of the vectorial sum of acceleration data. As shown in FIGURE 2C and FIGURE 3a, the collected data value was the largest when the cow was running. Therefore, the normalized acceleration data of the cow running had the highest conversion value, equal to 1. The X -axis acceleration value of the walking behavior was up to approximately 0.8 (FIGURE 2D). The values of VeSA of most steps when the cow was running were greater than 0.4, while those of walking were less than 0.4 as shown in FIGURE 3a and FIGURE 3b. After the KNN-RF model recognized that the data segment was a cow running behavior, a threshold could be set. When the vectorial sum acceleration data values of the running were greater than 0.4, they were recorded as the beginning of the first step; when the values were less than 0.4, they were the end of the first step; when they were again greater than 0.4, they were recorded as the beginning of the second step, and this continued until the vectorial sum acceleration data were no longer greater than 0.4, signifying that the running behavior had ended. The walking behavior threshold was set to 0.25, and the step counting method was the same as that for running. When the cow was eating, the motion mode was to chew with the head down, swallow with the head up, and then continue to chew with the head down, so the Y -axis data were the most regular, as shown in FIGURE 2A-a and FIGURE 2A-b. FIGURE 3c shows the geomagnetic Y -axis data when the cows were lying and standing. They were resting, so the behavior could be classified directly by setting a threshold according to the geomagnetic data. When the magnetic sensor is close to the object with magnetism, the signal of the geomagnetic sensor changes, so the specific water supply device with certain magnetism can be used to detect the drinking behavior of dairy cows. In addition, the drinking trough is generally located on a corner or side of the cow sports field. However, the cow could ruminate, lying, and standing in most areas of the cowshed and the sports field, as shown in FIGURE 2B and FIGURE 3c. Thus, the geomagnetic data of these three behaviors were not the same as the drinking behavior.

B. EXPERIMENTS WITH DIFFERENT PARAMETERS OF WEIGHTED FUSION MODEL
The KNN-RF model has four hyperparameters, the Neighbors (the KNN model), the Estimators (the RF model), and the fusion model weights a and b (dynamically adjusting the weights of the KNN and the RF models).
To obtain the relationship between each hyperparameter and the accuracy of the model recognition, the control variable method was adopted for experiments to obtain the optimal hyperparameters. According to the results lisited in TABLE 4, it is known that the relationship between the value of the Neighbors and the model's recognition accuracy was positive if the value of the Neighbors was less than 5; contrarily, if the value of the Neighbors was greater than 5, the relationship between the value of the Neighbors and the model's recognition accuracy was negative. Therefore, when the value of the Neighbors was equal to 5, the model's recognition accuracy was the highest at 98.27%. Then, setting Neighbors equal to 5, the value of the Estimators was adjusted; when the value of the Estimators was less than 12, it had a positive correlation with the model's recognition accuracy and vice versa. Therefore, when the hyperparameters Neighbors was equal to 5 and the Estimators was equal to 12, the KNN-RF model had the highest recognition rate. Similarly, when a was equal to 0.6 and b was equal to 0.4, the KNN-RF model had the highest recognition rate. The data were collected for 15 consecutive days and verified once per day. The average recognition accuracy is shown in FIGURE 4a. The highest recognition accuracy was 98.51%, and the lowest was 98.32%. Among these values, the KNN-RF model had the highest recognition accuracy of 99.34% for the cow feeding behavior and the lowest recognition accuracy of 89.11% for the head-shaking behavior as shown in FIGURE 4b.
The KNN-RF model combining two classical machine learning models has the following advantages: 1) Due to that the time of each activity of the cow is not the same, so the amount of data of each behavior is also different. The KNN model has a low prediction accuracy for rare categories when the samples in the dataset are unbalanced. The base predictors of the RF mode are decision trees, and the final ensemble prediction result is obtained by voting on the predicted values of each decision tree. In the forest, each decision tree is independent, and each decision node learns and classifies independently. Therefore, the RF method can overcome the shortcomings of the KNN model and improve the overall prediction accuracy. 2) It was found that the acceleration sensor and geomagnetic sensor will produce noise signal when they collect the behavior data of dairy cows. Over fitting will occur when RF model is used to process noisy data sets. Therefore, in order to avoid the over-fitting during analyzing the noise dataset with the RF model, a model combined the RF model with the KNN model was used in this research, which could effectively remove outliers.

C. ACCURACY ANALYSIS OF THE COW'S HEAD-SHAKING BEHAVIOR
By validating, it is known that all models used in this research had the lowest recognition accuracy for the head-shaking behavior. For recognizing the cow's head-shaking behavior, the KNN-RF fusion model had the highest recognition accuracy at 89.11%, followed by the KNN model with a recognition accuracy of 85.14%. With the analysis of the results from all models misrecognising cow head-shaking behavior, it could be found that the head-shaking behavior was recognized as the ruminating behavior in the highest proportion. Among them, the recognition error rate of the KNN-RF fusion model was 100%; the recognition error rate of the SVM model was 76.19%.

D. COMPARISON TESTS
To evaluate the performance of the KNN-RF model, the recognized test results of the RF, SVM, GBDT, KNN, and LVQ  were compared with each other. The relationship between the model's parameters and the model's average recognition accuracy is shown in TABLE 5.
Representation of the confusion matrix for each model is shown in Table 6. In the confusion matrix, the diagonal elements are correctly recognized samples, and nondiagonal elements are misclassified samples. The recognition results of the test set were counted, and the confusion matrix obtained under each algorithm is shown in Table 6. The best results of all the models were compared. The statistics of the recognition accuracy of the cows' seven behaviors by each model are shown in FIGURE 5.
Through the analysis of the above test results, it can be concluded that: 1) Compared with the recognition rate of other behavior of dairy cows, the recognition rate of headshaking was the lowest. When a cow shakes its head, the head may be shaken from side to side or rotational with different speeds and directions. So, the accuracy of head-shaking behavior recognition of the

V. DISCUSSION
The behavior of a dairy cow is the comprehensive embodiment of its physiological activity states. Monitoring dairy cow behavior could provide insight into an animal's wellness status; however, traditional observational techniques may influence results, being time and labor-intensive, and may not provide the necessary level of diagnostic accuracy. To overcome the shortages of the traditional methods for detecting the individual behavior of the dairy cow, the acceleration sensors and the geomagnetic sensors were used to collect simultaneously accelerated data and geomagnetic data during cow activities, then the different classifiers based on the deeplearning model were used to monitor the multiple behaviors. In recent years, the acceleration sensor has been used for identifying animal behavior due to its relevance and potential applications [24], [27], [29], [31]- [40]. However, as far as the authors' knowledge, using the geomagnetic sensor for classifying the multiple behaviors of the dairy cowthis is the first. The use of a neck-mounted accelerometer for monitoring ingestive-related cow behaviors based on a KNN-RF fusion model was investigated in this research.
For the individual identification of the dairy cows, TABLE 7 illustrates the results obtained by our work and those of other works. In our work, acceleration/geomagnetic data and KNN-RF recognition model were used, a global accuracy of 98.51% was achieved for recongnizing behaviors of the dairy cows, it was higher by 0.51% than that reported by Robert et al. [24], by 13.51% than that reported by González et al. [25], by 8.51% than that reported by Alvarenga et al. [27], by 6.51% than that reported by Andriamasinoro et al. [28], by 18.51% than that reported by Wang et al. [31], by 13.51% than that reported by Foldager et al. [38], by 10.75% than that reported by Guo [11]. The author thought that the reasons for the high accuracy of the cow behavior recognition are as follows: (1) The acceleration data and magnetic data of cow behavior were used; (2) The multidimensional features of the collected data were extracted and inputed into the recognition algorithm; (3) According to the advantages of KNN and RF algorithm, this two algorithms were combined for behavior recognition; (4) The parameters of KNN and RF algorithm were optimized to improve the performance of the algorithm on cow behavior data.
In this study, the recognition rate of cow head shaking is the lowest, so the behavior of head-shaking of dairy cow was analyzed. After comparing the data with the video one-to-one correspondence, it was found that the manner that the cows shook their heads could be divided into two types, one is ''turn-back'' style and the other is ''rotary'' style, as shown in Figure 6. The different types of the cow shaking their heads resulted in the lowest recognition rate of the model and most of them were mistaken for ruminating behavior. The specific reasons are as follows: 1) When a cow shakes its head, if it shakes in a the manner of ''turn-back'' style as shown in FIGURE 6a, i.e., the cow shakes its mouth toward its tail (the cow's head rotates along the Y-axis), the data from the acceleration and geomagnetic sensors differ greatly from those of other behaviors, making the motion easier to distinguish. 2) When a cow shakes head in a ''rotary'' style (the head of the cow rotates along the X-axis) as shown in FIGURE 6b. The orientation of the cow's mouth is unchanged because the direction indicated by the X-axis of the sensor is the same as the orientation of the cow's mouth. So the data of acceleration and geomagnetic sensor of the cow in the X-axis will have the smallest change, and the data of acceleration and geomagnetic sensor of the cow in the Y-axis and the Z-axis will have the largest changes. The pattern is similar to the sensor X-axis data of rumination behavior. Therefore, the head shaking behavior is most likely to be mistaken for rumination when the cows shake their heads in a ''rotary'' style.
Detection of the dairy multi-behaviors such as walking, standing, or lying, with accelerometers or IMU [8], [26], placed on the neck [25], [26], [30], legs [41], halter [30], or ears [30] is accurate to between 29% and 99% using machine learning [35]. In this research, it was found that only a sensor on one part of the dairy would result in misleading (or at least incomplete) data. So in the future, the IMU, the UWB, and the geomagnetic sensors would be installed in the different parts of the cows to detect their multi-behaviors. It was also found that the geomagnetic sensors fixed in different parts of the cow, such as the halter, the back, and the tail, would detect the different behavioral characteristics of the cows.

VI. CONCLUSION
1) The results in this research demonstrated that the low-cost, non-commercial, lab-constructed acceleration/geomagnetic collar sensors could be used to accurately monitor cow multi behavior parameters and that the model predictions align with expected cow behavior.
2) The six algorithms used in this research could recognize the muti-behaviors of the dairy cows with slightly different accuracy. It was found that the recognition results of the KNN-RF fusion model could accurately recognize the behaviors of the individual dairy cows. The KNN-RF fusion model had the most obvious improvement in the recognition of the dairy cow feeding behavior, with a recognition accuracy of 99.34%, followed by the KNN model with the feeding behavior recognition accuracy of 95.07%. Compared with the KNN model, the KNN-RF model had a 4.27% improvement in recognition accuracy.