Water supply network trihalomethane prediction method based on fusion model

By constructing the CNN-BiLSTM-MultiHead Attention model and SHAP interpreter, the problem of high monitoring cost of trihalomethane concentration in the water supply pipeline network is solved, low-cost and fast trihalomethane concentration prediction and characteristic importance analysis are achieved, and water safety is improved.

CN120375981APending Publication Date: 2025-07-25FUZHOU UNIV
View PDF 0 Cites 3 Cited by

Patent Information

Application Number
CN202510508487.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-04-22
Publication Date
2025-07-25

AI Technical Summary

Technical Problem

The prior art cannot efficiently and at low cost to monitor and control the concentration of trihalomethane in the water supply pipeline network, resulting in water safety risks.

Method used

The trihalomethane prediction method of water supply pipeline network based on the fusion model was used, and the water quality data was pretreated and predicted using the CNN-BiLSTM-MultiHead Attention model, and the characteristic importance analysis was performed in combination with the SHAP interpreter to achieve timely and accurate prediction of trihalomethane concentration.

Benefits of technology

It realizes low-cost and fast trihalomethane concentration prediction, improves the water safety of the water supply network, and visualizes the importance of each input feature.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120375981A_ABST
    Figure CN120375981A_ABST
Patent Text Reader

Abstract

The invention belongs to the technical field of water supply network water quality monitoring, and provides a water supply network trihalomethane prediction method based on a fusion model. Comprising the steps of original water quality data set acquisition, data preprocessing, CNN-BiLSTM-MultiHead Attention fusion prediction model construction, hyper-parameter optimization space and model evaluation function definition, model prediction and output evaluation, model parameter iteration and verification evaluation, model verification, sample importance display, to-be-detected water quality data set collection and trihalomethane prediction. According to the method, the convolutional neural network, the bidirectional long-short-term memory network and the multi-head attention mechanism are fused, so that the change characteristics of a plurality of conventional water quality indexes in the water supply network are efficiently learned and extracted, and better prediction precision can still be kept under the condition of less training data; and the prediction performance and generalization ability of the model in the aspect of predicting a plurality of target variables by using a plurality of input variables are remarkably improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of water quality monitoring in water supply networks, and particularly to a method for predicting trihalomethanes in water supply networks based on a fusion model. Background Art

[0002] Natural organic matter is abbreviated as NOM, disinfection by-products are abbreviated as DBPs, trihalomethanes are abbreviated as THMs, trichloromethane is abbreviated as TCM, bromodichloromethane is abbreviated as BDCM, dibromochloromethane is abbreviated as DBCM, tribromomethane is abbreviated as TBM, and headspace packed column gas chromatography is abbreviated as HS-GC. With people's yearning for a better and happier life, the water quality safety of water supply networks has received increasing attention and concern from the general public. Due to water eutrophication, NOM such as humic acid, fulvic acid, and algae generally exist in water bodies. After purification and disinfection treatment in water plants, especially when reacting with hypochlorite in chlorinated disinfectants, DBPs that pose a threat to human health will be produced. According to relevant research, the largest proportion of DBPs in water supply networks is THMs, which mainly include four types: TCM, BDCM, DBCM, and TBM. Due to its toxicity and carcinogenic risk, the hygienic standard for drinking water GB 5749-2022 has taken it as one of the important detection indicators, and clearly stipulates that the limits of four THMs, namely TCM, DCBM, DBCM, and TBN, in drinking water are 0.06, 0.06, 0.1, and 0.1 mg / L respectively, and the total amount of the four THMs should be less than 1 mg / L, and the sum of the ratios of the measured values of each THM to the limit value of each THM is less than 1. The generation concentration of THMs in water supply networks is usually detected by HS-GC. HS-GC uses gas injection to specifically collect volatile components in samples and has advantages such as high sensitivity and fast analysis speed.

[0003] However, the above measurement steps are relatively cumbersome, and the costs of the required instruments and reagents are very high. It takes at least 20 minutes to measure each sample. If there are more water samples to be measured, it will consume a large amount of manpower, material resources, and time. Therefore, it is impossible to timely monitor and control the concentration of THMs in the entire water supply network, which will pose a potential risk to the water use safety at the end of some pipe networks. Summary of the Invention

[0004] In order to overcome the deficiencies of the prior art, the purpose of the present invention is to provide a method for predicting trihalomethanes in water supply networks based on a fusion model, so as to realize timely and accurate prediction of the generation concentration of THMs in water supply networks at a lower cost and visual analysis of the importance of each input feature.

[0005] To achieve the above purpose, the present invention provides the following solutions:

[0006] A method for predicting trihalomethanes in water supply networks based on a fusion time series model, comprising:

[0007] Collect the preset water quality index data in the target monitoring water supply pipeline to obtain the original water quality data set;

[0008] Use the quartile method of the box plot, the K-nearest neighbor algorithm, and the linear normalization method to remove outliers, fill in missing values, and scale the data for the original water quality data set to obtain preprocessed data;

[0009] Construct a CNN-BiLSTM-MultiHead Attention fusion prediction model; the CNN-BiLSTM-MultiHead Attention fusion prediction model includes an input layer, a convolutional and pooling layer, a MultiHeadAttention layer, a ScaledDotProductAttention layer, a bidirectional LSTM layer, and a fully connected layer; the input layer is used to traverse the preprocessed data using a fixed-size window sliding technique and determine the target value corresponding to the last time step of the window as the sample label; the MultiHeadAttention layer is used to perform linear projection, multi-head segmentation and merging, and linear integration on the input query, key, and value; the ScaledDotProductAttention layer is used to dynamically allocate weights to the MultiHeadAttention layer according to time segments; the fully connected layer is used to perform non-linear transformation and output mapping;

[0010] Define the hyperparameter optimization space and the model evaluation function;

