# Prediction of Mg Alloy Corrosion Based on Machine Learning Models

Authors: Zhenxin Lu; Shujing Si; Keying He; Yang Ren; Shuo Li; Shuman Zhang; Yi Fu; Qi Jia; Heng Bo Jiang; Haiying Song; Mailing Hao
Journal: Advances in Materials Science and Engineering
 (2022)

---

## Abstract

Magnesium alloy is a potential biodegradable metallic material characterized by bone-like elastic modulus, which has great application prospects in medical, automotive, and aerospace industries owing to its bone-like elastic modulus, biocompatibility, and lightweight properties. However, the rapid corrosion rates of magnesium alloys seriously limit their applications. This study collected magnesium alloys’ corrosion data and developed a model to predict the corrosion potential, based on the chemical composition of magnesium alloys. We compared four machine learning algorithms: random forest (RF), multiple linear regression (MLR), support vector machine regression (SVR), and extreme gradient boosting (XGBoost). The RF algorithm offered the most accurate predictions than the other three machine learning algorithms. The input effects on corrosion potential have been investigated. Moreover, we used feature creation (transforming chemical component characteristics into atomic and physical characteristics) so that the input characteristics were not limited to specific chemical compositions. From this result, the model’s application range was widened, and machine learning was used to verify the accuracy and feasibility of predicting corrosion of magnesium alloys.


---

## Body

## 1. Introduction

Magnesium alloys have excellent application prospects in the medical, automotive, and aerospace industries owing to their bone-like elastic modulus, biocompatibility, and lightweight properties [1–4]. However, the rapid corrosion rates of magnesium alloys seriously limit their broad application [5, 6]. Therefore, developing corrosion-resistant magnesium alloys is vital to increase their application potential.Alloying is an efficient way to improve the properties of metals since various properties can be enhanced by introducing alloying elements into Mg [7, 8]. Moreover, studying the chemical composition of a magnesium alloy is a vital technique for controlling its corrosion rate. Many scholars have added alloying elements (such as Ca [9, 10], Zn [11, 12], Al [13], Mn [14, 15], Sr [16], and Sn [17, 18]) to increase corrosion resistance of magnesium alloys. However, the corrosion-resistant magnesium alloys depend on local optimization conducted through repeated experiments and constant adjustment of the alloy concentration. Thus, optimizing the alloy composition is vital by testing every possibility. However, this process is costly and requires many human and material resources. Most previous studies have only focused on binary and ternary alloys and have not examined the effects of various elements.The application of machine learning in studying the alloy has increased in recent years [19, 20]. Machine learning uses experimental data to develop a mathematical model and establish the quantitative relationship between the target attributes and features. This model is used to predict the target alloy properties with different compositions. Machine learning has been widely studied for predicting the corrosivity of low-alloy steels [21–23]. Further, machine learning exhibits excellent prediction accuracy, robust fitting, and analysis ability. However, the prediction of the corrosion rate of a magnesium alloy by machine learning has not been reported.This study collected corrosion data from the published literature and developed a prediction model for the chemical composition, corrosion potential, and corrosion of magnesium alloys using machine learning. Feature creation was used to broaden the model’s applicability.


## 2. Datasets and Machine Learning

### 2.1. Corrosion Data and Pretreatment

This study collected material data from the published literature. A total of 164 different alloy compositions were considered, consisting of a primary test environment (chloride ion concentration) and two target properties (corrosion potential and current density). In Table1, each row of corrosion data has nine material characteristics, one environmental characteristic, and the associated corrosion potential. Table S1 presents the current density.Table 1
Chemical composite features, environment features, and target attributes.
FeaturesUnitDescriptionsData rangeMaterialElementswt%Al content0.015–17.000Zn content0.001–30.000Mn content0.002–1.000Si content0.001–1.110Fe content0.001–0.040Cu content0–10.000Ca content0–4.000Sr content0–1.960Sn content0–9.140EnvironmentalCl ionmol·L−1Chloride ion concentration0.087–1.026Target propertyIcorrA·cm−2Corrosion current0–0.167EcorrVCorrosion potential−2.027–−1.117Most studies did not provide the content of trace alloying components in the chemical composition analysis of magnesium alloy, and these studies marked the components lower than the detection threshold. We used the average value of the original data to fill in the missing values and enhance the accuracy of the machine learning. We selected the logarithm of 10 as the target attribute of the current density since the value of the current density was too small. Finally, all the materials, environmental characteristics, and target attributes were standardized.


