Farmland soil heavy metal prediction method and system
By using extreme random tree regression algorithm and ant colony algorithm to optimize hyperparameters in farmland soil heavy metal prediction, the problem of failure to fully consider human factors and high-dimensional data in the existing technology is solved, and more efficient and reliable soil heavy metal prediction is achieved.
Patent Information
- Application Number
- CN202411603858.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-12
- Publication Date
- 2025-03-07
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
When predicting heavy metal content in farmland soil, the prior art fails to fully consider human factors and high-dimensional data, resulting in insufficient stability and applicability of model prediction results. In addition, traditional hyperparameter optimization methods lack adaptive adjustment mechanisms, making it difficult to achieve efficient hyperparameter search and adjustment.
The extreme random tree regression algorithm is used to combine the ant colony algorithm for hyperparameter optimization. By initializing and training the extreme random tree regression model, the ant colony algorithm is used to optimize the model hyperparameters, and then the decision tree set is constructed and the prediction accuracy is evaluated, and the prediction value of heavy metals in the soil is finally obtained.
It improves the performance and generalization ability of the model, can better capture the nonlinear relationship between soil heavy metal content and input variables, enhances the model's adaptability to complex environmental conditions, and improves prediction accuracy and reliability.
Smart Images

Figure CN119577595A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of machine learning and relates to a method and system for predicting heavy metals in farmland soil. Background Art
[0002] As people's awareness of food security continues to increase, the safety of soil for growing food has also received attention; soil heavy metal pollution can destroy the ecological balance of the soil, inhibit plant growth, and endanger human health. Therefore, how to detect heavy metals in soil has become a hot topic in current research. In the prior art, machine learning algorithms such as support vector machines (SVM), random forests, neural networks, etc. are usually used to predict the heavy metal content of soil in unknown areas by learning the relationship between features and heavy metal content in historical data sets. Such methods can handle nonlinear and high-dimensional data, and can handle complex nonlinear relationships to improve the accuracy of prediction. At the same time, researchers usually use geostatistical methods to process point data to predict the distribution of soil heavy metal content in space. Such methods perform well when there are abundant spatial data points, but the accuracy will drop significantly when the point data is sparse and the data quality is poor.
[0003] However, existing technologies also have shortcomings in predicting the content of heavy metals in farmland soil. Specifically, existing methods usually focus on the chemical composition analysis of the soil itself, but rarely consider the potential impact of human factors such as agricultural management measures and socio-economic activities on the content of heavy metals in the soil. Changes in these external factors may cause changes in the basic data on which the prediction model relies, thereby affecting the stability and applicability of the prediction results. At the same time, current technologies lack effective mechanisms when integrating high-dimensional data (such as land use, crop planting, etc.), resulting in the model being unable to fully utilize all influencing factors. Moreover, traditional hyperparameter optimization methods rely on exhaustive methods or random selection, lack an adaptive adjustment mechanism, and are difficult to achieve efficient hyperparameter search and adjustment. Summary of the invention
[0004] The purpose of the present invention is to solve the problems in the prior art of the potential impact of human factors on the content of heavy metals in soil, the lack of effective mechanism when fusing high-dimensional data, the traditional hyperparameter optimization method relies on exhaustive method or random selection, lacks adaptive adjustment mechanism, and is difficult to achieve efficient hyperparameter search and adjustment, and to provide a method and system for predicting heavy metals in farmland soil.
[0005] In order to achieve the above object, the present invention adopts the following technical solutions:
[0006] A method for predicting heavy metals in farmland soil, comprising:
[0007] Collect soil data in the study area;
[0008] Preprocessing the collected soil data, and dividing the preprocessed data set into a training set and a test set;
[0009] Initialize the extreme random tree regression algorithm model, and optimize the hyperparameters of the extreme random tree regression algorithm model based on the ant colony algorithm;
[0010] Based on the extreme random tree regression algorithm model, a decision tree set is constructed; the decision trees in the decision tree set are trained based on the training set, and the performance of the decision trees is tested through the test set. The prediction accuracy is evaluated based on the determination coefficient index to obtain the optimized extreme random tree regression algorithm model;
[0011] Soil data were collected again, and the predicted values of heavy metals in the soil were obtained based on the optimized extreme random tree regression algorithm model.
[0012] A further improvement of the present invention is:
[0013] Furthermore, soil data include: different land use types, crop planting conditions, soil property data, and soil heavy metal data in the study area;
[0014] The different land use types are L = {l 1 ,l 2 ,…,l m}, where each l j Represents a land use type; define function D L Represents a land use dataset, which contains the spatial distribution information and area of each land use type;
[0015] The crop planting situation is C = {c 1 ,c 2 ,…,c k}, each c i Represents a crop type, which is the set of crop planting types in the study area; define function D C Represents a crop planting dataset, which contains characteristic information of crop planting area and planting time;
[0016] The soil characteristic data is S = {s 1 ,s 2 ,…,s q}, the parameters include land use type, crop planting type, pH, organic matter content, soil texture, total nitrogen, available phosphorus, available potassium and cation exchange capacity; define function D s Represents a soil property dataset, which contains soil sample analysis results and is used to describe the chemical and physical properties of the soil;
[0017] The soil heavy metal data is R = {r 1,r 2 ,…,r t}; r i Indicates the type of soil heavy metals; defines the function D r Represents a heavy metal historical dataset; this dataset contains historically monitored heavy metal content data and its corresponding time and location information.
[0018] Furthermore, the collected soil data are preprocessed, including data cleaning, normalization and One-Hot encoding.
[0019] Furthermore, One-Hot encoding is as follows: collect text data and build a unique vocabulary containing all different words; assign a fixed-length vector to each word, where the position of the word is 1 and the remaining positions are 0; traverse each text sample and convert it into the corresponding One-Hot encoding form; the encoding results of all samples form a two-dimensional matrix as the input of the machine learning model.
[0020] Furthermore, the extreme random tree regression algorithm model is initialized, specifically: the search space of the extreme random tree regression algorithm model hyperparameters is set, including the number of trees, the maximum depth, the minimum number of sample splits, and the maximum number of features considered for each split; the ant colony algorithm is used to find the above optimal hyperparameter combination, and the search space of the ant colony algorithm is the possible value range of the hyperparameters.
[0021] Furthermore, the hyperparameters of the extreme random tree regression algorithm model are optimized based on the ant colony algorithm, specifically:
[0022] Initialize M ants, and each ant randomly selects a set of hyperparameters as the initial solution; the hyperparameter search space consists of the value range of each hyperparameter; each ant trains the extreme random tree regression algorithm model in the randomly selected hyperparameter combination; use cross validation to evaluate the model performance on the training set, and calculate the mean square error MSE of the model as the fitness value:
[0023]
[0024] Among them, y i Is the real heavy metal content, is the heavy metal content predicted by the model; the training set is where x i ∈D is the feature vector, y i ∈R is the heavy metal target value;
[0025] The pheromone is updated according to the fitness value of each set of hyperparameter combinations; the pheromone update formula is:
[0026] τ ij (t+1)=(1-ρ)·τij (t)+Δτ ij
[0027] Among them, ρ is the pheromone volatility coefficient, which determines the pheromone decay rate and is set to 0.5, Δτ ij is the pheromone increment contributed by each ant in the current iteration; τ ij (t) represents the pheromone concentration on the edge or path (i, j) at the tth iteration; τ ij (t+1) represents the updated pheromone concentration at the t+1th iteration;
[0028] Each ant selects the next set of hyperparameters based on the updated pheromone concentration; by continuously exploring new hyperparameter combinations, it gradually converges to the optimal solution during the search process; the iterative process continues until the set number of iterations is reached or the fitness value converges to a certain threshold; and finally the optimal hyperparameter combination is obtained.
[0029] Furthermore, the decision trees in the decision tree set are trained based on the training set, and the performance of the decision trees is tested through the test set, specifically:
[0030] Construct m decision trees, each of which is divided on different random features and random thresholds;
[0031] For each decision tree, a feature subset F is randomly selected from all features. m ; For feature subset F m For each feature in, randomly select a partition threshold node t;
[0032] Split nodes based on randomly selected features and thresholds to divide the dataset into two subsets; repeat the above steps for each node until the preset stop condition is met;
[0033] By letting each decision tree make predictions separately, and then averaging the predictions of all the trees:
[0034]
[0035] Among them, h m (x) is the prediction result of the mth tree, This is the final heavy metal prediction result.
[0036] Furthermore, the prediction accuracy is evaluated based on the coefficient of determination index, and the optimal extreme random tree regression algorithm model is obtained, which is:
[0037]
[0038] in, is the mean of the heavy metal target values; iis the actual observed value of heavy metals, that is, the actual heavy metal content value in the data set; Model predicted value refers to the predicted value of heavy metal content obtained using the model.
[0039] A farmland soil heavy metal prediction system, comprising:
[0040] A collection module, wherein the collection module collects soil data in a study area;
[0041] A preprocessing module, wherein the preprocessing module preprocesses the collected soil data and divides the preprocessed data set into a training set and a test set;
[0042] An optimization module, wherein the optimization module initializes the extreme random tree regression algorithm model and optimizes the hyperparameters of the extreme random tree regression algorithm model based on an ant colony algorithm;
[0043] A training module, wherein the training module constructs a decision tree set based on an extreme random tree regression algorithm model; trains the decision trees in the decision tree set based on the training set, tests the performance of the decision trees through a test set, evaluates the prediction accuracy based on a determination coefficient index, and obtains an optimized extreme random tree regression algorithm model;
[0044] The acquisition module collects soil data again and obtains the predicted value of heavy metals in the soil based on the optimized extreme random tree regression algorithm model.
[0045] Compared with the prior art, the present invention has the following beneficial effects:
[0046] The present invention uses land use type and crop planting type as input data, and optimizes the hyperparameters in the extreme random tree regression model through the ant colony algorithm, especially the depth and splitting criteria of each tree, and improves the search efficiency through iterative updates of pheromones, solves the local optimal problem in traditional methods, and improves the performance and generalization ability of the model. The present invention effectively utilizes environmental and crop factors related to heavy metal pollution, enhances the adaptability of the model to complex environmental conditions, and the ant colony optimization extreme tree algorithm can better capture the nonlinear relationship between soil heavy metal content and input variables, and improves the prediction accuracy and reliability. BRIEF DESCRIPTION OF THE DRAWINGS
[0047] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings required for use in the embodiments are briefly introduced below. It should be understood that the following drawings only show certain embodiments of the present invention and therefore should not be regarded as limiting the scope. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without creative work.
[0048] Figure 1 It is a schematic diagram of the process of the method for predicting heavy metals in farmland soil of the present invention;
[0049] Figure 2 It is a structural schematic diagram of the farmland soil heavy metal prediction system of the present invention;
[0050] Figure 3 Schematic diagram of prediction results using land use type and planted crop data as input data;
[0051] Figure 4 This is a diagram showing the prediction results without taking land use type and crop data as input data;
[0052] Figure 5 This is a schematic diagram of the prediction of heavy metal content in farmland soil using the extreme tree algorithm;
[0053] Figure 6 Schematic diagram of prediction of heavy metal content in farmland soil by gradient boosting decision tree;
[0054] Figure 7 Schematic diagram of prediction of heavy metal content in farmland soil using K-nearest neighbor algorithm;
[0055] Figure 8 Schematic diagram of the prediction of heavy metal content in farmland soil using the random forest algorithm. DETAILED DESCRIPTION
[0056] In order to make the purpose, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Generally, the components of the embodiments of the present invention described and shown in the drawings here can be arranged and designed in various different configurations.
[0057] Therefore, the following detailed description of the embodiments of the present invention provided in the accompanying drawings is not intended to limit the scope of the invention claimed for protection, but merely represents selected embodiments of the present invention. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0058] It should be noted that similar reference numerals and letters denote similar items in the following drawings, and therefore, once an item is defined in one drawing, further definition and explanation thereof is not required in subsequent drawings.
[0059] In the description of the embodiments of the present invention, it should be noted that if the terms "upper", "lower", "horizontal", "inner", etc. indicate an orientation or positional relationship based on the orientation or positional relationship shown in the drawings, or the orientation or positional relationship in which the product of the invention is usually placed when in use, it is only for the convenience of describing the present invention and simplifying the description, and does not indicate or imply that the device or element referred to must have a specific orientation, be constructed and operated in a specific orientation, and therefore cannot be understood as a limitation on the present invention. In addition, the terms "first", "second", etc. are only used to distinguish the description, and cannot be understood as indicating or implying relative importance.
[0060] In addition, if the term "horizontal" appears, it does not mean that the component must be absolutely horizontal, but can be slightly tilted. For example, "horizontal" only means that its direction is more horizontal than "vertical", which does not mean that the structure must be completely horizontal, but can be slightly tilted.
[0061] In the description of the embodiments of the present invention, it is also necessary to explain that, unless otherwise clearly specified and limited, the terms "set", "install", "connect", and "connect" should be understood in a broad sense, for example, it can be a fixed connection, a detachable connection, or an integral connection; it can be a mechanical connection or an electrical connection; it can be a direct connection, or it can be indirectly connected through an intermediate medium, or it can be the internal connection of two components. For ordinary technicians in this field, the specific meanings of the above terms in the present invention can be understood according to specific circumstances.
[0062] The present invention is further described in detail below in conjunction with the accompanying drawings:
[0063] See also Figure 1 The present invention discloses a method for predicting heavy metals in farmland soil, comprising:
[0064] S101, collect soil data in the study area;
[0065] Soil data include: different land use types, crop planting conditions, soil properties data and soil heavy metal data in the study area;
[0066] The different land use types are L = {l 1 ,l 2 ,…,l m}, where each l j Represents a land use type; define function D L Represents a land use dataset, which contains the spatial distribution information and area of each land use type;
[0067] The crop planting situation is C = {c 1 ,c 2 ,…,c k}, each c i Represents a crop type, which is the set of crop planting types in the study area; define function D C Represents a crop planting dataset, which contains characteristic information of crop planting area and planting time;
[0068] The soil characteristic data is S = {s 1 ,s 2 ,…,s q}, the parameters include land use type, crop planting type, pH, organic matter content, soil texture, total nitrogen, available phosphorus, available potassium and cation exchange capacity; define function D s Represents a soil property dataset, which contains soil sample analysis results and is used to describe the chemical and physical properties of the soil;
[0069] The soil heavy metal data is R = {r 1 ,r 2 ,…,r t}; r i Indicates the type of soil heavy metals; defines the function D r Represents a heavy metal historical dataset; this dataset contains historically monitored heavy metal content data and its corresponding time and location information.
[0070] S102, preprocessing the collected soil data, and dividing the preprocessed data set to obtain a training set and a test set;
[0071] The collected soil data was preprocessed, including data cleaning, normalization, and One-Hot encoding. One-Hot encoding is as follows: collect text data and build a unique vocabulary containing all different words; assign a fixed-length vector to each word, where the word position is 1 and the rest are 0; traverse each text sample and convert it into the corresponding One-Hot encoding form; the encoding results of all samples form a two-dimensional matrix as the input of the machine learning model.
[0072] S103, initializing the extreme random tree regression algorithm model, and optimizing the hyperparameters of the extreme random tree regression algorithm model based on the ant colony algorithm;
[0073] Set the search space of the extreme random tree regression algorithm model hyperparameters, including the number of trees, maximum depth, minimum number of sample splits, and the maximum number of features considered for each split; use the ant colony algorithm to find the best combination of the above hyperparameters, and the search space of the ant colony algorithm is the possible value range of the hyperparameters.
[0074] The hyperparameters of the extreme random tree regression algorithm model are optimized based on the ant colony algorithm, specifically:
[0075] Initialize M ants, and each ant randomly selects a set of hyperparameters as the initial solution; the hyperparameter search space consists of the value range of each hyperparameter; each ant trains the extreme random tree regression algorithm model in the randomly selected hyperparameter combination; use cross validation to evaluate the model performance on the training set, and calculate the mean square error MSE of the model as the fitness value:
[0076]
[0077] Among them, y i Is the real heavy metal content, is the heavy metal content predicted by the model; the training set is where x i ∈D is the feature vector, y i ∈R is the heavy metal target value;
[0078] The pheromone is updated according to the fitness value of each set of hyperparameter combinations; the pheromone update formula is:
[0079] τ ij (t+1)=(1-ρ)·τ ij (t)+Δτ ij
[0080] Among them, ρ is the pheromone volatility coefficient, which determines the pheromone decay rate and is set to 0.5, Δτ ij is the pheromone increment contributed by each ant in the current iteration; τ ij (t) represents the pheromone concentration on the edge or path (i, j) at the tth iteration; τ ij (t+1) represents the updated pheromone concentration at the t+1th iteration;
[0081] Each ant selects the next set of hyperparameters based on the updated pheromone concentration; by continuously exploring new hyperparameter combinations, it gradually converges to the optimal solution during the search process; the iterative process continues until the set number of iterations is reached or the fitness value converges to a certain threshold; and finally the optimal hyperparameter combination is obtained.
[0082] S104, constructing a decision tree set based on the extreme random tree regression algorithm model; training the decision trees in the decision tree set based on the training set, testing the performance of the decision trees through the test set, evaluating the prediction accuracy based on the determination coefficient index, and obtaining the optimized extreme random tree regression algorithm model;
[0083] Construct m decision trees, each of which is divided on different random features and random thresholds;
[0084] For each decision tree, a feature subset F is randomly selected from all features. m; For feature subset F m For each feature in, randomly select a partition threshold node t;
[0085] Split nodes based on randomly selected features and thresholds to divide the dataset into two subsets; repeat the above steps for each node until the preset stop condition is met;
[0086] By letting each decision tree make predictions separately, and then averaging the predictions of all the trees:
[0087]
[0088] Among them, h m (x) is the prediction result of the mth tree, This is the final heavy metal prediction result.
[0089] The prediction accuracy is evaluated based on the coefficient of determination index, and the optimal extreme random tree regression algorithm model is obtained, which is:
[0090]
[0091] in, is the mean of the heavy metal target values; i is the actual observed value of heavy metals, that is, the actual heavy metal content value in the data set; Model predicted value refers to the predicted value of heavy metal content obtained using the model.
[0092] S105, collecting soil data again, and obtaining predicted values of heavy metals in the soil based on the optimized extreme random tree regression algorithm model.
[0093] See also Figure 2 The present invention discloses a farmland soil heavy metal prediction system, comprising:
[0094] A collection module, wherein the collection module collects soil data in a study area;
[0095] A preprocessing module, wherein the preprocessing module preprocesses the collected soil data and divides the preprocessed data set into a training set and a test set;
[0096] An optimization module, wherein the optimization module initializes the extreme random tree regression algorithm model and optimizes the hyperparameters of the extreme random tree regression algorithm model based on an ant colony algorithm;
[0097] A training module, wherein the training module constructs a decision tree set based on an extreme random tree regression algorithm model; trains the decision trees in the decision tree set based on the training set, tests the performance of the decision trees through a test set, evaluates the prediction accuracy based on a determination coefficient index, and obtains an optimized extreme random tree regression algorithm model;
[0098] The acquisition module collects soil data again and obtains the predicted value of heavy metals in the soil based on the optimized extreme random tree regression algorithm model.
[0099] Example:
[0100] The invention discloses a method for predicting heavy metals in farmland soil, comprising: 1. data collection.
[0101] Land use type data. Collect information on different land use types in the study area, such as farmland, forest land, urban land, wasteland, etc., and obtain relevant data through remote sensing images, land use surveys, etc. Let L = {l 1 ,l 2 ,…,l m}, l j ∈ text data, where each l j Represents a type of land use, such as farmland, forest land, urban land, etc.; it is the set of land use types in the study area. Define function D L Represents a land use dataset, which contains the spatial distribution information and area of each land use type.
[0102] Crop planting type data. Field surveys are used to obtain crop planting conditions, including major crop types (such as rice, rapeseed, and fruit trees). Let C = {c 1 ,c 2 ,…,c k}, c j ∈ text data, each c i Represents a crop type (such as rice, vegetables, etc.), which is the set of crop planting types in the study area. Define function D C Represents a crop planting dataset, which contains characteristic information such as crop planting area and planting time.
[0103] Soil characteristic data. The soil characteristic data of the study area is obtained through soil sample analysis. Let S = {s 1 ,s 2 ,…,s q} is a set of soil characteristics, including land use type, crop planting type, pH, organic matter content (Organic matter), soil texture, total nitrogen (Total nitrogen, TN), available phosphorus (available phosphorus, AP), rapidly available potassium (rapidly available potassium, AK), cation exchange capacity, etc. Define function D s Represents a soil property dataset, which contains soil sample analysis results and is used to describe the chemical and physical properties of the soil. S ={s 1 ,s 2 ,…,s r}, including numerical parameters {pH, OM, TN, AP, AK, CEC} and text parameters {soil texture}.
[0104] Soil heavy metal data. Let R = {r 1 ,r 2 ,…,r t} is a set of heavy metal types (such as lead, cadmium, etc.). Define function D r Represents a historical heavy metal dataset. This dataset contains historically monitored heavy metal content data and its corresponding time and location information.
[0105] In summary, all data from different sources are integrated into a unified data framework, which is the data acquisition process of the method of the present invention:
[0106] D=D L ∪D C ∪D S ∪D R
[0107] Among them, D is all relevant data sets that are finally integrated, laying the foundation for subsequent data preprocessing and model building.
[0108] 2. Data preprocessing.
[0109] (1) Data cleaning: Remove missing values and outliers. For missing values, you can use interpolation to fill in or delete missing rows to ensure data integrity.
[0110] (2) Normalization: Normalize numerical features, such as Min-Max normalization or Z-score normalization, to eliminate the dimensionality effects between different features and ensure the effectiveness of model training.
[0111] (3) One-Hot encoding. To convert text data into numerical format, first collect the text data and build a unique vocabulary containing all different words. Then, assign a fixed-length vector to each word, where the position of the word is 1 and the rest are 0. Next, traverse each text sample and convert it into the corresponding One-Hot encoding form. The encoding results of all samples form a two-dimensional matrix as the input of the machine learning model.
[0112] 3. Build a predictive model.
[0113] According to the data characteristics and research objectives, the Extremely Randomized Trees Regressor (ERTR) algorithm was selected as the heavy metal prediction model. This algorithm is an integrated learning algorithm that performs regression tasks by constructing multiple completely random decision trees. It is a variant of the random forest algorithm. Unlike random forests, ERTR not only selects random features when selecting the partition of each node, but also the partition threshold is random.
[0114] Given a training dataset where x i ∈D is the feature vector, y i ∈R is the heavy metal target value.
[0115] Algorithm steps:
[0116] (1) Initialize model creation. Initialize the ExtraTreesRegressor instance and set the search space of hyperparameters, including the number of trees (n_estimators), maximum depth (max_depth), minimum number of sample splits (min_samples_split), and maximum number of features considered for each split (max_features). The ant colony algorithm is used to find the best combination of the above hyperparameters. The search space of the ant colony algorithm is the possible value range of these hyperparameters.
[0117] Where: n estimators ∈[50,200], max_depth∈[5,50], min_samples_split∈[2,20], max_features∈[0.1,1.0].
[0118] (2) Ant colony algorithm is used to optimize model hyperparameters.
[0119] Initialize M ants, and each ant randomly selects a set of hyperparameters as the initial solution. The hyperparameter search space consists of the value range of each hyperparameter. Each ant trains the ExtraTreesRegressor model under the randomly selected hyperparameter combination. Use cross-validation to evaluate the model performance on the training set, and calculate the mean square error (MSE) of the model as the fitness value:
[0120]
[0121] Among them, y i Is the real heavy metal content, is the heavy metal content predicted by the model.
[0122] The pheromone is updated according to the fitness value of each hyperparameter combination. Hyperparameter combinations with low fitness values (i.e., small MSE) will retain more pheromones, guiding ants in future iterations to preferentially select hyperparameters near this combination. The pheromone update formula is:
[0123] τ ij (t+1)=(1-ρ)·τ ij (t)+Δτ ij
[0124] Among them, ρ is the pheromone volatility coefficient, which determines the pheromone decay rate and is set to 0.5, Δτ ij is the pheromone increment contributed by each ant in the current iteration. τ ij (t) represents the pheromone concentration on the edge or path (i, j) at the tth iteration. τ ij (t+1) represents the updated pheromone concentration at the t+1th iteration.
[0125] Each ant selects the next set of hyperparameters based on the updated pheromone concentration. By continuously exploring new hyperparameter combinations, it gradually converges to the optimal solution during the search process. The iterative process continues until the set number of iterations is reached or the fitness value converges to a certain threshold. Finally, the optimal hyperparameter combination is obtained.
[0126] (3) Construct a set of decision trees. ERTR constructs m decision trees, each of which is divided based on different random features and random thresholds.
[0127] (4) Train each decision tree. For each tree m:
[0128] a. Select random features: Randomly select a feature subset F from all features m
[0129] b. Randomly select threshold: For feature subset F m For each feature in , a split threshold t is randomly selected.
[0130] c. Split nodes: Use randomly selected features and thresholds to split nodes and divide the dataset into two subsets. For each node, repeat the above steps until the stopping condition is met (such as reaching the maximum depth or the number of samples contained in the node is less than a certain threshold).
[0131] (6) Tree collection. The collection of all trees constitutes the final model. This is done by letting each tree make predictions separately and then taking the average of the predictions of all trees:
[0132]
[0133] Among them, h m (x) is the prediction result of the mth tree, This is the final heavy metal prediction result.
[0134] (7) Model verification and evaluation. The performance of the model was verified on the test set using mean square error (MSE), coefficient of determination (R 2 ) and other indicators to evaluate the prediction accuracy. The actual performance of the calculation model is:
[0135]
[0136] in, is the mean of the heavy metal target values. i is the actual observed value of heavy metals, that is, the actual heavy metal content value in the data set. Model predicted value refers to the predicted value of heavy metal content obtained using the model.
[0137] 4. Store the prediction model.
[0138] After training is complete, use joblib to save the model to a local file (extra_trees_model.pkl). When prediction is required, the saved model can be called.
[0139] When prediction is needed, first load the saved model. You can use the joblib.load method to read the model file (extra_trees_model.pkl). After loading, input the test data set (such as new data set features) into the model, and use the predict method to obtain the predicted value of heavy metal content. Finally, output the prediction results for analysis and decision-making.
[0140] The optimized extreme random tree regression algorithm model is applied to unknown land use types and crop planting types to predict their possible impact on the heavy metal content of farmland soil. Based on the prediction results output by the model, combined with the specific conditions of land use and crop planting, the reliability of the prediction is explained, and a basis is provided for agricultural management and pollution prevention.
[0141] The land use type and crop data are used as input data to improve the accuracy of the farmland soil heavy metal content prediction model of the present invention, see Figure 3 and Figure 4 . See Table 1 for specific data comparison.
[0142] Table 1 Comparison of prediction accuracy of different data sets
[0143]
[0144] Comparison of predictions of heavy metal content in farmland soil using different algorithms, see Figure 5 , Figure 6 , Figure 7 and Figure 8 The extreme random tree regression algorithm improved by the ant colony algorithm (hereinafter referred to as the ant colony optimized extreme tree algorithm) has a better determination coefficient than the traditional random forest algorithm. The specific results are shown in Table 2.
[0145] Table 2 Comparison of the accuracy of different prediction methods
[0146]
[0147] The terminal device provided in an embodiment of the present invention. The terminal device of this embodiment includes: a processor, a memory, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, the steps in the above-mentioned method embodiments are implemented. Alternatively, when the processor executes the computer program, the functions of the modules / units in the above-mentioned device embodiments are implemented.
[0148] The computer program may be divided into one or more modules / units, and the one or more modules / units are stored in the memory and executed by the processor to accomplish the present invention.
[0149] The terminal device may be a computing device such as a desktop computer, a notebook, a PDA, a cloud server, etc. The terminal device may include, but is not limited to, a processor and a memory.
[0150] The processor can be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc.
[0151] The memory may be used to store the computer programs and / or modules, and the processor implements various functions of the terminal device by running or executing the computer programs and / or modules stored in the memory and calling the data stored in the memory.
[0152] If the module / unit integrated in the terminal device is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the present invention implements all or part of the processes in the above-mentioned embodiment method, and can also be completed by instructing the relevant hardware through a computer program. The computer program can be stored in a computer-readable storage medium, and the computer program can implement the steps of the above-mentioned various method embodiments when executed by the processor. Among them, the computer program includes computer program code, and the computer program code can be in source code form, object code form, executable file or some intermediate form. The computer-readable medium may include: any entity or device capable of carrying the computer program code, recording medium, U disk, mobile hard disk, disk, optical disk, computer memory, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), electric carrier signal, telecommunication signal and software distribution medium. It should be noted that the content contained in the computer-readable medium can be appropriately increased or decreased according to the requirements of legislation and patent practice in the jurisdiction. For example, in some jurisdictions, according to legislation and patent practice, computer-readable media do not include electric carrier signals and telecommunication signals.
[0153] The above are only preferred embodiments of the present invention and are not intended to limit the present invention. For those skilled in the art, the present invention may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.
Claims
1. A method for predicting heavy metals in farmland soil, characterized in that: include: Collect soil data in the study area; Preprocessing the collected soil data, and dividing the preprocessed data set into a training set and a test set; Initialize the extreme random tree regression algorithm model, and optimize the hyperparameters of the extreme random tree regression algorithm model based on the ant colony algorithm; Construct a decision tree set based on the extreme random tree regression algorithm model; The decision trees in the decision tree set are trained based on the training set, and the performance of the decision trees is tested through the test set. The prediction accuracy is evaluated based on the determination coefficient index to obtain the optimized extreme random tree regression algorithm model; Soil data were collected again, and the predicted values of heavy metals in the soil were obtained based on the optimized extreme random tree regression algorithm model.
2. The method for predicting heavy metals in farmland soil according to claim 1, characterized in that: The soil data include: different land use types, crop planting conditions, soil property data and soil heavy metal data in the study area; The different land use types are L = {l1, l2, ..., l m }, where each l j Represents a land use type; define function D L Represents a land use dataset, which contains the spatial distribution information and area of each land use type; The crop planting situation is C = {c1, c2, ..., c k }, each c i Represents a crop type, which is the set of crop planting types in the study area; define function D C Represents a crop planting dataset, which contains characteristic information of crop planting area and planting time; The soil characteristic data is S = {s1, s2, ..., s q }, the parameters include land use type, crop planting type, pH, organic matter content, soil texture, total nitrogen, available phosphorus, available potassium and cation exchange capacity; define function D s Represents a soil property dataset, which contains soil sample analysis results and is used to describe the chemical and physical properties of the soil; The soil heavy metal data is R = {r1, r2, ..., r t }; r i Indicates the type of soil heavy metals; defines the function D r Represents a heavy metal historical dataset; this dataset contains historically monitored heavy metal content data and its corresponding time and location information.
3. The method for predicting heavy metals in farmland soil according to claim 2, characterized in that: The preprocessing of the collected soil data includes: data cleaning, normalization processing and One-Hot encoding.
4. The method for predicting heavy metals in farmland soil according to claim 3, characterized in that: The One-Hot encoding is as follows: collect text data and build a unique vocabulary containing all different words; assign a fixed-length vector to each word, where the position of the word is 1 and the remaining positions are 0; traverse each text sample and convert it into the corresponding One-Hot encoding form; the encoding results of all samples form a two-dimensional matrix as the input of the machine learning model.
5. The method for predicting heavy metals in farmland soil according to claim 4, characterized in that: The method of initializing the extreme random tree regression algorithm model specifically includes setting a search space for hyperparameters of the extreme random tree regression algorithm model, including the number of trees, the maximum depth, the minimum number of sample splits, and the maximum number of features considered for each split; and using an ant colony algorithm to find the best hyperparameter combination, where the search space of the ant colony algorithm is a possible range of values for the hyperparameters.
6. The method for predicting heavy metals in farmland soil according to claim 5, characterized in that: The method of optimizing the hyperparameters of the extreme random tree regression algorithm model based on the ant colony algorithm is as follows: Initialize M ants, and each ant randomly selects a set of hyperparameters as the initial solution; the hyperparameter search space consists of the value range of each hyperparameter; each ant trains the extreme random tree regression algorithm model in the randomly selected hyperparameter combination; use cross validation to evaluate the model performance on the training set, and calculate the mean square error MSE of the model as the fitness value: Among them, y i Is the real heavy metal content, is the heavy metal content predicted by the model; the training set is where x i ∈D is the feature vector, y i ∈R is the heavy metal target value; The pheromone is updated according to the fitness value of each set of hyperparameter combinations; the pheromone update formula is: t ij (t+1)=(1-ρ)·τ ij (t)+Δτ ij Among them, ρ is the pheromone volatility coefficient, which determines the decay rate of pheromone and is set to 0.5, Δτ ij is the pheromone increment contributed by each ant in the current iteration; τ ij (t) represents the pheromone concentration on the edge or path (i, j) at the tth iteration; τ ij (t+1) represents the updated pheromone concentration at the t+1th iteration; Each ant selects the next set of hyperparameters based on the updated pheromone concentration; by continuously exploring new hyperparameter combinations, it gradually converges to the optimal solution during the search process; the iterative process continues until the set number of iterations is reached or the fitness value converges to a certain threshold; and finally the optimal hyperparameter combination is obtained.
7. The method for predicting heavy metals in farmland soil according to claim 6, characterized in that: The decision tree in the decision tree set is trained based on the training set, and the performance of the decision tree is tested through the test set, specifically: Construct m decision trees, each of which is divided on different random features and random thresholds; For each decision tree, a feature subset F is randomly selected from all features. m ; For feature subset F m For each feature in, randomly select a partition threshold node t; Split nodes based on randomly selected features and thresholds to divide the dataset into two subsets; repeat the above steps for each node until the preset stop condition is met; By letting each decision tree predict separately, and then averaging the predictions of all the trees: Among them, h m (x) is the prediction result of the mth tree, This is the final heavy metal prediction result.
8. The method for predicting heavy metals in farmland soil according to claim 7, characterized in that: The prediction accuracy is evaluated based on the determination coefficient index to obtain the optimized extreme random tree regression algorithm model, specifically: in, is the mean of the heavy metal target values; i is the actual observed value of heavy metals, that is, the actual heavy metal content value in the data set; Model predicted value refers to the predicted value of heavy metal content obtained using the model.
9. A farmland soil heavy metal prediction system, characterized in that: include: A collection module, wherein the collection module collects soil data in a study area; A preprocessing module, wherein the preprocessing module preprocesses the collected soil data and divides the preprocessed data set into a training set and a test set; An optimization module, wherein the optimization module initializes the extreme random tree regression algorithm model and optimizes the hyperparameters of the extreme random tree regression algorithm model based on an ant colony algorithm; A training module, wherein the training module constructs a decision tree set based on an extreme random tree regression algorithm model; The decision trees in the decision tree set are trained based on the training set, and the performance of the decision trees is tested through the test set. The prediction accuracy is evaluated based on the determination coefficient index to obtain the optimized extreme random tree regression algorithm model; The acquisition module collects soil data again and obtains the predicted value of heavy metals in the soil based on the optimized extreme random tree regression algorithm model.
Citation Information
Patent Citations
Transformer fault detection method based on ant colony algorithm optimization random forest
CN109142946A
Soil heavy metal pollution risk area division method and device, electronic equipment and medium
CN114511239A
Soil heavy metal prediction method based on BOSS-SAPSO optimized extreme learning machine
CN115130377A
Soil heavy metal content prediction method based on machine learning and application thereof
CN116187543A
Real-time feedback training system and method based on swimming phase monitoring
CN116785673A
Cited By
Hybrid model seedling growth prediction method based on ant colony algorithm optimization
CN121189373A