[0011] Input the preprocessed data into the CNN-BiLSTM-MultiHead Attention fusion prediction model for prediction to obtain the model prediction result, and use the model evaluation function to calculate the prediction result to obtain the pre-evaluation result;

[0012] Iteratively optimize the CNN-BiLSTM-MultiHead Attention fusion prediction model according to the pre-evaluation result and the hyperparameter optimization space using the Bayesian optimization algorithm, and use the 5-fold cross-validation evaluation method to determine the hyperparameter combination of the CNN-BiLSTM-MultiHead Attention fusion prediction model to obtain a pre-trained model;

[0013] Use the preprocessed data to retrain the pre-trained model and calculate each evaluation index to obtain the verification result of the pre-trained model. If the verification result meets the preset index threshold, determine the pre-trained model as the trihalomethane prediction model;

[0014] Use the SHAP interpreter to calculate the SHAP values of each feature of each sample in the preprocessed data during the training phase, and draw and output a honeycomb plot and a feature importance circular plot according to the SHAP values;

[0015] Collect the preset water quality index data in the target detection water supply pipeline to obtain a water quality dataset to be detected;

[0016] Input the water quality dataset to be detected into the trichloromethane prediction model to obtain a trichloromethane prediction result.

[0017] Preferably, the CNN-BiLSTM-MultiHead Attention fusion prediction model also embeds a Conv1D convolutional layer, a MaxPooling1D pooling layer, a Flatten layer, a Reshape layer, and an output layer;

[0018] The input layer, the first Conv1D convolutional layer, the first MaxPooling1D pooling layer, the second Conv1D convolutional layer, the second MaxPooling1D pooling layer, the Flatten layer, the Reshape layer, the MultiHeadAttention layer, the ScaledDotProductAttention layer, the output layer, the first fully connected layer, the second fully connected layer, and the output layer are connected in sequence.

[0019] Preferably, the preset water quality index data includes: residual chlorine data, turbidity data, temperature data, dissolved carbon data, UV 254 data, and pH data.

[0020] Preferably, use the quartile method of the box plot, the K-nearest neighbor algorithm, and the linear normalization method to remove outliers, fill in missing values, and scale the data of the original water quality dataset to obtain preprocessed data, including:

[0021] Use the read_excel function in pandas to read the Excel file of the original water quality dataset, and use the try-except statement to capture and handle the FileNotFoundError exception and ParserError exception in the original water quality dataset;

[0022] Perform an expected comparison and correction on the data types of the feature columns and target columns of the original water quality dataset;

[0023] Use the quartile method based on the box plot to filter outliers from the original water quality dataset and use the locally weighted regression method to smooth the data of the original water quality dataset;

[0024] Use KNNImputer to fill in the missing values for the n_neighbors parameter of the time series characteristics and data density in the original water quality dataset;

[0025] Use the Min-Max normalization method to divide the original water quality dataset after the preprocessing is completed to obtain the preprocessed data.

[0026] Preferably, the hyperparameter search space includes: the range of the number of training rounds is from 100 to 600; the range of the batch size is from 20 to 40; the range of the learning rate is 1e -6 to 1e -2 .

[0027] Preferably, the loss function is: mean absolute error.

[0028] Preferably, use the SHAP explainer to calculate the SHAP values of each feature of each sample in the preprocessed data during the training phase, and draw and output the honeycomb plot and the feature importance circular plot according to the SHAP values, including:

[0029] Use GradientExplainer to create the SHAP explainer;

[0030] Use the shap_values in the SHAP explainer to calculate the SHAP values of each feature of each sample in the preprocessed data;

[0031] Draw and output the honeycomb plot and the feature importance circular plot respectively according to the SHAP values using the shap.summary_plot and plt.pie functions.

[0032] Preferably, the receptive shape of the input layer is (window_size, train_X.shape); where window_size is the time window size; train_X.shape is the number of input features.

[0033] Preferably, the filter parameter range of the first Conv1D convolutional layer is from 64 to 256; the convolutional kernel size parameter range of the first Conv1D convolutional layer is from 3 to 9; the filter parameter range of the second Conv1D convolutional layer is from 32 to 128; the convolutional kernel size parameter range of the second Conv1D convolutional layer is from 3 to 9; the cell number parameter range of the Bidirectional LSTM layer is set from 64 to 256; the cell number parameter range of the fully connected layer is set from 32 to 128.

[0034] The present invention discloses the following technical effects:

[0035] The present invention provides a method for predicting trihalomethanes in a water supply network based on a fusion model. By integrating a convolutional neural network, a bidirectional long short-term memory network, and a multi-head attention mechanism, the problems of high cost, long time consumption, and high human input in conventional detection methods are solved, and reliable prediction results are achieved using a small amount of training data. BRIEF DESCRIPTION OF THE DRAWINGS

[0036] In order to more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the following will briefly introduce the drawings required in the embodiments. Obviously, the drawings in the following description are only some embodiments of the present invention. For those of ordinary skill in the art, without creative efforts, other drawings can also be obtained based on these drawings.

[0037] Figure 1 Schematic diagram of the prediction process of trihalomethanes in a water supply network based on a fusion model provided by an embodiment of the present invention;

[0038] Figure 2 Flowchart of THMs prediction provided by an embodiment of the present invention;

[0039] Figure 3 Graph of the prediction results of each target variable of trihalomethanes in a water supply network provided by an embodiment of the present invention;

[0040] Figure 4 Summary graph of the SHAP feature importance of each target variable of trihalomethanes in a water supply network provided by an embodiment of the present invention;

[0041] Figure 5 Schematic diagram of the contribution degree of each input feature of trihalomethanes in a water supply network to the prediction results of target variables;

[0042] Figure 6 Schematic diagram of the optimal model evaluation index of the fusion time series prediction model based on 480 sets of training data provided by an embodiment of the present invention. DETAILED DESCRIPTION OF THE EMBODIMENTS