### 2.2. Feature Creation

Predicting the corrosion of magnesium alloys with other materials is impossible because the material characteristics of the model only contained nine elements. Therefore, we applied the feature creation method proposed by Diao et al. [24] to the model’s generalization ability and application range. The physical properties of atoms were used to predict the material characteristics. We selected 16 atomic and physical characteristics (Table 2) to replace magnesium alloy’s chemical composition. The physical attributes, corresponding to each element, were obtained from the WebElements periodic table [25]. The following equation defines the feature creation method:(1)X=fA1XA1+fA2XA2+…+fAnXAn,where X represents one of the physical characteristics of an atom; fAn stands for the mass fraction of elements; and An and XAn denote the corresponding feature.Table 2
Atomic and physical characteristics used in feature creation.
Physical propertyDescriptionsVMolar volumeKBulk modulusλThermal conductivityMpMelting pointΔHfEnthalpy of fusionΔHvEnthalpy of vaporizationΔHaEnthalpy of atomizationRaAtomic radiusRmCovalent radius of a single moleculeRvVan der Waals radiiRcCovalent radiusENpPauling electronegativityENsSanderson electronegativityENaAllred–Rochow electronegativityE1First ionization energyE2Second ionization energy


### 2.3. Experimental Procedure

The acquired data were grouped into a training set (70%) and a testing set (30%). The training set was used to optimize the parameters of the prediction model. However, a testing set was used to identify the model’s prediction accuracy. Random forest (RF), support vector machine regression (SVR), multiple linear regression (MLR), and extreme gradient boosting (XGBoost) were used to train the collected data. The optimum parameters of the model were determined using the grid search approach and k-fold cross-validation (k = 5). Moreover, we compared the training results of each algorithm and selected the algorithm with the highest accuracy for prediction. The above calculations were performed using Python software and the scikit-learn toolkit for all algorithms, except the XGBoost, which used the XGBoost library [26].


### 2.4. Evaluation of Model Performance

The ratio of the information, captured by the model, to the information in the actual label was measured. The closer the model is to 1.00, the better the model fit is. The mean absolute error (MAE), which was the average of the absolute error between the predicted value and the actual value, was used to measure the model accuracy. These models are calculated as follows:(2)R2=1−∑i=1nyi−y^i2∑i=1nyi−yi¯2,MAE=1n∑i=1nyi−y^i2,where yi represents the measured value; y^i stands for the predicted value; and yi¯ is the average value of the measured value.


## 2.1. Corrosion Data and Pretreatment

This study collected material data from the published literature. A total of 164 different alloy compositions were considered, consisting of a primary test environment (chloride ion concentration) and two target properties (corrosion potential and current density). In Table1, each row of corrosion data has nine material characteristics, one environmental characteristic, and the associated corrosion potential. Table S1 presents the current density.Table 1
Chemical composite features, environment features, and target attributes.
FeaturesUnitDescriptionsData rangeMaterialElementswt%Al content0.015–17.000Zn content0.001–30.000Mn content0.002–1.000Si content0.001–1.110Fe content0.001–0.040Cu content0–10.000Ca content0–4.000Sr content0–1.960Sn content0–9.140EnvironmentalCl ionmol·L−1Chloride ion concentration0.087–1.026Target propertyIcorrA·cm−2Corrosion current0–0.167EcorrVCorrosion potential−2.027–−1.117Most studies did not provide the content of trace alloying components in the chemical composition analysis of magnesium alloy, and these studies marked the components lower than the detection threshold. We used the average value of the original data to fill in the missing values and enhance the accuracy of the machine learning. We selected the logarithm of 10 as the target attribute of the current density since the value of the current density was too small. Finally, all the materials, environmental characteristics, and target attributes were standardized.


