Food safety risk early warning method and system based on optimized random forest
By using MLSSA to optimize the random forest model in the food safety risk warning system, the problem of low prediction accuracy under unbalanced data is solved, and more efficient feature selection and more accurate risk warning are achieved.
Patent Information
- Application Number
- CN202411827516.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-12
- Publication Date
- 2025-04-29
AI Technical Summary
When facing unbalanced data, the existing food safety risk warning system has low prediction accuracy and lacks intelligence in feature selection.
The food safety risk warning method based on optimization of random forests is adopted, and the hyperparameter θ in the RF model is optimized through MLSSA and the feature weight w is dynamically adjusted, and the MLSSA-RF model is constructed, and the training and testing is performed, and the high-risk combination that affects the vegetable sampling results is output and risk warning is performed.
It improves prediction accuracy, improves the classification effect of the model, can effectively process high-dimensional data, and reduces the interference of redundant features on the model.
Smart Images

Figure CN119904092A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of food safety early warning, and in particular to a food safety risk early warning method and system based on optimized random forest. Background Art
[0002] With the rapid development of the food industry, food safety systems are facing more and more challenges. Rapidly and accurately predicting and warning potential food safety risks has become an important task in food safety management. However, traditional methods may find it difficult to balance analysis efficiency and prediction accuracy in a high-dimensional data environment. Existing food safety risk warning systems have the following main problems: feature selection lacks intelligence, especially when faced with unbalanced data, the prediction accuracy is low. Summary of the invention
[0003] In view of this, the purpose of the present invention is to propose a food safety risk early warning method and system based on optimized random forest to solve the problem of low prediction accuracy when facing unbalanced data.
[0004] Based on the above purpose, the present invention provides a food safety risk early warning method based on optimized random forest, comprising:
[0005] S1: Acquire initial data, preprocess the initial data to obtain a processed first data set, divide the first data set into a training set and a test set according to an 8:2 ratio, and use the SMOTE method to oversample the training set so that the ratio of qualified samples to unqualified samples is 1:1;
[0006] S2: Optimize the hyperparameter θ and dynamically adjust the feature weight w in the RF model through MLSSA to obtain the optimized hyperparameter θ and dynamically adjust the feature weight w;
[0007] S3: Use the MLSSA-optimized θ and feature weight w to build the MLSSA-RF model;
[0008] S4: Train the MLSSA-RF model based on the training set;
[0009] S5: Test the trained MLSSA-RF model based on the test set data. The test uses precision, recall, F1 score, and AUC value indicators. When the test result does not meet the predetermined range, return to step S3. When the test result meets the predetermined range, the training is terminated to obtain the final MLSSA-RF model.
[0010] S6: Based on the historical sampling data of clothianidin in vegetables, the final MLSSA-RF model is used to predict the sampling results of clothianidin in vegetables in the future, output the high-risk combinations that affect the sampling results of vegetables, and issue risk warnings.
[0011] Optionally, the step of optimizing the hyperparameter θ and dynamically adjusting the feature weight w in the RF model by MLSSA to obtain the optimized hyperparameter θ and dynamically adjusting the feature weight w includes:
[0012] Determine the optimization target, which is the hyperparameter θ and the dynamically adjusted feature weight w in the objective function as follows:
[0013] min θ,w F(θ,w)=-score(RF(X,y,θ,w))
[0014] Where: min θ,w represents the minimization operation of the parameter θ and the weight w; F(θ,w) is the objective function; score(RF(X,y,θ,w)) is the scoring function;
[0015] Based on the MLSSA algorithm, a population is randomly generated, each individual in the population represents a combination of a set of hyperparameters θ and a dynamically adjusted feature weight w. The objective function (1) is used as the fitness function, and the classification error is calculated through the test set to guide the population update.
[0016] Use equation (4) to calculate the adaptive step size and update the position according to the hierarchical population;
[0017]
[0018] Where: c represents the step size; α represents the parameter for adjusting dynamic behavior; iter is the current iteration number; Gmax represents the total iteration number;
[0019] The updating of the position according to the stratified population includes: dynamically updating the individual position according to the characteristics of the elite group, the middle group and the tail group, the elite group performs a global search to update the individual position; the middle group performs a neighborhood local search to update the individual position; the tail group protects the diversity of the population to update the individual position;
[0020] Based on the above update method, the MLSSA algorithm adaptively adjusts the four hyperparameters θ of the RF model, including the number of decision trees, the maximum depth of the decision tree, the minimum sample ratio when splitting, and the minimum impurity reduction threshold, to obtain the four optimized hyperparameters θ;
[0021] According to the following formula, the adjusted and optimized feature weight w is obtained by maximizing the weighted Gini index;
[0022]
[0023] Where: Gini w (D,w) is the Gini index after feature weight optimization, reflecting the impurity of the dataset D; K represents the number of categories; wi is the weight of the i-th feature; p i is the probability of the i-th feature; IG(D, A) represents the information gain of feature A; V represents the different levels or categories of feature A; |Dv| represents the number of samples with the value v in feature A; |D| represents the total number of samples; Gini(Dv) represents the Gini impurity calculated in the subset Dv where the value of feature A is v.
[0024] Optionally, the elite group performs a global search to update individual positions based on the following formula:
[0025]
[0026] in, represents the state of the i-th individual in the iteration of time t+1; represents the state of the i-th individual in the time t iteration; Gmax represents the maximum number of iterations allowed; represents the global optimal solution found in the tth generation; N(0,1) represents the standard normal distribution; c(t) represents the control parameter that affects the Levy distribution; Levy(λ) represents the random number generated by the Lévy distribution, whose parameter is λ.
[0027] Optionally, the intermediate group performs a neighborhood local search to update individual positions based on the following formula:
[0028]
[0029] Where A is a binary random number (0 or 1) generated based on the standard normal distribution, which is used to adjust the update amplitude, N(0,1) represents the standard normal distribution; i is the imaginary unit; represents the state of the i-th individual in the time t iteration; represents the global optimal solution found in the tth generation, and rand() is a random number in the range of 0 to 1.
[0030] Optionally, the tail population updates the diversity of individual positions based on the following formula:
[0031]
[0032] Where N(0,1) represents the standard normal distribution; i is the imaginary unit; α is a parameter used to adjust the dynamic behavior; c(t) is a constant. represents the state of the i-th individual in the time t iteration; represents the global optimal solution found in the tth generation, affecting the influence of Levy distribution; Levy(λ) represents the random number generated by Lévy distribution, whose parameter is λ.
[0033] Optionally, step S5 also includes: evaluating the classification performance of the MLSSA-RF model, drawing a confusion matrix diagram, performing feature importance analysis, and obtaining a feature importance ranking of the MLSSA-RF model.
[0034] 7. A food safety risk early warning method based on optimized random forest according to claim 1, characterized in that the MLSSA-RF model is as follows:
[0035]
[0036] in:
[0037]
[0038] Where: mode{...} means voting on the prediction results of all decision trees and returning the category with the highest frequency; tree k (x;θ opt ,w opt ) is the prediction result of the kth decision tree for the input x, θ opt is the optimized parameter vector, which contains the hyperparameters of the decision tree, w opt is the optimized feature weight vector, representing the importance of each feature in the model; k represents the total number of decision trees in the random forest.
[0039] Based on the same invention, the present invention also provides a system for a food safety risk early warning method based on optimized random forest, comprising:
[0040] Data processing module: used to obtain initial data, pre-process the initial data, obtain a processed first data set, divide the first data set into a training set and a test set according to an 8:2 ratio, and use the SMOTE method to oversample the training set so that the ratio of qualified and unqualified samples is 1:1;
[0041] Model optimization module: used to optimize the hyperparameter θ and dynamically adjust the feature weight w in the RF model through MLSSA, obtain the optimized hyperparameter θ and dynamically adjust the feature weight w, and use the MLSSA-optimized θ and feature weight w to build the MLSSA-RF model;
[0042] Training module: used to train the MLSSA-RF model based on the training set;
[0043] Testing module: used to test the trained MLSSA-RF model based on the test set data. The test uses precision, recall, F1 score, and AUC value indicators. When the test result does not meet the predetermined range, return to step S3. When the test result meets the predetermined range, the training is terminated to obtain the final MLSSA-RF model.
[0044] Detection prediction module: Based on the historical sampling data of clothianidin in vegetables, the final MLSSA-RF model is used to predict the future sampling results of clothianidin in vegetables, output the high-risk combinations that affect the vegetable sampling results, and issue risk warnings.
[0045] When using this method to warn of food safety risks, the hyperparameter θ in the existing RF model is optimized by MLSSA and the feature weight w is dynamically adjusted. MLSSA is a method based on adaptive stratified sparrow search optimization (Adaptive Lévy Flight Strategy Optimization Sparrow Search Algorithm). Then, the trained MLSSA-RF model is tested based on the test set data. The test adopts precision, recall, F1 score, and AUC value indicators. When the test result does not meet the predetermined range, return to step S3. When the test result meets the predetermined range, the training is terminated to obtain the final MLSSA-RF model. Based on the historical sampling data of clothianidin in vegetables, the final MLSSA-RF model is used to predict the sampling results of clothianidin in vegetables in the future, output the high-risk combination that affects the sampling results of vegetables, and conduct risk warning. This method combines the MLSSA optimization algorithm with the random forest feature selection mechanism, optimizes the hyperparameter θ and dynamically adjusts the feature weight w to select the features that have the greatest impact on the model performance, improves the overall classification effect of the model, and improves the prediction accuracy. BRIEF DESCRIPTION OF THE DRAWINGS
[0046] In order to more clearly illustrate the technical solutions in the present invention or the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings in the following description are only for the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0047] Figure 1 This is a schematic diagram of a user interface of a food safety risk early warning system according to an embodiment of the present invention;
[0048] Figure 2 A confusion matrix of a prediction model for sampling results of clothianidin in vegetables established based on the MLSSA-RF algorithm in an embodiment of the present invention;
[0049] Figure 3 : is the ROC curve of the MLSSA-RF model of the embodiment of the present invention;
[0050] Figure 4 The feature importance ranking of the MLSSA-RF model according to the embodiment of the present invention is as follows;
[0051] Figure 5It is a high-risk characteristic combination of clothianidin in vegetables according to an embodiment of the present invention;
[0052] Figure 6 The embodiment of the present invention adopts the MLSSA-RF model to predict the results of the random inspection of clothianidin in vegetables. DETAILED DESCRIPTION
[0053] In order to make the objectives, technical solutions and advantages of the present invention more clearly understood, the present invention is further described in detail below in conjunction with specific embodiments.
[0054] It should be noted that, unless otherwise defined, the technical terms or scientific terms used in the present invention should be understood by people with ordinary skills in the field to which the present invention belongs. The "first", "second" and similar words used in the present invention do not indicate any order, quantity or importance, but are only used to distinguish different components. "Include" or "comprise" and similar words mean that the elements or objects appearing before the word include the elements or objects listed after the word and their equivalents, without excluding other elements or objects. "Connect" or "connected" and similar words are not limited to physical or mechanical connections, but may include electrical connections, whether direct or indirect. "Up", "down", "left", "right" and the like are only used to indicate relative positional relationships. When the absolute position of the described object changes, the relative positional relationship may also change accordingly.
[0055] like Figure 1 As shown, a food safety risk early warning method based on optimized random forests includes:
[0056] S1: Acquire initial data, preprocess the initial data to obtain a processed first data set, divide the first data set into a training set and a test set according to an 8:2 ratio, and use the SMOTE method to oversample the training set so that the ratio of qualified samples to unqualified samples is 1:1;
[0057] S2: Optimize the hyperparameter θ and dynamically adjust the feature weight w in the RF model through MLSSA to obtain the optimized hyperparameter θ and dynamically adjust the feature weight w;
[0058] S3: Use the MLSSA-optimized θ and feature weight w to build the MLSSA-RF model;
[0059] S4: Train the MLSSA-RF model based on the training set;
[0060] S5: Test the trained MLSSA-RF model based on the test set data. The test uses precision, recall, F1 score, and AUC value indicators. When the test result does not meet the predetermined range, return to step S3. When the test result meets the predetermined range, the training is terminated to obtain the final MLSSA-RF model.
[0061] S6: Based on the historical sampling data of clothianidin in vegetables, the final MLSSA-RF model is used to predict the sampling results of clothianidin in vegetables in the future, output the high-risk combinations that affect the sampling results of vegetables, and issue risk warnings.
[0062] When using this method to warn of food safety risks, the hyperparameter θ in the existing RF model is optimized by MLSSA and the feature weight w is dynamically adjusted. MLSSA is a method based on adaptive stratified sparrow search optimization (Adaptive Lévy Flight Strategy Optimization Sparrow Search Algorithm). Then, the trained MLSSA-RF model is tested based on the test set data. The test adopts precision, recall, F1 score, and AUC value indicators. When the test result does not meet the predetermined range, return to step S3. When the test result meets the predetermined range, the training is terminated to obtain the final MLSSA-RF model. Based on the historical sampling data of clothianidin in vegetables, the final MLSSA-RF model is used to predict the sampling results of clothianidin in vegetables in the future, output the high-risk combination that affects the sampling results of vegetables, and conduct risk warning. This method combines the MLSSA optimization algorithm with the random forest feature selection mechanism, optimizes the hyperparameter θ and dynamically adjusts the feature weight w to select the features that have the greatest impact on the model performance, improves the overall classification effect of the model, and improves the prediction accuracy.
[0063] In some embodiments, optimizing the hyperparameter θ and dynamically adjusting the feature weight w in the RF model by MLSSA to obtain the optimized hyperparameter θ and dynamically adjusting the feature weight w includes:
[0064] Determine the optimization target, which is the hyperparameter θ in the objective function (see formula (1)) and the dynamically adjusted feature weight w
[0065] min θ,w F(θ,w)=-score(RF(X,y,θ,w)) (1)
[0066] Where: min θ,w It represents the minimization operation of parameters θ and weight w; F(θ,w) is the objective function; score(RF(X,y,θ,w)) is the scoring function.
[0067] Based on the MLSSA algorithm, a population is randomly generated, each individual in the population represents a combination of a set of hyperparameters θ and a dynamically adjusted feature weight w. The objective function (1) is used as the fitness function, and the classification error is calculated through the test set to guide the population update.
[0068] Use equation (4) to calculate the adaptive step size and update the position according to the hierarchical population;
[0069]
[0070] Where: c represents the step size; α represents the parameter for adjusting dynamic behavior; iter is the current number of iterations; Gmax represents the total number of iterations.
[0071] The updating of the position according to the stratified population includes: dynamically updating the individual position according to the characteristics of the elite group, the middle group and the tail group, the elite group (the first 20%) performs a global search to update the individual position; the middle group (20%-80%) performs a neighborhood local search to update the individual position; the tail group (the last 20%) protects the diversity of the population to update the individual position;
[0072] Based on the above updating method, the MLSSA algorithm adaptively adjusts the four hyperparameters θ of the RF model, including the number of decision trees (n_estimators), the maximum depth of the decision tree (max_depth), the minimum sample ratio at splitting (min_samples_leaf), and the minimum impurity reduction threshold (min_impurity_decrease), and obtains the four optimized hyperparameters θ. Taking the data of vegetable clothianidin as an example, the optimal values of the hyperparameters θopt = (n_estimators = 100, max_depth = 11, min_samples_leaf = 0.0243, min_impurity_decrease = 0.0001), wopt = (0.22, 0.18, 0.15, 0.12, 0.1, 0.15, 0.08) are obtained.
[0073] According to formula (9), by maximizing the weighted Gini index, the dynamically adjusted feature weight w is obtained;
[0074]
[0075] In formula (9): Gini w (D,w) is the Gini index after feature weight optimization, reflecting the impurity of the dataset D; K represents the number of categories; w i is the weight of the i-th feature; p iis the probability of the i-th feature; IG(D, A) represents the information gain of feature A; V represents the different levels or categories of feature A; |Dv| represents the number of samples with the value v in feature A; |D| represents the total number of samples; Gini(Dv) represents the Gini impurity calculated in the subset Dv where the value of feature A is v.
[0076] In some embodiments, the elite group (top 20%) performs a global search to update individual positions based on formula (5):
[0077]
[0078] Among them, among them, represents the state of the i-th individual in the iteration of time t+1; represents the state of the i-th individual in the time t iteration; Gmax represents the maximum number of iterations allowed; represents the global optimal solution found in the tth generation; N(0,1) represents the standard normal distribution; c(t) represents the control parameter that affects the Levy distribution; Levy(λ) represents the random number generated by the Lévy distribution, whose parameter is λ.
[0079] In some embodiments, the middle group (20%-80%) performs a neighborhood local search to update individual positions based on formula (6):
[0080]
[0081] Where A is a binary random number (0 or 1) generated based on the standard normal distribution, which is used to adjust the update amplitude, N(0,1) represents the standard normal distribution; i is the imaginary unit; represents the state of the i-th individual in the time t iteration; represents the global optimal solution found in the tth generation, and rand() is a random number in the range of 0 to 1.
[0082] In some embodiments, the tail population (bottom 20%) protects the diversity of the population to update individual positions based on formula (7):
[0083]
[0084] Where N(0,1) represents the standard normal distribution; i is the imaginary unit; α is a parameter used to adjust the dynamic behavior; represents the state of the i-th individual in the time t iteration; represents the global optimal solution found in the tth generation, c(t) is a constant that affects the Levy distribution; Levy(λ) represents the random number generated by the Lévy distribution with parameter λ.
[0085] In some embodiments, step S5 also includes: evaluating the classification performance of the MLSSA-RF model, drawing a confusion matrix diagram, performing feature importance analysis, and obtaining a feature importance ranking of the MLSSA-RF model.
[0086] In some embodiments, the MLSSA-RF model is as follows:
[0087]
[0088] in:
[0089]
[0090] In formula (10), mode{...} means voting on the prediction results of all decision trees and returning the category with the highest frequency; tree k (x;θ opt ,w opt ) is the prediction result of the kth decision tree for the input x, θ opt is the optimized parameter vector, which contains the hyperparameters of the decision tree (such as tree depth, splitting criteria, etc.), w opt is the optimized feature weight vector, representing the importance of each feature in the model; k represents the total number of decision trees in the random forest.
[0091] In order to further implement the present invention, the present invention also provides a system for a food safety risk early warning method based on optimized random forests, comprising:
[0092] Data processing module: used to obtain initial data, pre-process the initial data, obtain a processed first data set, divide the first data set into a training set and a test set according to an 8:2 ratio, and use the SMOTE method to oversample the training set so that the ratio of qualified and unqualified samples is 1:1;
[0093] Model optimization module: used to optimize the hyperparameter θ and dynamically adjust the feature weight w in the RF model through MLSSA, obtain the optimized hyperparameter θ and dynamically adjust the feature weight w, and use the MLSSA-optimized θ and feature weight w to build the MLSSA-RF model;
[0094] Training module: used to train the MLSSA-RF model based on the training set;
[0095] Testing module: used to test the trained MLSSA-RF model based on the test set data. The test uses precision, recall, F1 score, and AUC value indicators. When the test result does not meet the predetermined range, return to step S3. When the test result meets the predetermined range, the training is terminated to obtain the final MLSSA-RF model.
[0096] Detection prediction module: Based on the historical sampling data of clothianidin in vegetables, the final MLSSA-RF model is used to predict the future sampling results of clothianidin in vegetables, output the high-risk combinations that affect the vegetable sampling results, and issue risk warnings.
[0097] In order to further illustrate the present invention, a specific example is given below:
[0098] Based on the sampling data of clothianidin in vegetables from 2022 to 2023, the MLSSA-RF prediction model was established with seven characteristics, namely sampling area, sampling month, sampling link, sampling location, testing agency, vegetable subcategory, and vegetable variety, as input variables and whether the sampling results of clothianidin in vegetables were qualified as the output variable. Figure 1-Figure 6 The results of this study show that the MLSSA-RF model is sensitive and accurate for food safety risk early warning, and can timely predict the results of clothianidin sampling in vegetables. In particular, it can obtain the accurate probability of unqualified clothianidin sampling in actual vegetable samples in the future through the combination of different pre-set sampling factors.
[0099] The results show that the system is sensitive and accurate for risk warning, and is rich in functions, easy to use and highly interactive. In particular, it can obtain the precise probability of unqualified samples in future actual samples through the combination of different pre-set sampling factors, providing a reference for the formulation of supervision and sampling plans for pesticides in vegetables, and further improving the targeting, scientific nature of vegetable safety supervision and sampling, as well as the timeliness and accuracy of risk warning.
[0100] The food safety risk early warning system built based on the MLSSA optimization algorithm is technologically advanced and has practical application value. It innovatively combines optimization algorithms and modular system design. It has good application prospects in the field of food safety risk early warning. It can timely discover food safety risk hazards, help improve the effectiveness of food safety supervision, and protect residents' food safety.
[0101] In summary, the beneficial effects of the present invention include:
[0102] (1) Algorithm improvement. The adaptive adjustment strategy and Levy flight strategy are introduced to jointly improve the traditional sparrow search algorithm, realize the hierarchical sparrow search strategy of elite group, middle group and tail group, enhance the search radius and search ability, and reduce the defect of the traditional sparrow algorithm that is easy to fall into the local optimal solution.
[0103] (2) Improvement of prediction performance. MLSSA-RF significantly improves the ability of RF in feature selection and parameter optimization. It has strong high-dimensional data processing capabilities, can effectively mine key features, and reduce the interference of redundant features on the model. For example, the RF model and MLSSA-RF model established based on the historical sampling data of clothianidin in vegetables, the average precision and average recall of the original RF model are 0.68 and 0.54 respectively, while the average precision and average recall of the MLSSA-RF model are 0.84 and 0.83 respectively, and the prediction performance is significantly improved.
[0104] (3) The prediction and early warning system is rich in functions. The food safety risk early warning system has the characteristics of real-time response and strong scalability. The system interface is simple in design, and the combination of graphics and interactive functions helps users quickly master system operation skills, reduce learning and use costs, provide efficient information support for decision analysts, and provide reference for scientific supervision of food safety.
[0105] It should be understood by those skilled in the art that the discussion of any of the above embodiments is merely illustrative and is not intended to imply that the scope of the present invention (including the claims) is limited to these examples. Within the scope of the present invention, the technical features in the above embodiments or in different embodiments may be combined, the steps may be implemented in any order, and there are many other variations of the above aspects of the present invention, which are not provided in detail for the sake of simplicity.
[0106] The present invention is intended to cover all such substitutions, modifications and variations that fall within the broad scope of the appended claims. Therefore, any omissions, modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the scope of protection of the present invention.
Claims
1. A food safety risk early warning method based on optimized random forest, characterized in that: include: S1: Acquire initial data, preprocess the initial data to obtain a processed first data set, divide the first data set into a training set and a test set according to an 8:2 ratio, and use the SMOTE method to oversample the training set so that the ratio of qualified samples to unqualified samples is 1:1; S2: Optimize the hyperparameter θ and dynamically adjust the feature weight w in the RF model through MLSSA to obtain the optimized hyperparameter θ and dynamically adjust the feature weight w; S3: Use the MLSSA-optimized θ and feature weight w to build the MLSSA-RF model; S4: Train the MLSSA-RF model based on the training set; S5: Test the trained MLSSA-RF model based on the test set data. The test uses precision, recall, F1 score, and AUC value indicators. When the test result does not meet the predetermined range, return to step S3. When the test result meets the predetermined range, the training is terminated to obtain the final MLSSA-RF model. S6: Based on the historical sampling data of clothianidin in vegetables, the final MLSSA-RF model is used to predict the sampling results of clothianidin in vegetables in the future, output the high-risk combinations that affect the sampling results of vegetables, and issue risk warnings.
2. A food safety risk early warning method based on optimized random forest according to claim 1, characterized in that: The method of optimizing the hyperparameter θ and dynamically adjusting the feature weight w in the RF model by MLSSA to obtain the optimized hyperparameter θ and dynamically adjusting the feature weight w includes: Determine the optimization target, which is the hyperparameter θ and the dynamically adjusted feature weight w in the objective function as follows: min θ,w F(θ,w)=-score(RF(X,y,θ,w)) Where: min θ,w represents the minimization operation of the parameter θ and the weight w; F(θ,w) is the objective function; score(RF(X,y,θ,w)) is the scoring function; Based on the MLSSA algorithm, a population is randomly generated, each individual in the population represents a combination of a set of hyperparameters θ and a dynamically adjusted feature weight w. The objective function (1) is used as the fitness function, and the classification error is calculated through the test set to guide the population update. Use equation (4) to calculate the adaptive step size and update the position according to the hierarchical population; Where: c represents the step size; α represents the parameter for adjusting dynamic behavior; iter is the current iteration number; Gmax represents the total iteration number; The updating of the position according to the stratified population includes: dynamically updating the individual position according to the characteristics of the elite group, the middle group and the tail group, the elite group performs a global search to update the individual position; the middle group performs a neighborhood local search to update the individual position; the tail group protects the diversity of the population to update the individual position; Based on the above update method, the MLSSA algorithm adaptively adjusts the four hyperparameters θ of the RF model, including the number of decision trees, the maximum depth of the decision tree, the minimum sample ratio when splitting, and the minimum impurity reduction threshold, to obtain the four optimized hyperparameters θ; According to the following formula, the adjusted and optimized feature weight w is obtained by maximizing the weighted Gini index; Where: Gini w (D,w) is the Gini index after feature weight optimization, reflecting the impurity of the dataset D; K represents the number of categories; w i is the weight of the i-th feature; p i is the probability of the i-th feature; IG(D, A) represents the information gain of feature A; V represents the different levels or categories of feature A; |Dv| represents the number of samples with the value v in feature A; |D| represents the total number of samples; Gini(Dv) represents the Gini impurity calculated in the subset Dv where the value of feature A is v.
3. A food safety risk early warning method based on optimized random forest according to claim 2, characterized in that: The elite group performs global search and updates individual positions based on the following formula: in, represents the state of the i-th individual in the iteration of time t+1; represents the state of the i-th individual in the time t iteration; Gmax represents the maximum number of iterations allowed; represents the global optimal solution found in the tth generation; N(0,1) represents the standard normal distribution; c(t) represents the control parameter that affects the Levy distribution; Levy(λ) represents the random number generated by the Lévy distribution, whose parameter is λ.
4. A food safety risk early warning method based on optimized random forest according to claim 2, characterized in that: The intermediate group performs a local neighborhood search to update individual positions based on the following formula: Where A is a binary random number (0 or 1) generated based on the standard normal distribution, which is used to adjust the update amplitude, N(0,1) represents the standard normal distribution; i is the imaginary unit; represents the state of the i-th individual in the time t iteration; represents the global optimal solution found in the tth generation, and rand() is a random number in the range of 0 to 1.
5. The food safety risk early warning method based on optimized random forest according to claim 2, characterized in that: The tail population updates the diversity of individual positions based on the following conservation population formula: Where N(0,1) represents the standard normal distribution; i is the imaginary unit; α is a parameter used to adjust the dynamic behavior; c(t) is a constant. represents the state of the i-th individual in the time t iteration; represents the global optimal solution found in the tth generation, affecting the influence of Levy distribution; Levy(λ) represents the random number generated by Lévy distribution, whose parameter is λ.
6. The food safety risk early warning method based on optimized random forest according to claim 1, characterized in that: Step S5 also includes: evaluating the classification performance of the MLSSA-RF model, drawing a confusion matrix diagram, performing feature importance analysis, and obtaining the feature importance ranking of the MLSSA-RF model.
7. The food safety risk early warning method based on optimized random forest according to claim 1, characterized in that: The MLSSA-RF model is as follows: in: Where: mode{…} means voting on the prediction results of all decision trees and returning the category with the highest frequency; tree k (x;θ opt ,w opt ) is the prediction result of the kth decision tree for the input x, θ opt is the optimized parameter vector, which contains the hyperparameters of the decision tree, w opt is the optimized feature weight vector, representing the importance of each feature in the model; k represents the total number of decision trees in the random forest.
8. A system for executing the food safety risk early warning method based on optimized random forest as claimed in claim 1, characterized in that: include: Data processing module: used to obtain initial data, pre-process the initial data, obtain a processed first data set, divide the first data set into a training set and a test set according to an 8:2 ratio, and use the SMOTE method to oversample the training set so that the ratio of qualified and unqualified samples is 1:1; Model optimization module: used to optimize the hyperparameter θ and dynamically adjust the feature weight w in the RF model through MLSSA, obtain the optimized hyperparameter θ and dynamically adjust the feature weight w, and use the MLSSA-optimized θ and feature weight w to build the MLSSA-RF model; Training module: used to train the MLSSA-RF model based on the training set; Testing module: used to test the trained MLSSA-RF model based on the test set data. The test uses precision, recall, F1 score, and AUC value indicators. When the test result does not meet the predetermined range, return to step S3. When the test result meets the predetermined range, the training is terminated to obtain the final MLSSA-RF model. Detection prediction module: Based on the historical sampling data of clothianidin in vegetables, the final MLSSA-RF model is used to predict the future sampling results of clothianidin in vegetables, output the high-risk combinations that affect the vegetable sampling results, and issue risk warnings.