[0043] The following will clearly and completely describe the technical solutions in the embodiments of the present invention with reference to the drawings in the embodiments of the present invention. Obviously, the described embodiments are only some embodiments of the present invention, rather than all embodiments. All other embodiments obtained by those of ordinary skill in the art based on the embodiments of the present invention without creative efforts belong to the scope of protection of the present invention.

[0044] The object of the present invention is to provide a prediction method for trihalomethanes in water supply networks based on a fusion model, so as to realize timely and accurate prediction of the generated concentration of THMs in water supply networks at a lower cost and visualize the importance of each input feature.

[0045] To make the above objects, features and advantages of the present invention more obvious and understandable, the present invention will be further described in detail below with reference to the accompanying drawings and specific embodiments.

[0046] Figure 1 It is a schematic diagram of the prediction process of trihalomethanes in water supply networks based on a fusion model provided by an embodiment of the present invention. As Figure 1 shown, the present invention provides a prediction method for trihalomethanes in water supply networks based on a fusion time series model, including:

[0047] Step 100: Collect preset water quality index data in the target monitored water supply pipeline to obtain an original water quality data set;

[0048] Step 200: Use the quartile method of the box plot, the K-nearest neighbor algorithm, and the linear normalization method to remove outliers, fill in missing values, and scale the data in the original water quality data set to obtain preprocessed data;

[0049] Step 300: Construct a CNN-BiLSTM-MultiHead Attention fusion prediction model; the CNN-BiLSTM-MultiHead Attention fusion prediction model includes an input layer, a convolutional and pooling layer, a MultiHeadAttention layer, a ScaledDotProductAttention layer, a bidirectional LSTM layer, and a fully connected layer; the input layer is used to traverse the preprocessed data by using a fixed-size window sliding technique, and determine the target value corresponding to the last time step of the window as the sample label, so as to effectively capture the dependence relationship and change trend of water quality parameters in the time dimension; the MultiHeadAttention layer is used to perform linear projection, multi-head segmentation and merging, and linear integration on the input query, key, and value, and capture data features from different subspaces to make it focus on the global dependence relationship in the data; the ScaledDotProductAttention layer is used to dynamically allocate weights to the MultiHeadAttention layer according to time segments to enhance the model's ability to capture complex spatio-temporal patterns; the fully connected layer is used for non-linear transformation and output mapping; L2 regularization technology is introduced between the layers of the CNN-BiLSTM-MultiHead Attention fusion prediction model. By constraining the model parameters, the model has stronger generalization ability, avoids overfitting of the model, and improves the prediction performance of the model on unknown data.

[0050] Step 400: Define the hyperparameter optimization space and the model evaluation function;

[0051] Step 500: Input the preprocessed data into the CNN-BiLSTM-MultiHead Attention fusion prediction model for prediction to obtain the model prediction result, and calculate the pre-evaluation result by using the model evaluation function for the prediction result;

[0052] Step 600: Iteratively optimize the CNN-BiLSTM-MultiHead Attention fusion prediction model according to the pre-evaluation result and the hyperparameter optimization space by using the Bayesian optimization algorithm, and determine the hyperparameter combination of the CNN-BiLSTM-MultiHead Attention fusion prediction model by using the 5-fold cross-validation evaluation method to obtain the pre-trained model;

[0053] Step 700: Retrain the pre-trained model by using the preprocessed data and calculate each evaluation index to obtain the verification result of the pre-trained model. If the verification result meets the preset index threshold, determine the pre-trained model as the trihalomethane prediction model;

[0054] Step 800: Calculate the SHAP value of each feature of each sample of the preprocessed data in the training stage by using the SHAP interpreter, and draw and output a honeycomb chart and a feature importance circular chart according to the SHAP value to show the contribution degree of each feature of the water quality dataset to be detected to the trihalomethane prediction result, and enhance the interpretability of the trihalomethane prediction model;

[0055] Step 900: Collect the preset water quality index data in the target detection water supply pipeline to obtain the water quality dataset to be detected;

[0056] Step 1000: Input the water quality dataset to be detected into the trihalomethane prediction model to obtain the trihalomethane prediction result.

[0057] Further, the CNN-BiLSTM-MultiHead Attention fusion prediction model also embeds a Conv1D convolutional layer, a MaxPooling1D pooling layer, a Flatten layer, a Reshape layer, and an output layer;

[0058] The input layer, the first Conv1D convolutional layer, the first MaxPooling1D pooling layer, the second Conv1D convolutional layer, the second MaxPooling1D pooling layer, the Flatten layer, the Reshape layer, the MultiHeadAttention layer, the ScaledDotProductAttention layer, the output layer, the first fully-connected layer, the second fully-connected layer, and the output layer are connected in sequence.

[0059] Specifically, the preset water quality index data includes: residual chlorine data, turbidity data, temperature data, dissolved carbon data, UV 254 data, and pH data.

[0060] Preferably, the quartile method of the box plot, the K-nearest neighbor algorithm, and the linear normalization method are used to remove outliers, fill in missing values, and scale the data of the original water quality dataset to obtain preprocessed data, including:

[0061] Use the read_excel function in pandas to read the Excel file of the original water quality dataset, and use the try-except statement to capture and handle the FileNotFoundError exception and ParserError exception in the original water quality dataset;

[0062] Perform an expected comparison and correction on the data types of the feature columns and target columns of the original water quality dataset;

[0063] Use the box plot method (IQR) to detect and remove outliers in the input features (6 water quality parameters) and target variables (4 THMs concentrations) that exceed the range of ±1.5×IQR to ensure the normality of the data distribution. Use KNNImputer (k = 5) to fill in the missing values for the n_neighbors parameter of the time series characteristics and data density in the original water quality dataset;

[0064] Scale the input features and target variables to the range of [0, 1] through Min-Max normalization to obtain the preprocessed data.