## 2.2. Feature Creation

Predicting the corrosion of magnesium alloys with other materials is impossible because the material characteristics of the model only contained nine elements. Therefore, we applied the feature creation method proposed by Diao et al. [24] to the model’s generalization ability and application range. The physical properties of atoms were used to predict the material characteristics. We selected 16 atomic and physical characteristics (Table 2) to replace magnesium alloy’s chemical composition. The physical attributes, corresponding to each element, were obtained from the WebElements periodic table [25]. The following equation defines the feature creation method:(1)X=fA1XA1+fA2XA2+…+fAnXAn,where X represents one of the physical characteristics of an atom; fAn stands for the mass fraction of elements; and An and XAn denote the corresponding feature.Table 2
Atomic and physical characteristics used in feature creation.
Physical propertyDescriptionsVMolar volumeKBulk modulusλThermal conductivityMpMelting pointΔHfEnthalpy of fusionΔHvEnthalpy of vaporizationΔHaEnthalpy of atomizationRaAtomic radiusRmCovalent radius of a single moleculeRvVan der Waals radiiRcCovalent radiusENpPauling electronegativityENsSanderson electronegativityENaAllred–Rochow electronegativityE1First ionization energyE2Second ionization energy


## 2.3. Experimental Procedure

The acquired data were grouped into a training set (70%) and a testing set (30%). The training set was used to optimize the parameters of the prediction model. However, a testing set was used to identify the model’s prediction accuracy. Random forest (RF), support vector machine regression (SVR), multiple linear regression (MLR), and extreme gradient boosting (XGBoost) were used to train the collected data. The optimum parameters of the model were determined using the grid search approach and k-fold cross-validation (k = 5). Moreover, we compared the training results of each algorithm and selected the algorithm with the highest accuracy for prediction. The above calculations were performed using Python software and the scikit-learn toolkit for all algorithms, except the XGBoost, which used the XGBoost library [26].


## 2.4. Evaluation of Model Performance

The ratio of the information, captured by the model, to the information in the actual label was measured. The closer the model is to 1.00, the better the model fit is. The mean absolute error (MAE), which was the average of the absolute error between the predicted value and the actual value, was used to measure the model accuracy. These models are calculated as follows:(2)R2=1−∑i=1nyi−y^i2∑i=1nyi−yi¯2,MAE=1n∑i=1nyi−y^i2,where yi represents the measured value; y^i stands for the predicted value; and yi¯ is the average value of the measured value.


## 3. Results and Discussion

### 3.1. Modeling and Application of Forecasting Models

