Method for predicting diarrhea risk grade of weaned piglets
By collecting and analyzing diarrhea data from weaned piglets and using machine learning models for training and evaluation, the reliability problem of the risk prediction of diarrhea in piglets after weaning is solved in the existing technology, and the accurate prediction and risk management of diarrhea risk level are achieved.
Patent Information
- Application Number
- CN202411574863.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-06
- Publication Date
- 2025-02-25
AI Technical Summary
Existing methods for assessing diarrhea in weaned piglets cannot accurately capture all factors that affect the risk of diarrhea in post-weaning piglets, resulting in limited reliability of predicted results.
By collecting diarrhea data from weaned piglets, including independent and dependent variables, training and evaluation based on machine learning models, the best model is selected to predict diarrhea risk level. Specific steps include data preprocessing, model training and evaluation, and use decision trees, support vector machines, XGB and other models for analysis.
Accurate prediction of the risk level of diarrhea in weaned piglets is achieved, and more reliable risk warning and intervention guidance is provided, helping breeders optimize the feeding environment and feed formula, reduce the occurrence of diarrhea, and improve breeding efficiency and economic benefits.
Smart Images

Figure CN119513535A_ABST
Abstract
Description
Technical Field
[0001] The invention relates to the technical field of piglet breeding, and in particular to a method for predicting the risk level of diarrhea in weaned piglets. Background Art
[0002] In modern large-scale pig production, diarrhea in piglets after weaning is an important problem faced by the pig farming industry. Weaning is an important turning point in the growth and development of piglets. Due to changes in physiology, environment and diet, piglets are susceptible to various health problems during this period. Among them, diarrhea is one of the most common diseases, which not only seriously affects the growth and development of piglets, but also increases breeding costs and reduces economic benefits.
[0003] There have been a large number of studies on factors related to diarrhea in post-weaned piglets, such as studies on the relationship between feed nutrients and diarrhea. It was found that excessively high or low protein content in feed may affect the intestinal health of piglets and cause diarrhea. However, most of these studies are single-factor explorations and lack multi-factor integrated analysis. In addition, most existing studies focus on qualitative description stages such as stress factor analysis, nutritional strategy research, and microbial flora research. These methods often lack accurate predictions of diarrhea risk levels, making it difficult to provide farmers with timely risk warnings and intervention guidance.
[0004] Existing research has limitations in data collection and processing. Most of them remain at the descriptive or correlation level and have not formed a systematic risk prediction model. In addition, research on the causes of diarrhea in weaned piglets is mostly a single factor exploration, lacking multi-factor integrated analysis.
[0005] Since the occurrence of post-weaning piglet diarrhea is affected by many factors, including feeding management, feed composition, environmental hygiene, etc., the accurate acquisition and integration of these data is crucial to the establishment of an effective prediction model. Due to the lack of analysis of a large amount of diverse data, the existing prediction models may not accurately capture all factors that affect the risk of post-weaning piglet diarrhea, resulting in limited reliability of the prediction results. Summary of the invention
[0006] The present invention proposes a method for predicting the risk level of diarrhea in weaned piglets to solve the technical problem that the existing method for evaluating diarrhea in weaned piglets cannot accurately capture all factors affecting the risk of diarrhea in piglets after weaning, resulting in limited reliability of prediction results.
[0007] In order to solve the above technical problems, the present invention provides a method for predicting the risk level of diarrhea in weaned piglets, comprising the following steps:
[0008] Step S1: Collect diarrhea data of weaned piglets;
[0009] The weaned piglet diarrhea data include independent variables and dependent variables;
[0010] The independent variables include: piglet source farm, test site, test start age, weaning age, number of pigs per pen, initial average weight, crude protein level, NE level, SID Lly, temperature extremes and pig transportation time; the dependent variable is diarrhea rate;
[0011] Step S2: training several machine learning models based on the weaned piglet diarrhea data;
[0012] Step S3: Evaluate the several machine learning models after training, and select the best machine learning model to predict the risk level of diarrhea in weaned piglets.
[0013] Preferably, before training in step S2, the diarrhea data of weaned piglets is preprocessed: the categorical variables in the independent variables are encoded.
[0014] Preferably, before training in step S2, the diarrhea data of weaned piglets is preprocessed: the numerical variables in the independent variables are normalized:
[0015]
[0016] In the formula, Z is the standardized data, X is the original value, μ is the mean, and σ is the standard deviation.
[0017] Preferably, before training in step S2, the weaned piglet diarrhea data is evaluated for data balance:
[0018]
[0019] like If the number of minority class samples is not within the set threshold, random upsampling is used to increase the number of minority class samples to adjust
[0020] Preferably, the machine learning model in step S2 includes: decision tree classifier, support vector classifier, XGB classifier, naive Bayes algorithm, random forest classifier, K nearest neighbor classifier, gradient boosting classifier, adaptive boosting classifier, self-help aggregation classifier and logistic regression classifier.
[0021] Preferably, the method for training the gradient boosting classifier GBC in step S2 includes:
[0022] Step S21: construct the independent variables and dependent variables of the weaned piglet diarrhea data into a training data set D = {(x1, y1), (x2, y2), ..., (x N ,y N )},in X is the input space, i.e., the independent variable, and n represents the number of independent variables. Y is the output space, i.e., the dependent variable;
[0023] Step S22: setting the number of categories k of the risk level of diarrhea in weaned piglets;
[0024] Step S23: inputting the training data set into a gradient boosting classifier GBC; constructing k regression trees, each of which corresponds to a risk category of diarrhea risk level in weaned piglets;
[0025] Step S24: converting the output result of each regression tree into a probability value through an activation function, and outputting a prediction result;
[0026] Step S25: Repeat steps S23 to S24 to iterate, taking the cross entropy loss function as the objective function, and then updating the weight parameters based on the gradient descent method to find the corresponding weight parameters when the objective function reaches the minimum value, and obtain the trained gradient boosting classifier GBC.
[0027] Preferably, the expression of the prediction result in step S24 is:
[0028]
[0029]
[0030] In the formula, represents the predicted category of the diarrhea data of the i-th weaned piglet; p i It is a k-dimensional vector, each component is between 0 and 1, indicating the probability value of the model predicting a certain label; represents the k outputs of k regression trees on the i-th weaned piglet diarrhea data; x i is the diarrhea data of the ith weaned piglet; F m (x i ) represents the output of the mth regression tree on the i-th sample.
[0031] Preferably, in step S25, the cross entropy loss function is modified to:
[0032]
[0033] In the formula, represents the predicted label; y represents the true label; y i Represents the true risk classification label of the diarrhea data of the i-th weaned piglet.
[0034] Preferably, the method for updating the weight parameters based on the gradient descent method in step S25 includes:
[0035] Step S251: The regression tree fits the negative gradient of the current loss function, updates the weight parameters along the gradient direction, and calculates the negative gradient r of the loss function of the current iteration round. m,i :
[0036]
[0037] Step S252: m,i Train the regression tree and get the leaf node region R of the mth regression tree m,j , j = 1, 2, ..., J, J represents the number of leaf nodes, then the fitting value of each leaf node of the mth regression tree is:
[0038]
[0039] The above formula can be approximated as:
[0040]
[0041] Step S253: Update using the following formula:
[0042] F t (x) = F t-1 (x)+v*∑c m,j I(x i ∈R m,j );
[0043] Where t is the number of decision trees generated; coefficient v represents the learning rate; and I(·) represents the indicator function.
[0044] Preferably, the method for evaluating the machine learning model in step S3 includes: calculating the accuracy, precision, recall and F1 score based on the confusion matrix; and calculating the arithmetic mean of the accuracy, recall and F1 score respectively; evaluating the calculated data to obtain the best machine learning model.
[0045] The beneficial effects of the present invention include at least: on the basis of existing research, the present invention integrates multiple factors such as environmental parameters, physiological indicators and nutritional intake of weaned piglets, trains and evaluates several machine learning models, and then selects the optimal machine learning model for data analysis and diarrhea risk level identification. By identifying and evaluating different diarrhea risk levels of weaned piglets, pig producers can better manage pig herds, optimize the feeding environment, adjust feed formulas, and implement targeted health management measures to reduce the occurrence of diarrhea in weaned piglets, improve breeding efficiency and economic benefits, and provide technical support for breeders to more accurately predict disease risks, optimize feed formulas and improve feeding management measures, thereby effectively reducing the risk of diarrhea rate in post-weaned piglets. BRIEF DESCRIPTION OF THE DRAWINGS
[0046] Figure 1 A schematic diagram of a method flow chart of an embodiment of the present invention;
[0047] Figure 2 A schematic diagram of a confusion matrix for model evaluation according to an embodiment of the present invention;
[0048] Figure 3 This is a summary diagram of the global feature importance ranking of an embodiment of the present invention;
[0049] Figure 4 This is a SHAP diagram of an embodiment of the present invention. DETAILED DESCRIPTION
[0050] The following is a clear and complete description of the technical solutions in the embodiments of the present invention in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work belong to the protection scope of the present invention.
[0051] like Figure 1 As shown, the embodiment of the present invention provides a method for predicting the risk level of diarrhea in weaned piglets, comprising the following steps:
[0052] Step S1: Collect diarrhea data of weaned piglets;
[0053] The data on diarrhea in weaned piglets include independent and dependent variables;
[0054] The independent variables included: piglet source farm, experimental site, experimental start age, weaning age, number of pigs per pen, initial average weight, crude protein level, NE level, SID Lly, temperature extremes and pig transportation time; the dependent variable was diarrhea rate.
[0055] Step S2: Based on the diarrhea data of weaned piglets, several machine learning models are trained.
[0056] Step S3: Evaluate several trained machine learning models and select the best machine learning model to predict the diarrhea risk level of weaned piglets.
[0057] Specifically, in step S1, this embodiment divides the independent variables in the weaned piglet diarrhea data into 2 categorical variables and 9 numerical variables.
[0058] Among them, the numerical variables include the age at the start of the experiment, weaning age, number of pigs in each pen, initial average weight, crude protein level, NE level, SID Lly, temperature extremes and pig transportation time; the categorical variables include the piglet source farm and the experimental site.
[0059] In this embodiment, in order to combine with the machine learning model, the two categorical variables are label encoded: the piglet source farm includes 10 categories: Farm No. 1, Farm No. 2, Farm No. 3, Farm No. 4, Farm No. 5, Farm No. 6, Farm No. 7, Farm No. 8, Farm No. 9 and Farm No. 10, and each category is encoded as 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 in sequence; the experimental site includes 4 categories: Experimental Site No. 1, Experimental Site No. 2, Experimental Site No. 3 and Experimental Site No. 4, and each category is encoded as 0, 1, 2, 3 in sequence.
[0060] The dependent variable diarrhea rate in this embodiment is a numerical variable, which is divided into three levels according to the size of the diarrhea rate: diarrhea rate = 0 is coded as class '0', 0 < diarrhea rate < 5% is coded as class '1', and diarrhea rate ≥ 5% is coded as class '2'.
[0061] Since the number of samples with diarrhea rate ≥ 5% in the data set of this embodiment is significantly less than that of other categories, class imbalance occurs. In classification problems, the significant difference in the number of samples in different categories will lead to problems such as poor model generalization ability and inaccurate model performance evaluation.
[0062] Therefore, before entering step S2 for training, the data balance of weaned piglet diarrhea data is evaluated:
[0063]
[0064] like If the number of minority class samples is not within the set threshold, random upsampling is used to increase the number of minority class samples to adjust To achieve data balance. During random upsampling, some samples are randomly selected from the minority class for replication until the number of samples in the minority class and the majority class are close, that is, within the set threshold range.
[0065] The data scales of different independent variables vary greatly, which may lead to unstable numerical calculations. Therefore, in this implementation, the original data set is standardized so that all variables are on the same dimension and the scale differences between data are reduced. The expression is as follows:
[0066]
[0067] In the formula, Z is the standardized data, X is the original value, μ is the mean, and σ is the standard deviation.
[0068] Specifically, in step S2, a machine learning method is used to predict the diarrhea risk level of weaned piglets, and the following 10 classification model algorithms are used to model in Python using the scikit-learn library and the xgboost library: Decision Tree Classifier, Support Vector Classifier, XGB Classifier, Gaussian NB, Random Forest Classifier, K Neighbors Classifier, Gradient Boosting Classifier, AdaBoost Classifier, Bagging Classifier and Logistic Regression. The following is a brief overview of the 10 classification model algorithms:
[0069] 1.Decision Tree Classifier
[0070] The decision tree classifier is a classification model that makes decisions based on a tree structure. It divides data into different categories by selecting features and splitting them at each node. The decision tree includes three core concepts: nodes: each node represents a feature, which is used to divide the input data; branches: the path from the root node to the child node represents the decision rule, which is used to divide the data into different child nodes; leaves: each leaf represents a category, which is used to classify the input data. When using the decision tree algorithm for classification, first randomly select a feature from the entire data set as the root node; secondly, divide the data set into multiple subsets based on the selected features; then recursively apply the above steps to each subset until the stopping condition is met; finally, return the constructed decision tree. The stopping conditions include that the samples in the node belong to the same category, all features have been used, the number of remaining samples reaches the threshold, and the preset maximum depth is reached.
[0071] 2.Support Vector Classifier
[0072] The basic principle of support vector classifier is to find a decision boundary that can maximize the interval between two categories. When constructing a support vector classifier, first find a hyperplane in the feature space to maximize the interval between different categories; secondly, since the data is nonlinearly separable in the original feature space, the radial basis function is used to map the data to a high-dimensional space to calculate the classifier. Support vector classifiers are usually used to solve binary classification problems. Here, the expansion from binary classification to multi-classification is achieved based on the one-against-rest (OVR) strategy. The OVR method refers to establishing a binary classifier by taking one of the categories as a positive sample and the other categories as negative samples. If the dependent variable includes n categories, n binary classifiers will be obtained. Finally, the classification is determined based on the scores of the n classifiers. The positive sample category corresponding to the classifier with the highest score is the predicted category of the test sample.
[0073] 3.XGB Classifier
[0074] XGB Classifier is an efficient classifier based on the gradient boosting framework, which combines multiple weak classifiers (usually decision trees) to improve prediction accuracy. When building the XGB classifier, the model is first initialized; then new decision trees are added iteratively. In each iteration, the prediction residual of the current model needs to be calculated, and a new decision tree is built based on the residual. The new tree is linearly combined with the current model to form a new model; finally, the best features and split points are selected for each tree based on feature importance and gradient information, and the branches of the tree are recursively constructed until the preset maximum depth is reached or other stopping conditions are met.
[0075] 4.Gaussian NB
[0076] The Naive Bayes algorithm is a classification algorithm based on Bayes' theorem and the conditional independence assumption of features. It assumes that all features are independent under a given category. The core idea of the Naive Bayes algorithm is: for a given data set, based on the conditional independence assumption of features, the probability of occurrence of each feature is multiplied by the prior probability of the target category to obtain the posterior probability that the data belongs to the target category, and the category with the largest posterior probability is selected as the classification result of the data.
[0077] 5. Random Forest Classifier
[0078] The random forest classifier is composed of multiple decision trees, and these trees are independent during training. Each decision tree will make a prediction for the input data, and then the random forest integrates the prediction results of each tree by majority voting, and the category with the most votes is used as the final prediction category. To build a random forest classifier, first use a random sampling strategy with replacement in the original data set, sample m times continuously, and get a sampling set; secondly, randomly select a part of all the features in the data set, and use the sampled data to train the decision tree classification model; then repeat the above two steps n times to get n decision tree classification models; finally, put the new data into the n decision tree classification models, and the category with the most votes is the final category.
[0079] 6.KNeighbors Classifier
[0080] The K nearest neighbor classifier uses proximity to make classification predictions for a single data point. The basic idea is: given a training data set, for a new input sample, calculate the distance between it and all samples in the training set, select the k closest samples based on the calculated distance, and use the category that appears most frequently among the k nearest samples as the predicted category.
[0081] 7.Gradient Boosting Classifier
[0082] Gradient boosting classifier is an ensemble learning algorithm that builds a powerful classification model by combining multiple weak classifiers. The core idea is to gradually add weak classifiers. Each new classifier will correct the prediction error based on the previous classifier, thereby gradually improving the prediction accuracy of the model. The main steps to build a gradient boosting classifier are: first initialize a decision tree base model; secondly, perform iterative training. In each round of iteration, the negative gradient of the loss function is calculated based on the previously established model, and these negative gradients are used to train a new weak classifier, such as a decision tree, linear regression, etc.; then the trained new classifier is added to the previous classifier, and the model is updated to reflect the prediction results of the new classifier; repeat the above process until the preset number of iterations is reached or the preset accuracy requirement is met.
[0083] 8.AdaBoost Classifier
[0084] Adaptive boosting classifier is an ensemble learning algorithm that builds a strong classifier by combining multiple weak classifiers. The main steps to build an adaptive boosting classifier are: first, give each sample in the training set the same weight; second, perform iterative training. Each iteration, the algorithm will train a weak classifier based on the current weight, and adjust the weight of the training sample in the next iteration according to the error rate of the current weak classifier, so that the previously misclassified samples will receive a higher weight in subsequent iterations; then, the weight of the classifier is calculated based on the error rate of each weak classifier. The lower the error rate, the greater the weight. Repeat the above process until the predetermined number of iterations is reached or the performance of the classifier is no longer significantly improved; finally, all weak classifiers are combined into a strong classifier by weighted voting.
[0085] 9.Bagging Classifier
[0086] The self-service aggregate classifier is an ensemble learning method that improves the generalization ability of the model by building multiple base classifiers and integrating their prediction results. The main steps to build a self-service aggregate classifier are: first, based on the self-service sampling method, perform replacement sampling from the original training set to generate multiple different training subsets; second, train the base classifier independently on each subset; finally, through majority voting, the prediction results of each base classifier are combined to form the final decision.
[0087] 10.Logistic Regression
[0088] The logistic regression classifier is a generalized linear regression analysis model that uses the logarithmic probability function as a link function. The output is a probability value. When the probability output by the model is greater than the preset threshold, the sample is judged as a positive class, otherwise it is judged as a negative class. When dealing with multi-classification problems, the logistic regression classifier uses the OVR strategy to expand the binary classification model to multi-classification. This method divides the multi-classification problem into multiple binary classification problems to solve. The main steps to build a logistic regression classifier are: suppose the data set is a k-classification problem, create a logistic regression binary classifier for each category, mark the target category as a positive class, and mark other categories as negative classes, and finally get k binary classifiers; secondly, predict the samples in the test set based on each binary classifier to get the probability of the current positive class; finally, select the classifier with the highest calculation result, and its positive class is the prediction result.
[0089] Exemplarily, for the above 10 classification model algorithms, hyperparameter tuning is required to improve the generalization ability of the model and obtain the optimal model. When adjusting the hyperparameters, the approximate values of the hyperparameters with great influence, such as the learning rate and n_estimators, can be determined by drawing the learning curve. The specific operation is: first generate a value range with a larger step size, and then record the model scores on the training set and the test set in a loop, where the test set score is the average of the 5-fold cross-validation score, which can better reflect the generalization ability of the model, and finally draw the learning curve to observe its approximate value. In addition, a set of hyperparameter grids is defined for each classification model, that is, the value range and step size of the parameters in the model are set, and then the model training and evaluation are performed on each set of parameters based on the grid search method, and finally the best performing parameter combination is selected.
[0090] Since the gradient boosting classifier GBC has the best prediction effect on the risk level of diarrhea in weaned piglets in the comparative evaluation of the prediction performance of different classification models in this embodiment, the GBC modeling process will be further described below.
[0091] The establishment of a GBC-based weaned piglet diarrhea risk level prediction model specifically includes the following processes:
[0092] The independent variables and dependent variables of the diarrhea data of weaned piglets are constructed as a training data set. The training data set D = {(x1, y1), (x2, y2), ..., (x N ,y N )},in X is the input space, i.e., the independent variable, n is the number of independent variables, and N is the number of diarrhea data in weaned piglets. Y is the output space, i.e., the dependent variable.
[0093] Label pre-encoding: During the model training phase, the labels are first one-hot encoded to convert the categorical variables into a form that the machine learning algorithm can better handle. It represents each category as a binary vector, with all positions 0 except for one position representing the category, which is 1.
[0094] Define the output function: If there are k categories in the classification task, k regression trees will be constructed in each iteration process, each regression tree corresponds to a category, and the output result of each tree is converted into a probability value through an activation function, and the label corresponding to the maximum probability value is the prediction result. In this embodiment, the risk level of diarrhea in weaned piglets is a 3-classification task, so 3 regression trees will be constructed in each iteration. At the same time, the classification task is a multi-classification prediction, and the activation function is no longer the sigmoid function in the binary classification problem, so the softmax function is used as the activation function. Assume that in the t-th iteration, the outputs of the k regression trees on the i-th sample are: Then the output function can be defined as:
[0095]
[0096] In the formula, represents the predicted category of the diarrhea data of the i-th weaned piglet; p i It is a k-dimensional vector, each component is between 0 and 1, indicating the probability value of the model predicting a certain label; represents the k outputs of k regression trees on the i-th weaned piglet diarrhea data; x i is the diarrhea data of the ith weaned piglet; F m (x i ) represents the output of the mth regression tree on the i-th sample.
[0097] In the formula, represents the predicted category of the diarrhea data of the i-th weaned piglet, that is, the category corresponding to the maximum probability; p i It is a k-dimensional vector, each component is between 0 and 1, indicating the probability value of the model predicting a certain label; k represents the number of categories; represents the k outputs of k regression trees on the i-th weaned piglet diarrhea data; x i is the diarrhea data of the ith weaned piglet; F m (x i ) represents the output of the mth regression tree on the i-th sample.
[0098] Definition of objective function: The cross entropy loss function (logarithmic loss) is used as the objective function. Since the risk level of diarrhea in weaned piglets in this embodiment is a multi-classification task, this embodiment improves the loss function on the basis of binary classification to adapt to the classification task of the risk level of diarrhea in weaned piglets, as shown in formula (3):
[0099]
[0100] Substituting formula (4) into formula (3), we get the loss function as follows:
[0101]
[0102] Among them, y has been one-hot encoded, so The loss function can be simplified as:
[0103]
[0104] Then, the weight parameters are iteratively updated based on the gradient descent method to find the corresponding weight parameters that minimize the objective function.
[0105] Negative gradient calculation: In the next iteration, the regression tree will try to fit the negative gradient of the current loss function, update the weight parameters along the gradient direction, and calculate the negative gradient of the loss function of the current iteration:
[0106]
[0107] Among them, r m,i represents the negative gradient of the loss function of the mth regression tree, and i represents the i-th sample. The above results show that the negative gradient of the loss function of the previous strong classifier fitted by the current classifier is equivalent to the difference between the fitted observation value and the predicted value.
[0108] Training model: m,i Train the regression tree and get the leaf node region R of the mth tree m,j , j=1,2,...,J, j represents a leaf node, J represents the number of leaf nodes, then the fitting value of each leaf node of the mth decision tree is:
[0109]
[0110] The above formula can be approximately expressed as:
[0111]
[0112] Update the strong learner:
[0113] F t (x) = F t-1 (x)+v*∑c m,j I(x i ∈R m,j )(10)
[0114] Where t is the number of decision trees generated; the coefficient v represents a regularization method. In order to prevent overfitting, each time the residual estimation is iterated, the currently fitted residual is not directly added, but multiplied by a coefficient v. The coefficient v is also called the learning rate. It can adjust the step size of the gradient boosting and control the contribution of the weak classifier to the final model in each iteration. It is between 0 and 1 and needs to be determined by the grid search method. I(·) represents the indicator function.
[0115] The above steps are iterated M times to obtain the final strong learner F M (x).
[0116] Prediction: Assume that the new input sample is x new , then the predicted category can be expressed as:
[0117]
[0118] In the formula, FM,m (x new ) represents the final strong learner F M (x) The mth regression tree in sample x new Output.
[0119] In order to complete the task of predicting the risk level of diarrhea in weaned piglets in this embodiment, the adjustments in the above modeling process are: adjusting the loss function calculation formula to adapt to multi-classification tasks; changing the activation function type to output multiple diarrhea risk levels; adding a regularization penalty term, namely the learning rate, in each round of iteration to prevent overfitting. The learning rate controls the contribution of the weak classifier to the final model in each iteration. A smaller learning rate makes the model learning process more robust.
[0120] Specifically, in step S3, in order to select the best machine learning model, this embodiment evaluates several machine learning models after training and also provides the following method.
[0121] Use accuracy, precision, recall, and F1 score to evaluate the predictive performance of different classification models and select the model with the best classification effect.
[0122] The above evaluation indicators are calculated based on the confusion matrix. The confusion matrix is shown in the following table:
[0123] Table 1: Confusion Matrix
[0124]
[0125] The confusion matrix takes binary classification as an example, where event 1 is called "positive" and event 0 is called "negative". The confusion matrix divides the classification results into four categories: TP (True Positive) is a true positive example, indicating that the true value of the sample is "positive" and the model prediction value is also "positive"; TN (True Negative) is a true negative example, indicating that the true value of the sample is "negative" and the model prediction value is also "negative"; FP (False Positive) is a false positive example, indicating that the true value of the sample is "negative" and the model prediction value is "positive"; FN (False Negative) is a false negative example, indicating that the true value of the sample is "positive" and the model prediction value is "negative".
[0126] Accuracy: refers to the proportion of samples that the model predicts correctly to the total number of samples. It may produce misleadingly high scores on class-imbalanced datasets, even if the model predicts poorly for the minority class. The calculation formula is as follows:
[0127]
[0128] Precision: refers to the proportion of samples that are actually positive among all samples predicted by the model to be positive. It reflects the credibility of the results predicted by the model to be positive. It is also called "precision rate" and does not consider positive samples that are misclassified as negative. The calculation formula is as follows:
[0129]
[0130] Recall: refers to the proportion of samples that are correctly predicted as positive by the model among all samples that are actually positive. It reflects the ability of the model to capture positive samples. It is also called "recall rate" and does not consider negative samples that are incorrectly classified as positive. The calculation formula is as follows:
[0131]
[0132] F1 score: The F1 score is the harmonic mean of precision and recall. It takes both precision and recall into account and is a comprehensive performance indicator. When precision and recall are equally important, the F1 score can be selected as the main evaluation indicator of model performance. The calculation formula is as follows:
[0133]
[0134] Since the dependent variable diarrhea grade here is divided into three categories, the calculation formula of the above indicators needs to be further expanded. The calculation method of accuracy remains unchanged, and the three indicators of precision, recall and F1 score need to be calculated using macro-average. Macro-average means calculating indicators independently for each category, and then taking the average of all category indicators. Specifically: first calculate the precision, recall and F1 score for each category separately; secondly calculate the arithmetic mean of the precision, recall and F1 scores of all categories, and the obtained value is the final value of each indicator.
[0135] In the specific implementation of the embodiment of the present invention, the data comes from 831 complete weaned piglet test data records from 10 sow farms of a company from December 2019 to December 2022, including piglet source farm, test site, test start age, weaning age, number of pigs per pen, initial average weight, crude protein level, NE level, SID Lly, temperature extremes, pig transportation time and diarrhea rate data records.
[0136] The data set contains 11 independent variables and 1 dependent variable. The independent variables include 2 categorical variables: piglet source farm and test site, 9 numerical variables: age at the start of the test, weaning age, number of pigs per pen, initial average weight, crude protein level, NE level, SID Lly, temperature extremes and pig transportation time. The dependent variable diarrhea rate is a numerical variable, which is divided into three levels according to the value of diarrhea rate: diarrhea rate = 0 is coded as '0', 0 < diarrhea rate < 5% is coded as '1', and diarrhea rate ≥ 5% is coded as '2'.
[0137] As shown in Table 2, the number of samples with diarrhea rate ≥ 5% in the dataset is significantly less than that of other categories, resulting in class imbalance. In order to improve the generalization ability of the model, a random upsampling method is used to increase the number of minority class samples, thereby achieving data balance.
[0138] Table 2: Number of diarrhea rates in different categories
[0139]
[0140] In addition, different independent variables in the data set have different dimensions, such as degrees Celsius, days, and minutes. At the same time, the data scales of different independent variables are quite different and cannot be directly compared. Therefore, standardization is used to make all variables on the same dimension and reduce the scale differences between data.
[0141] The hyperparameters of the above 10 classification model algorithms were adjusted to obtain the model with the best performance. Table 3 shows the hyperparameter tuning results of each model. The data set was randomly divided into two parts, 80% of which was used as a training set and 20% as a test set.
[0142] Table 3: Hyperparameter tuning results
[0143]
[0144] The effectiveness of different models in predicting diarrhea grades in weaned piglets was evaluated using four indicators: accuracy, precision, recall, and F1 score. The higher the values of these indicators, the better the performance of the model. The higher the accuracy value, the higher the proportion of samples correctly classified by the model, and the better the overall classification performance; the higher the precision value, the higher the credibility of the results predicted by the model as positive, that is, the higher the proportion of samples predicted by the model as positive; the higher the recall value, the more actual positive samples can be identified by the model, and the fewer false negatives; the higher the F1 score value, the better the model has achieved a balance between precision and recall, and the better the overall performance.
[0145] The data set was divided into a training set (80%) and a test set (20%). The above classification models were used to train and learn on the training set, and the diarrhea grade of weaned piglets was predicted on the test set. The various evaluation indicators were calculated and compared to select the best classification model. Table 4 presents the prediction effects of 10 classification models based on the four evaluation indicators of Accuracy, Precision, Recall and F1 score.
[0146] Table 4: Comparison of prediction performance of 10 models
[0147]
[0148] From the results in Table 4, we can see that the gradient boosting classifier (GBC) has the best prediction effect on the diarrhea grade of weaned piglets, with accuracy, precision, recall and F1 score of 0.814, 0.808, 0.813 and 0.807 respectively, and the four index values are higher than other classification models; the decision tree classifier (DTC) has the second best prediction effect, with the four index values of 0.810, 0.804, 0.808 and 0.802 respectively; the model prediction performance ranks third in the XGB classifier, with the four index values of 0.797, 0.791, 0.796 and 0.792 respectively. The prediction effects of other classification models are inferior to the above three models.
[0149] When evaluating the prediction effect of the classification model, the above evaluation indicators of accuracy, precision, recall and F1 score provide a multi-dimensional quantitative analysis of the prediction performance of the model. However, these indicators may lack intuitiveness in explaining the overall performance of the model. Therefore, based on the evaluation indicators, this embodiment further displays the relationship between the model prediction results and the actual labels by drawing confusion matrix diagrams of the three classification models of GBC, DTC and XGBC. Figure 2 As shown in the figure, a, b, and c are the confusion matrix diagrams of the three classification models of GradientBoosting Classifier (GBC), Decision Tree Classifier (DTC), and XGB Classifier (XGBC), respectively. In the confusion matrix diagram, each row represents the actual category, each column represents the predicted category, and the value on the diagonal refers to the number of correct predictions of each category by the model. The larger the value, the better the model prediction effect. In addition, the larger the value on the confusion matrix diagram, the darker the corresponding color, that is, the darker the diagonal color and the lighter the non-diagonal color, the better the model prediction effect. Figure 2 The results showed that the GBC, DTC and XGBC models were effective in predicting diarrhea grade in weaned piglets.
[0150] This example draws a global feature importance ranking summary diagram to further measure the contribution of each feature to the model output. Figure 3As shown in the figure, the summary graph can show the impact of different features on each category and their contribution to the overall performance of the model. The length of the bar in the figure is the average absolute value of SHAP of all samples, and its length represents the overall impact of the feature on the model prediction. The longer the bar, the greater the impact of the feature. At the same time, the summary graph can observe the feature importance of each category, and each color represents a category. Figure 3 The results showed that temperature extremes, crude protein levels, NE levels and initial average weight played an important role in predicting the diarrhea grade of weaned piglets; temperature extremes and crude protein levels had a key influence on predicting the diarrhea grade of weaned piglets as "Class 2"; initial average weight and crude protein levels had a key influence on predicting the diarrhea grade of weaned piglets as "Class 1"; temperature extremes and NE levels had a key influence on predicting the diarrhea grade of weaned piglets as "Class 0".
[0151] By drawing a force plot such as Figure 4 As shown, it is possible to observe from a microscopic perspective how a single prediction result is generated, and to explain how each feature affects a single output of the model. In the SHAP force diagram of a specific category, the length of the bar corresponding to each feature represents the contribution of the feature to the model's predicted value. Red indicates that an increase in the feature value tends to increase the predicted probability of the category, and blue indicates that an increase in the feature value tends to decrease the predicted probability of the category; the baseline value represents the average of the sample prediction values, and f(x) represents the predicted value of a certain sample, which is the sum of the baseline value plus the contribution of all features. This example selects 3 representative samples and plots the diarrhea grade of weaned piglets as "category 0" in turn. Figure 4 (a) and "Class 1" in Figure 4 (b) and "Category 2" in Figure 4 The results of the force graph in (c) show that the probability of the diarrhea grade of weaned piglets being "Class 0" predicted by the model with a tendency to reduce NE level and temperature extreme difference; the probability of the diarrhea grade of weaned piglets being "Class 1" predicted by the model with a tendency to reduce initial average weight, crude protein level and temperature extreme difference; and the probability of the diarrhea grade of weaned piglets being "Class 2" predicted by the model with a tendency to increase initial average weight, temperature extreme difference and crude protein level.
[0152] The technical features of the above embodiments can be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. Only the preferred embodiments of the present invention are expressed. The description is more specific and detailed, but it cannot be understood as limiting the scope of the patent of the present invention. As long as there is no contradiction in the combination of these technical features, they should be considered as the scope recorded in this specification.
[0153] It should be noted that, for those skilled in the art, several modifications and improvements can be made without departing from the concept of the present invention, and these modifications and improvements all belong to the protection scope of the present invention. Therefore, the protection scope of the patent of the present invention shall be subject to the attached claims.
Claims
1. A method for predicting the risk level of diarrhea in weaned piglets, characterized in that: The following steps are involved: Step S1: Collect diarrhea data of weaned piglets; The weaned piglet diarrhea data include independent variables and dependent variables; The independent variables include: piglet source farm, test site, test start age, weaning age, number of pigs per pen, initial average weight, crude protein level, NE level, SID Lly, temperature extremes and pig transportation time; the dependent variable is diarrhea rate; Step S2: training several machine learning models based on the weaned piglet diarrhea data; Step S3: Evaluate the several machine learning models after training, and select the best machine learning model to predict the risk level of diarrhea in weaned piglets.
2. The method for predicting the risk level of diarrhea in weaned piglets according to claim 1, characterized in that: Before training in step S2, the diarrhea data of weaned piglets is preprocessed: the categorical variables in the independent variables are encoded.
3. The method for predicting the risk level of diarrhea in weaned piglets according to claim 1, characterized in that: Before training in step S2, the diarrhea data of weaned piglets is preprocessed: the numerical variables in the independent variables are normalized: In the formula, Z is the standardized data, X is the original value, μ is the mean, and σ is the standard deviation.
4. The method for predicting the risk level of diarrhea in weaned piglets according to claim 1, characterized in that: Before training in step S2, the data balance of the weaned piglet diarrhea data is evaluated: like If the number of minority class samples is not within the set threshold, random upsampling is used to increase the number of minority class samples to adjust 5. The method for predicting the risk level of diarrhea in weaned piglets according to claim 1, characterized in that: The machine learning models described in step S2 include: decision tree classifier, support vector classifier, XGB classifier, naive Bayes algorithm, random forest classifier, K nearest neighbor classifier, gradient boosting classifier, adaptive boosting classifier, self-help aggregation classifier and logistic regression classifier.
6. A method for predicting the risk level of diarrhea in weaned piglets according to claim 5, characterized in that: The method for training the gradient boosting classifier GBC in step S2 includes: Step S21: construct the independent variables and dependent variables of the weaned piglet diarrhea data into a training data set D = {(x1, y1), (x2, y2), ..., (x N ,y N )},in X is the input space, i.e., the independent variable, and n represents the number of independent variables. Y is the output space, i.e., the dependent variable; Step S22: setting the number of categories k of the risk level of diarrhea in weaned piglets; Step S23: inputting the training data set into a gradient boosting classifier GBC; constructing k regression trees, each of which corresponds to a risk category of diarrhea risk level in weaned piglets; Step S24: converting the output result of each regression tree into a probability value through an activation function, and outputting a prediction result; Step S25: Repeat steps S23 to S24 to iterate, taking the cross entropy loss function as the objective function, and then updating the weight parameters based on the gradient descent method to find the corresponding weight parameters when the objective function reaches the minimum value, and obtain the trained gradient boosting classifier GBC.
7. A method for predicting the risk level of diarrhea in weaned piglets according to claim 6, characterized in that: The expression of the prediction result in step S24 is: In the formula, represents the predicted category of the diarrhea data of the i-th weaned piglet; p i It is a k-dimensional vector, each component is between 0 and 1, indicating the probability value of the model predicting a certain label; k represents the number of categories; represents the k outputs of k regression trees on the i-th weaned piglet diarrhea data; x i is the diarrhea data of the ith weaned piglet; F m (x i ) represents the output of the mth regression tree on the i-th sample.
8. A method for predicting the risk level of diarrhea in weaned piglets according to claim 7, characterized in that: In step S25, the cross entropy loss function is modified to: In the formula, represents the predicted label; y represents the true label; y i Represents the true risk classification label of the diarrhea data of the i-th weaned piglet.
9. A method for predicting the risk level of diarrhea in weaned piglets according to claim 8, characterized in that: The method for updating the weight parameters based on the gradient descent method in step S25 includes: Step S251: The regression tree fits the negative gradient of the current loss function, updates the weight parameters along the gradient direction, and calculates the negative gradient r of the loss function of the current iteration round. m,i : Step S252: m,i Train the regression tree and get the leaf node region R of the mth regression tree m,j , j = 1, 2, ..., J, J represents the number of leaf nodes, then the fitting value of each leaf node of the mth regression tree is: The above formula can be approximated as: Step S253: Update using the following formula: F t (x)=F t-1 (x)+v*∑c m,j I(x i ∈R m,j ); Where t is the number of generated decision trees; coefficient v represents the learning rate; I(·) represents the indicator function.
10. The method for predicting the risk level of diarrhea in weaned piglets according to claim 1, characterized in that: The method for evaluating the machine learning model in step S3 includes: calculating the accuracy, precision, recall and F1 score based on the confusion matrix; and calculating the arithmetic mean of the accuracy, recall and F1 score respectively; evaluating the calculated data to obtain the best machine learning model.