[0065] Optionally, the hyperparameter search space includes: the range of the number of training epochs is from 100 to 600; the range of the batch size is from 20 to 40; the range of the learning rate is from 1e -6 to 1e -2 .

[0066] Optionally, the loss function is: mean absolute error.

[0067] Preferably, the filter parameter range of the first Conv1D convolutional layer is from 64 to 256; the parameter range of the convolutional kernel size of the first Conv1D convolutional layer is from 3 to 9; the filter parameter range of the second Conv1D convolutional layer is from 32 to 128; the parameter range of the convolutional kernel size of the second Conv1D convolutional layer is from 3 to 9; the parameter range of the number of units in the Bidirectional LSTM layer is set to be from 64 to 256; the parameter range of the number of units in the fully connected layer is set to be from 32 to 128.

[0068] Preferably, calculate the SHAP values of the features of the water quality data samples to be detected in the training stage of the trihalomethane prediction model, and draw and output a honeycomb diagram and a feature importance circular diagram according to the SHAP values, including:

[0069] Create the SHAP explainer using GradientExplainer;

[0070] Calculate the SHAP values of the features of each sample of the preprocessed data using shap_values in the SHAP explainer;

[0071] Draw and output the honeycomb diagram and the feature importance circular diagram respectively using the shap.summary_plot and plt.pie functions according to the SHAP values.

[0072] Specifically, in combination with Figure 2 Specifically illustrate as follows:

[0073] Furthermore, the first step: data collection and preprocessing:

[0074] Write the corresponding program code in the pyCharm environment to convert the original data into a standard format suitable for model input, and at the same time ensure the data quality in all aspects, and check and correct potential data problems through trial operation:

[0075] ① Use the read_excel function of pandas to read the Excel file, and use the try-except statement to capture FileNotFoundError and ParserError exceptions to handle file path errors or format problems.

[0076] ② Check the data types of the read data to ensure that the data types of the input feature columns (residual chlorine, DOC, UV254, pH, water temperature, and turbidity) and the target variable columns (trichloromethane, dichlorobromomethane, dibromochloromethane, and tribromomethane) meet the expectations. If there are illegal data types, convert them or prompt the user to correct them.

[0077] ③Detect outliers based on the interquartile method (IQR) of the box plot. For skewed distribution data, delete outliers exceeding ±1.5×IQR.

[0078] ④Use KNNImputer (k = 5) to fill in missing values, and adjust the n_neighbors parameter according to the time series characteristics and data density of the data. When the data has small fluctuations and strong continuity, appropriately increase this value; when the data has large fluctuations and obvious local changes, decrease this value. After filling, check the data quality to ensure that the filled values are within a reasonable range.

[0079] Specifically, the second step: construction of the fusion time series model:

[0080] First, perform standardization or normalization on the input data to ensure that different features are on the same scale and avoid some features having too much influence on the model. Use a sliding window technique with a window size of 16 to construct time series samples as the input of the model; then, construct a CNN-BiLSTM-MultiHead Attention fusion prediction model. Among them, use two layers of Conv1D convolutional layers to extract local features of the input data, and the number and size of the convolutional kernels in each layer can be adjusted according to the data characteristics. Add a MaxPooling1D pooling layer after the convolutional layer for dimensionality reduction and reducing the computational amount, while retaining important feature information; customize the MultiHeadAttention and ScaledDotProductAttention classes to calculate the attention weights between the query (Query), key (Key), and value (Value), map the input data to multiple subspaces, calculate the attention weights respectively, and implement the multi-head attention mechanism through parallel computing to enhance the model's ability to focus on different features; then use the BidirectionalLSTM layer to capture the long-term dependencies of the time series, where the forward LSTM is used to capture past information and the backward LSTM is used to capture future information, and the number of LSTM units is determined by hyperparameter optimization; finally, add a fully connected layer (Dense) after the BiLSTM layer, with the number of hidden units being dense_units, the activation function being ReLU, and the tanh activation function being used to output the prediction result.

[0081] Preferably, the third step: hyperparameter optimization:

[0082] First, define a model evaluation function to calculate the evaluation metrics of the model, including mean squared error (MSE), root mean squared error (RMSE), mean absolute error (MAE), coefficient of determination (R 2) Mean Absolute Percentage Error (MAPE) and accuracy. Combining with the model characteristics and operation experience, hyperparameters such as L2 regularization coefficient, number of training epochs of CNN, batch size, number of convolutional layer filters, convolutional kernel size, number of LSTM layer units, number of fully connected layer units, learning rate, etc. and their search spaces are defined respectively. Then, the Bayesian optimization algorithm is used to guide the model to run with multiple (the number of times can be set as needed) random seeds, and 5-fold cross-validation is used to evaluate the performance of the model under different hyperparameter combinations. The negative mean square error is used as the scoring metric to accurately screen out the optimal hyperparameters for each run. Finally, based on the R 2 indicator in all the running results, the global best hyperparameter combination is determined to obtain the optimal prediction model.

[0083] Optionally, Step 4: Prediction performance evaluation:

[0084] 1) Reconstruction and training of the optimal model

[0085] The preprocessed dataset is retrained using the optimal prediction model, and the final evaluation metrics are calculated using the model evaluation function. Combining the prediction model performance evaluation criteria or setting thresholds for each evaluation metric according to actual needs, if a certain evaluation metric is higher than the threshold, the prediction performance is determined to be "unqualified", and when all metrics are lower than the threshold, the prediction performance is determined to be "qualified", and the prediction performance evaluation result is output. When the model prediction performance is qualified, information such as the best model parameters and optimal structure learned during the training process is saved as the final model file, which can be directly loaded for external validation or promotion applications.

[0086] 2) Analysis of model prediction results

[0087] ① Judge whether the comprehensive performance of the model meets the standard