The optimal parameters corresponding to the prediction models of the four algorithms are listed in Table3.Table 3
Optimization parameters of each algorithm (other parameters are default).
ModelsParametersEcorrIcorrRFn_estimators = 150n_estimators = 183SVRKernel = rbf,C = 100, gramma = 0.1Kernel = rbf,C = 100, gramma = 0.1XGBoostBooster = gbtree, learning_rate = 0.2,n_estimators = 10Booster = gbtree, learning_rate = 0.3,n_estimators = 10Figure1 shows the prediction results for the corrosion potential and current density, which correspond to the four algorithms. The prediction results were drawn as functions of the measured data, which are equal to the measured data for a perfect prediction model. All the data points fall on the diagonal lines; the closer the data points to the diagonal lines, the more accurate the prediction results. The MLR model had a poor fitting effect on the corrosion potential and current density. The model indicates that the influences of the different elements on the corrosion potential and current density in magnesium alloys are complex, and no superficial linear relationships are established. Moreover, the fitting effect for the SVM on the two target attributes was poor. Both RF and XGBoost models are two integrated techniques that combine two decision trees to improve the model ability and offer excellent fitting effects. Table 4 lists the R2 and MAE calculation results for each optimized model in the training and testing sets. Both the MLR and SVM models had small R2 and large MAE values in the testing set and training set, respectively, showing that the fitting effects of MLR and SVM were poor. Although the XGBoost model fitted the training dataset well, it performed poorly on the testing set, which might be due to overfitting. Therefore, the RF model was used in the subsequent analysis.Figure 1
Prediction results for corrosion potential and current density corresponding to the four algorithms. (a–d) Prediction results for corrosion potential by the RF, MLR, SVR, and XGBoost algorithms, respectively. (e–h) Prediction results for current corrosion by RF, MLR, SVR, and XGBoost algorithms, respectively.
(a)(b)(c)(d)(e)(f)(g)(h)Table 4
Prediction accuracy of each algorithm (theR2 and MAE calculation results for each optimized model in the training set and testing set).
ModelsEcorrIcorrR2MAER2MAETraining setTesting setTraining setTesting setTraining setTesting setTraining setTesting setRF0.9000.7900.0250.0420.8500.7300.0290.041MLR0.4300.1400.0790.0930.1600.0400.0920.043SVR0.5900.5000.0670.0780.6000.4000.0680.090XGBoost0.9700.7400.0130.0490.9500.6900.0200.053The RF model evaluates the importance of each feature by measuring the influence of the feature value on the target attribute. Based on the excellent goodness of fit of the model, the prediction model mastered the law of the influence of each input on the corrosion potential and corrosion current. Thus, the model with the feature importance is considered reliable. The Al content is an important factor that affects the corrosion potential. The Mg-Al alloy had a higher corrosion potential than the pure Mg alloy [27]. Mg17Al12 (β-phase) and Al8Mn5 in Mg-Al alloys act as cathodes [28–30]. Moreover, iron is an impurity in magnesium alloys. Iron forms a second pure metal phase owing to its lack of solubility in magnesium alloys. Microcurrent coupling between the Mg matrix (α-phase) and this second phase leads to corrosion [31–33]. Zn and Mn have significant effects on the corrosion potential and corrosion current. Zn forms MgxZny phases and acts as a local cathode for the Mg matrix. With this process, the cathode reaction speed is accelerated, resulting in a higher corrosion rate [34, 35]. The addition of manganese can effectively reduce the Fe content in aluminum-magnesium alloys to a specific limit because the iron is isolated in AlMnFe intermetallic compounds, which significantly reduces the degree of microgalvanic coupling [36]. Chloride ion is an environmental factor that affects the corrosion current. Chloride ions can dissolve the protective layer of magnesium hydroxide on the magnesium alloy, converting it to magnesium chloride, making the surface more active and more prone to corrosion [37, 38]. The importance of the input characteristics corresponding to corrosion potential and corrosion current prediction models is shown in Figure 2.Figure 2
Importance of the input characteristics corresponding to (a) corrosion potential and (b) corrosion current prediction models.
(a)(b)


### 3.2. Prediction of Corrosion by Creating Feature Methods

Using the feature creation method, a new set of material features was created, and the above four methods were used to predict the corrosion potential and corrosion current. Table5 lists the R2 and MAE calculation results of each optimized model in the training and testing sets. The results showed that the fitting effect of RF regression was the best accuracy comparatively. Therefore, the RF model was used in the subsequent analysis.Table 5
Prediction accuracy of each algorithm (theR2 and MAE calculation results for each optimized model in the training set and testing set).
ModelsEcorrIcorrR2MAER2MAETraining setTesting setTraining setTesting setTraining setTesting setTraining setTesting setRF0.8500.6600.0280.0560.7900.6100.0300.070MLR0.4700.2600.0690.1000.2100.1400.0800.093SVR0.5200.3500.0710.0890.4500.3700.0800.085XGBoost0.9400.5300.0220.0630.9500.4800.0190.078Based on the excellent prediction accuracy of the RF model, the relationship between the corrosion potential and corrosion current is well fitted using the feature creation method. However, no specific study has proven the relationships between the corrosion potential, corrosion current, and the physical properties of these atoms. While this process faces challenges in explaining the model, the sample composition is still represented by its atomic and physical properties, which helps predict corrosion using the feature creation method [24]. The RF model and its corresponding characteristic importance are shown in Figure 3.Figure 3
Predictive performance for corrosion potential and corrosion current using the RF model and corresponding characteristic importance.
(a)(b)(c)(d)