[0088] By setting the thresholds of each evaluation metric, it is automatically judged whether the comprehensive performance meets the standard. Suppose the threshold of R 2 is set to 0.9. When the actually calculated R 2 of the model is greater than 0.9, the model evaluation metric (R 2 ) "qualified" is automatically output; when the actually calculated R 2 of the model is less than 0.9, the model evaluation metric (R 2 ) "unqualified" is output, and so on. Corresponding thresholds can also be set for other evaluation metrics according to relevant standards or actual operation requirements. When all evaluation metrics are qualified, the output result shows "The model comprehensive performance evaluation level is: up to standard", and when one of the evaluation metrics is unqualified, the output result shows "The model comprehensive performance evaluation level is: not up to standard"

[0089] ② Output improvement suggestions for the model

[0090] Insufficient data volume has specific impacts on model training in multiple aspects. Such as underfitting, overfitting, slow convergence speed, and poor model running stability. If there is no problem with the overall framework of the model, the main reason for the unqualified comprehensive performance is that the input data volume is insufficient. The minimum data volume requirement can be predicted through the qualified situation of the evaluation indicators corresponding to the historical data volume.

[0091] Take model R 2 value as an example. Using the historical data volume as the input variable and the corresponding historical R 2 as the output variable, collect the evaluation indicators and prediction results corresponding to different data of the model. Use the quadratic polynomial regression model to extract the characteristics of different data volumes as inputs, and extract the evaluation indicators and prediction results corresponding to different data as output characteristics. Fit the data and predict the change trend of future output characteristics, and the minimum data volume requirement that meets the conditions and the probability distribution of the model performance reaching the standard can be calculated.

[0092] ③ Interpretability of the model

[0093] Take all the training data as background data, use GradientExplainer to create a SHAP explainer, and estimate the baseline behavior of the model with the background data; call the shap_values method of the explainer to calculate the SHAP values of each feature of each sample of the test data. Since the model has multiple output targets, the obtained shap_values is a list corresponding to each output target; then process the data shape matching problem. If the number of samples or features does not match, adjust it. Take the average value of the time window dimension to convert the data into two dimensions. Fill in when the number of features is insufficient and intercept when it is excessive; finally, use shap.summary_plot to draw the SHAP Summary Plot (hive plot) to show the influence degree of the features on the model output. At the same time, calculate the average value of the absolute value of the processed SHAP values, and use the plt.pie function to draw a circular diagram of feature importance to present the proportion of the importance of each feature and show the importance of each feature.

[0094] Specifically, the fifth step: result output and display:

[0095] Through evaluation indicators (such as MSE, MAE, R 2 , MAPE, accuracy rate, and RMSE) and visualization charts, intuitively analyze the prediction performance of the model, and save the optimal hyperparameters for subsequent use.

[0096] 1) Output the optimal result

[0097] Select the R from multiple runs 2Output the evaluation metrics of its validation set and test set for the highest optimal result; output the optimal hyperparameter configuration for subsequent model reproduction and tuning; based on the optimal calculation result, further analyze and output the contribution degree of each input feature in the calculation process.

[0098] 2) Output the evaluation results of the model performance

[0099] Based on the results of data preprocessing, judge whether the input data of the model is qualified and output relevant warning information; based on the trial run of the model, judge whether the model can run normally and output modification suggestions; according to the calculation results of the model prediction performance evaluation metrics, output the true values and predicted values of each prediction target test set, output the evaluation metrics of the test set and validation set, judge whether the comprehensive performance evaluation level meets the standard, and output improvement suggestions; output the final prediction result.

[0100] 3) Visualization display

[0101] For each target variable (such as chloroform, bromodichloromethane, etc.), draw a bar chart to show the comparison between the true value and the predicted value; according to the improvement suggestions of the model performance evaluation and the influence trend of the data volume on the prediction result, draw a scatter plot and a fitting curve to visually show the data requirements under the same conditions; according to the contribution degree of each input feature to the model calculation result, draw a hive chart summarizing the SHAP feature importance of each target variable of trihalomethanes and a circular chart of the importance of each input feature of trihalomethanes to the target variable.

[0102] 4) Save the prediction result

[0103] Save the evaluation metrics of each run (including MSE, MAE, R 2 , MAPE, accuracy rate, and RMSE of the validation set and test set), the optimal hyperparameters (such as the number of training epochs, batch size, the number of filters in convolutional layer 1, the kernel size of convolutional layer 1, the number of filters in convolutional layer 2, the kernel size of convolutional layer 2, the number of BiLSTM units, the number of units in the fully connected layer, learning rate, etc.), the influence trend of different data volumes on the evaluation metrics, and the probability of meeting the standard into an Excel file respectively.

[0104] 5) Save as a model

[0105] After the model training meets the standard, it can be automatically saved as a model. It can store information such as the model parameters and structure learned during the training process for subsequent direct loading and use, shortening the model calculation and running time.

[0106] Preferably, the THMs prediction method is established through the above steps, which fully combines the advantages of algorithms such as CNN, BiLSTM, and multi-head attention mechanism. It can effectively capture local and global features in time series data and accurately predict the generation concentration of trihalomethanes in the water supply network. At the same time, it can also comprehensively and systematically evaluate the prediction performance of the model based on the prediction results and evaluation indicators, deeply understand the generalization ability and prediction accuracy of the model, and provide a strong basis for the optimization and application of the model.