### 3.3. Generalization Ability of the Machine Learning Model

Nine rows of new corrosion data, comprising transition metal zirconium, nickel, and the rare earth element neodymium, were collected from the published literature as verification sets (TableS2) since none of the samples in our previous dataset contained three elements. We used this validation set to verify the model’s generalization. After the chemical compositions of these samples were transformed into the previously selected atomic and physical parameters via the method created using the above features, the optimized stochastic forest model was used to obtain the prediction results for each sample. The methods are shown in Figure 4. The prediction model demonstrated a high level of accuracy.Figure 4
Comparisons of experimental measurements with predicted values using machine learning models: (a) comparison of corrosion potential results and (b) comparison of corrosion current results. Nine validation set samples were used.
(a)(b)


## 3.1. Modeling and Application of Forecasting Models

The optimal parameters corresponding to the prediction models of the four algorithms are listed in Table3.Table 3
Optimization parameters of each algorithm (other parameters are default).
ModelsParametersEcorrIcorrRFn_estimators = 150n_estimators = 183SVRKernel = rbf,C = 100, gramma = 0.1Kernel = rbf,C = 100, gramma = 0.1XGBoostBooster = gbtree, learning_rate = 0.2,n_estimators = 10Booster = gbtree, learning_rate = 0.3,n_estimators = 10Figure1 shows the prediction results for the corrosion potential and current density, which correspond to the four algorithms. The prediction results were drawn as functions of the measured data, which are equal to the measured data for a perfect prediction model. All the data points fall on the diagonal lines; the closer the data points to the diagonal lines, the more accurate the prediction results. The MLR model had a poor fitting effect on the corrosion potential and current density. The model indicates that the influences of the different elements on the corrosion potential and current density in magnesium alloys are complex, and no superficial linear relationships are established. Moreover, the fitting effect for the SVM on the two target attributes was poor. Both RF and XGBoost models are two integrated techniques that combine two decision trees to improve the model ability and offer excellent fitting effects. Table 4 lists the R2 and MAE calculation results for each optimized model in the training and testing sets. Both the MLR and SVM models had small R2 and large MAE values in the testing set and training set, respectively, showing that the fitting effects of MLR and SVM were poor. Although the XGBoost model fitted the training dataset well, it performed poorly on the testing set, which might be due to overfitting. Therefore, the RF model was used in the subsequent analysis.Figure 1
Prediction results for corrosion potential and current density corresponding to the four algorithms. (a–d) Prediction results for corrosion potential by the RF, MLR, SVR, and XGBoost algorithms, respectively. (e–h) Prediction results for current corrosion by RF, MLR, SVR, and XGBoost algorithms, respectively.
(a)(b)(c)(d)(e)(f)(g)(h)Table 4
Prediction accuracy of each algorithm (theR2 and MAE calculation results for each optimized model in the training set and testing set).
ModelsEcorrIcorrR2MAER2MAETraining setTesting setTraining setTesting setTraining setTesting setTraining setTesting setRF0.9000.7900.0250.0420.8500.7300.0290.041MLR0.4300.1400.0790.0930.1600.0400.0920.043SVR0.5900.5000.0670.0780.6000.4000.0680.090XGBoost0.9700.7400.0130.0490.9500.6900.0200.053The RF model evaluates the importance of each feature by measuring the influence of the feature value on the target attribute. Based on the excellent goodness of fit of the model, the prediction model mastered the law of the influence of each input on the corrosion potential and corrosion current. Thus, the model with the feature importance is considered reliable. The Al content is an important factor that affects the corrosion potential. The Mg-Al alloy had a higher corrosion potential than the pure Mg alloy [27]. Mg17Al12 (β-phase) and Al8Mn5 in Mg-Al alloys act as cathodes [28–30]. Moreover, iron is an impurity in magnesium alloys. Iron forms a second pure metal phase owing to its lack of solubility in magnesium alloys. Microcurrent coupling between the Mg matrix (α-phase) and this second phase leads to corrosion [31–33]. Zn and Mn have significant effects on the corrosion potential and corrosion current. Zn forms MgxZny phases and acts as a local cathode for the Mg matrix. With this process, the cathode reaction speed is accelerated, resulting in a higher corrosion rate [34, 35]. The addition of manganese can effectively reduce the Fe content in aluminum-magnesium alloys to a specific limit because the iron is isolated in AlMnFe intermetallic compounds, which significantly reduces the degree of microgalvanic coupling [36]. Chloride ion is an environmental factor that affects the corrosion current. Chloride ions can dissolve the protective layer of magnesium hydroxide on the magnesium alloy, converting it to magnesium chloride, making the surface more active and more prone to corrosion [37, 38]. The importance of the input characteristics corresponding to corrosion potential and corrosion current prediction models is shown in Figure 2.Figure 2
Importance of the input characteristics corresponding to (a) corrosion potential and (b) corrosion current prediction models.
(a)(b)