[0107] Furthermore, an application example. Take the water supply network of a certain city as an example. The local water company has formulated systems such as "daily inspection, monthly inspection, and semi-annual inspection" in terms of network water quality monitoring and safety guarantee, and strictly follows the relevant requirements in the "Sanitary Standard for Drinking Water" (GB 5749-2022) to regularly detect more than 40 indicators such as residual chlorine, turbidity, trihalomethanes, and haloacetic acids in the water from each water plant and at the taps at the ends of the water supply network. According to the test results in 2024, all the test indicators are within the national standards. However, from the data of the maximum detected concentrations of each THMs indicator, the concentration of chloroform accounts for the main part, with the maximum value being 0.03 mg / L (detected in June), followed by 0.0292 mg / L (detected in September), reaching half of the standard limit (0.06 mg / L), indicating a certain water quality risk; from the ratio of the detected values to the limits of each THMs indicator and the data, abnormal detected values occurred in January, May, and October. In particular, the abnormal value in January was 0.605, exceeding half of the standard requirement (the limit is 1), which needs to be verified and taken seriously; in addition, the maximum values of the ratios of the detected values to the limits of each THMs indicator in January, June, August, and November all exceeded half of the standard limit, indicating that there are risks in the water quality change trends in the above months, with January and November being the most serious.

[0108] Specifically, to further improve the THMs detection efficiency of the water supply network in this city and comprehensively ensure the water quality safety of the water supply network. The THMs prediction method proposed in this embodiment is adopted. Eight typical monitoring points of different types of water users on a certain pipeline are selected. Six conventional water quality indicators, namely residual chlorine, turbidity, temperature, dissolved carbon (DOC), UV 254 , and pH, are used as input variables, and TCM, DCBM, DBCM, and TBN are used as target variables. The data of each variable are collected, and a CNN-BiLSTM-MultiHeadAttention fusion time series prediction model is established and trained and optimized, so as to predict and evaluate the THMs concentration and change trend at the target monitoring points, providing a decision-making basis for operation and management personnel. The specific implementation method is as follows:

[0109] 1) Data collection and preprocessing

[0110] Read the Excel file containing training data at the specified path using pandas; automatically extract information such as the target monitoring point number, input features, and target variables, and use the interquartile range (IQR) method to detect and remove outliers in the input features (6 water quality parameters) and target variables (4 THMs concentrations) that exceed the range of ±1.5×IQR to ensure the normality of the data distribution; use the K-nearest neighbor (KNN) method with K = 5 to fill in the missing values in the input features and target variables to complete data preprocessing.

[0111] 2) Model construction

[0112] In the PyCharm environment, use Min-Max normalization to scale the input features and target variables to the range [0, 1]; construct a CNN-BiLSTM-MultiHead Attention fusion time series prediction model that includes a convolutional layer, a pooling layer, an attention layer, a bidirectional LSTM layer, and a fully connected layer. Among them, the input layer receives data with a shape of (window_size, train_X.shape), where window_size is the defined time window size and train_X.shape is the number of input features. Then, the data enters the first convolutional layer conv1, and the filter parameter range of this layer is set to 64 - 256 according to running experience; the parameter range of the convolutional kernel size is set to 3 - 9, use the'relu' activation function and the padding method is'same' to extract features from the input data. Subsequently, it passes through the max pooling layer pool1 with a pooling size of 2 to downsample the output of the convolutional layer. After that, the data enters the second convolutional layer conv2, whose filter parameter range is set to 32 - 128, and the parameter range of the convolutional kernel size is set to 3 - 9. Similarly, use the'relu' activation function and the'same' padding method to further extract features. Then, it passes through the max pooling layer pool2 with a pooling size of 2 to downsample again. Next, use the Flatten layer to flatten the output of the pooling layer into a one-dimensional vector, and then use the Reshape layer to reshape it into a shape of (1, -1). Next, introduce the multi-head attention mechanism (the number of heads is set to 9 and the dimension is set to 117) to process the reshaped data and capture important feature relationships. The output of the attention mechanism enters the bidirectional LSTM layer lstm_out, and the parameter range of the number of LSTM units is set to 64 - 256 and does not return sequences. The output of the bidirectional LSTM layer passes through the fully connected layer, and the number of units in this layer is set to 32 - 128, with 64 neurons, use the'relu' activation function and the 'tanh' activation function to output the prediction result. The entire model connects the input layer and the output layer through the Model class and is compiled using'mae' as the loss function, 'adam' as the optimizer, and'mean_squared_error' as the evaluation metric.

[0113] Define the time window size as 16, use all the data for training, and use the last set of sample data of each target monitoring point as the test set.

[0114] 3) Hyperparameter optimization

[0115] Use Bayesian optimization (gp_minimize) to perform hyperparameter optimization. First, use the Integer and Real classes in the skopt library to define the search range of hyperparameters. Among them, use Integer to define the range of integer hyperparameters. For example, epochs (number of training rounds) is between 100 and 600, batch_size (batch size) is between 20 and 40, etc. Use Real to define the range of floating-point hyperparameters, such as learning_rate (learning rate) is between 1e -6 to 1e -2 . Then, according to the parameter ranges set in the model construction process, construct a deep learning model containing convolutional layers, multi-head attention layers, bidirectional LSTM layers, and fully connected layers in the function based on the input hyperparameters. Compile the model using the Adam optimizer, add early stopping and learning rate decay callback functions for training, and use the R 2 value of the validation set as the evaluation metric. Since it is a minimization algorithm, return the negative R 2 value. Call the gp_minimize function to perform 15 iterations of search, and try 10 different combinations of hyperparameters each time. Finally, extract the optimal combination of hyperparameters that minimizes the objective function from the search results, and then reconstruct and train the model with these optimal hyperparameters to obtain the model with the best performance. Assume that 480 sets of sample data are used for training, and the optimal combination of hyperparameters can be calculated through hyperparameter optimization as shown in Table 1.

[0116] Table 1

[0117] Serial number Hyperparameter name Optimal parameter value 1 L2 regularization coefficient 0.0009 2 Number of training epochs 233 3 Batch size 44 4 Number of filters in the first convolutional layer 197 5 Kernel size of the first convolutional layer 4 6 Number of filters in the second convolutional layer 117 7 Kernel size of the second convolutional layer 7 8 Number of BiLSTM units 201 9 Number of units in the fully connected layer 100 10 Learning rate 0.0001 11 Learning rate decay factor 0.0222

[0118] 4) Prediction performance evaluation

[0119] Reconstruct and train using the optimal hyperparameters in Table 1 to form the optimal model.

[0120] Furthermore, set the performance evaluation thresholds of the fusion time series prediction model as: MSE ≤ 0.6, MAE ≤ 0.6, R 2 ≥ 0.9, MAPE ≤ 10%, Accuracy ≥ 80%.

[0121] Furthermore, the analysis results of the evaluation metrics of the integrated time series prediction model are as follows: ① Mean Squared Error (MSE): 0.0447. This MSE value is relatively small, indicating that the overall prediction error of the model on the test set is small and meets the requirements.

[0122] ② Mean Absolute Error (MAE): 0.0965. This reflects that there is an average deviation of about 0.0965 between the predicted values and the true values of the model, meeting the requirements.

[0123] ③ Coefficient of Determination (R 2 ): 0.9811. It shows that the model can approximately explain 55.1% of the data variation and has a certain explanatory ability, but still needs improvement.

[0124] ④ Mean Absolute Percentage Error (MAPE): 4.38%. Generally, a MAPE within 10% indicates a high prediction accuracy. Therefore, the model performs well in this regard and meets the requirements.

[0125] ⑤ Root Mean Squared Error (RMSE): 0.2114. It indicates that the average deviation between the predicted values and the true values of the model is about 0.2114, meeting the requirements.

[0126] ⑥ Accuracy: 93.75%. This shows that the proportion of samples for which the prediction results of the model meet the relative error threshold requirements is relatively high, and the overall prediction accuracy of the model is very good.

[0127] Furthermore, the evaluation conclusion can be drawn: "Qualified".

[0128] 5) Result Output

[0129] Using the optimal model for calculation, the prediction results of each target variable can be output as Figure 3 shown; the summary chart of SHAP feature importance for each target variable can be obtained as Figure 4 shown; the contribution degree of each input feature to the prediction results of the target variable can be obtained as Figure 5 shown; the evaluation metrics of the integrated time series prediction model can be obtained as Figure 6 shown. Furthermore, the content output based on the calculation results of the optimal model with 480 groups of sample training data is:

[0130] True values and predicted values of chloroform in the test set:

[0131] True values: [4.4420 8.8404 8.1515 2.7239 2.4327 11.1693 1.861 4.3170];

[0132] Predicted values: [4.4110 8.8323 8.1323 2.5936 2.4012 11.1812 1.7985 4.3022];

[0133] True and predicted values of the test set for dibromochloromethane:

[0134] True values: [3.4450 4.9690 6.3395 2.6867 2.7055 10.0643 2.0164 4.1738];

[0135] Predicted values: [3.4502 4.9812 6.2843 2.6799 2.6990 9.8013 2.0024 3.8739];

[0136] True and predicted values of the test set for bromodichloromethane:

[0137] True values: [2.9377 3.0148 4.8871 2.8456 3.0450 7.1864 2.3574 4.4195];

[0138] Predicted values: [2.9341 2.97801 4.9477 2.8547 2.8981 7.3243 2.3870 3.3532];

[0139] True and predicted values of the test set for tribromomethane:

[0140] True values: [1.5021 0.4716 3.9566 0.5404 0.6251 1.1218 0.4601 0.8660];

[0141] Predicted values: [1.6299 0.5937 4.0120 0.6182 0.6060 1.1868 0.5178 0.7718].

[0142] Preferably, the THMs prediction method exhibits very good performance in aspects such as data preprocessing, model construction, hyperparameter optimization, model evaluation, and code structure. Methods such as KNN for filling missing values, box plots for removing outliers, and MinMaxScaler for feature scaling are used to ensure data quality; a fusion model of CNN, multi-head attention mechanism, and BiLSTM is adopted to effectively extract local features, focus on key information, and handle long-term dependencies; the Bayesian optimization algorithm is used to search for optimal hyperparameters to improve the model's generalization ability; multiple evaluation metrics and callback functions are used to ensure the stability and effectiveness of training; the modular design of the code enhances its modifiability, scalability, and repeatability. This method is applicable to time series data prediction tasks and has broad application value especially in fields such as water quality monitoring, financial market prediction, weather forecasting, and industrial production monitoring.

[0143] The beneficial effects of the present invention are as follows:

[0144] By integrating a convolutional neural network, a bidirectional long short-term memory network, and a multi-head attention mechanism, the present invention realizes efficient learning and extraction of the change characteristics of multiple conventional water quality indicators in the water supply network, and can still maintain good prediction accuracy even when the training data is small, significantly improving the prediction performance and generalization ability of the model in predicting multiple target variables using multiple input variables.

[0145] The various embodiments in this specification are described in a progressive manner. Each embodiment focuses on the differences from other embodiments. The same or similar parts among the various embodiments can be referred to each other.

[0146] Specific examples are used in this article to elaborate on the principle and implementation manner of the present invention. The description of the above embodiments is only used to help understand the method and its core idea of the present invention; at the same time, for those of ordinary skill in the art, there will be changes in the specific implementation manner and application scope according to the idea of the present invention. In summary, the content of this specification should not be construed as a limitation to the present invention.

Claims