## 3.2. Prediction of Corrosion by Creating Feature Methods

Using the feature creation method, a new set of material features was created, and the above four methods were used to predict the corrosion potential and corrosion current. Table5 lists the R2 and MAE calculation results of each optimized model in the training and testing sets. The results showed that the fitting effect of RF regression was the best accuracy comparatively. Therefore, the RF model was used in the subsequent analysis.Table 5
Prediction accuracy of each algorithm (theR2 and MAE calculation results for each optimized model in the training set and testing set).
ModelsEcorrIcorrR2MAER2MAETraining setTesting setTraining setTesting setTraining setTesting setTraining setTesting setRF0.8500.6600.0280.0560.7900.6100.0300.070MLR0.4700.2600.0690.1000.2100.1400.0800.093SVR0.5200.3500.0710.0890.4500.3700.0800.085XGBoost0.9400.5300.0220.0630.9500.4800.0190.078Based on the excellent prediction accuracy of the RF model, the relationship between the corrosion potential and corrosion current is well fitted using the feature creation method. However, no specific study has proven the relationships between the corrosion potential, corrosion current, and the physical properties of these atoms. While this process faces challenges in explaining the model, the sample composition is still represented by its atomic and physical properties, which helps predict corrosion using the feature creation method [24]. The RF model and its corresponding characteristic importance are shown in Figure 3.Figure 3
Predictive performance for corrosion potential and corrosion current using the RF model and corresponding characteristic importance.
(a)(b)(c)(d)


## 3.3. Generalization Ability of the Machine Learning Model

Nine rows of new corrosion data, comprising transition metal zirconium, nickel, and the rare earth element neodymium, were collected from the published literature as verification sets (TableS2) since none of the samples in our previous dataset contained three elements. We used this validation set to verify the model’s generalization. After the chemical compositions of these samples were transformed into the previously selected atomic and physical parameters via the method created using the above features, the optimized stochastic forest model was used to obtain the prediction results for each sample. The methods are shown in Figure 4. The prediction model demonstrated a high level of accuracy.Figure 4
Comparisons of experimental measurements with predicted values using machine learning models: (a) comparison of corrosion potential results and (b) comparison of corrosion current results. Nine validation set samples were used.
(a)(b)


## 4. Conclusion

This study proposed a model to predict the corrosion potential and corrosion current of magnesium alloys. After comparing different machine learning models, an RF prediction model was established. The influences of the chemical composition and environmental factors on each sample’s corrosion potential and corrosion current were proved intuitively using the RF algorithm. Then, we used the feature creation method to transform the alloy composition into the specific atomic and physical parameters, which expanded the model application range. Finally, a verification set was used to confirm the high accuracy of the prediction model. Machine learning’s robust data regression and data mining abilities are useful in analyzing magnesium alloy corrosion data, which could serve as a helping tool for researching magnesium alloy corrosion.



---

*Source: 9597155-2022-06-08.xml*