1. A prediction method for trihalomethanes in water supply networks based on a fusion time series model, characterized in that, Including: Collecting the preset water quality index data in the target monitored water supply pipeline to obtain the original water quality data set; Using the quartile method of the box plot, the K-nearest neighbor algorithm, and the linear normalization method to perform outlier removal, missing value filling, and data scaling on the original water quality data set to obtain preprocessed data; Constructing a CNN-BiLSTM-MultiHead Attention fusion prediction model; the CNN-BiLSTM-MultiHeadAttention fusion prediction model includes an input layer, a convolutional and pooling layer, a MultiHeadAttention layer, a ScaledDotProductAttention layer, a bidirectional LSTM layer, and a fully connected layer; the input layer is used to traverse the preprocessed data using a fixed-size window sliding technique and determine the target value corresponding to the last time step of the window as the sample label; the MultiHeadAttention layer is used to perform linear projection, multi-head segmentation and merging, and linear integration on the input query, key, and value; the ScaledDotProductAttention layer is used to perform dynamic weight allocation on the MultiHeadAttention layer according to time segments; the fully connected layer is used to perform non-linear transformation and output mapping; Defining the hyperparameter optimization space and the model evaluation function; Inputting the preprocessed data into the CNN-BiLSTM-MultiHead Attention fusion prediction model for prediction to obtain the model prediction result, and calculating the prediction result using the model evaluation function to obtain the pre-evaluation result; Iteratively optimizing the CNN-BiLSTM-MultiHead Attention fusion prediction model according to the pre-evaluation result and the hyperparameter optimization space using the Bayesian optimization algorithm, and using the 5-fold cross-validation evaluation method to determine the hyperparameter combination of the CNN-BiLSTM-MultiHead Attention fusion prediction model to obtain a pre-trained model; Re-training the pre-trained model using the preprocessed data and calculating each evaluation index to obtain the verification result of the pre-trained model. If the verification result meets the preset index threshold, the pre-trained model is determined as the trihalomethane prediction model; Using the SHAP interpreter to calculate the SHAP value of each feature of each sample in the preprocessed data during the training stage, and drawing and outputting a honeycomb plot and a feature importance circular plot according to the SHAP value; Collecting the preset water quality index data in the target detection water supply pipeline to obtain the water quality data set to be detected; Inputting the water quality data set to be detected into the trihalomethane prediction model to obtain the trihalomethane prediction result.

2. The method for predicting trihalomethane in a water supply network based on a fusion model according to claim 1, wherein The CNN-BiLSTM-MultiHead Attention fusion prediction model also embeds a Conv1D convolutional layer, a MaxPooling1D pooling layer, a Flatten layer, a Reshape layer, and an output layer; The input layer, the first Conv1D convolutional layer, the first MaxPooling1D pooling layer, the second Conv1D convolutional layer, the second MaxPooling1D pooling layer, the Flatten layer, the Reshape layer, the MultiHeadAttention layer, the ScaledDotProductAttention layer, the output layer, the first fully connected layer, the second fully connected layer, and the output layer are connected in sequence.

3. A method for predicting trihalomethanes in a water supply network based on a fusion model according to claim 1, wherein, The preset water quality index data includes: residual chlorine data, turbidity data, temperature data, dissolved carbon data, UV 254 data, and pH data.

4. A prediction method for trihalomethanes in a water supply network based on a fusion model according to claim 1, characterized in that, Using the quartile method of the box plot, the K-nearest neighbor algorithm, and the linear normalization method to perform outlier removal, missing value filling, and data scaling on the original water quality dataset, obtaining preprocessed data, including: Using the read_excel function in pandas to read the Excel file of the original water quality dataset, and using the try-except statement to capture and handle the FileNotFoundError exception and ParserError exception in the original water quality dataset; Performing expected comparison and correction on the data types of the feature columns and target columns of the original water quality dataset; Using the quartile method based on the box plot to filter outliers in the original water quality dataset and using the locally weighted regression method to smooth the data in the original water quality dataset; Using KNNImputer to fill in the missing values of the n_neighbors parameter of the time series characteristics and data density in the original water quality dataset; Using the Min-Max normalization method to divide the original water quality dataset after the preprocessing is completed to obtain the preprocessed data.

5. A method for predicting trihalomethanes in a water supply network based on a fusion model according to claim 1, characterized in that The hyperparameter search space includes: the range of the number of training epochs is from 100 to 600; the range of the batch size is from 20 to 40; the range of the learning rate is from 1e -6 to 1e -2 .

6. A method for predicting trihalomethanes in a water supply network based on a fusion model according to claim 1, characterized in that, The loss function is: mean absolute error.

7. A prediction method for trihalomethanes in a water supply network based on a fusion model according to claim 1, characterized in that Using the SHAP interpreter to calculate the SHAP values of each feature of each sample in the preprocessed data during the training phase, and drawing and outputting a honeycomb plot and a feature importance circular plot according to the SHAP values, including: Using GradientExplainer to create the SHAP interpreter; Using shap_values in the SHAP interpreter to calculate the SHAP values of each feature of each sample in the preprocessed data; Drawing and outputting the honeycomb plot and the feature importance circular plot respectively according to the SHAP values using the shap.summary_plot and plt.pie functions.

8. A prediction method for trihalomethanes in a water supply network based on a fusion model according to claim 1, characterized in that, The receiving shape of the input layer is (window_size, train_X.shape); where window_size is the time window size; train_X.shape is the number of input features.

9. The method for predicting trihalomethanes in a water supply network based on a fusion model according to claim 2, wherein The filter parameter range of the first Conv1D convolutional layer is from 64 to 256; The parameter range of the convolution kernel size of the first layer of the Conv1D convolutional layer is from 3 to 9; the parameter range of the filters of the second layer of the Conv1D convolutional layer is from 32 to 128; The parameter range of the convolution kernel size of the second layer of the Conv1D convolutional layer is from 3 to 9; the parameter range of the number of units of the Bidirectional LSTM layer is set to be from 64 to 256; the parameter range of the number of units of the fully connected layer is set to be from 32 to 128.

Citation Information

Cited By

  • Transform-BiLSTM water quality prediction method based on shop interpretability analysis

    CN121188745A

  • Methane concentration monitoring method based on deep learning

    CN121211085A

  • Rapid prediction method for trichloromethane generation potential in water disinfection process

    CN121459991A