## 2 Materials and Methods
### Methods  
#### 2.2.1 XGBoost Model  
XGBoost is an efficient machine learning model with strong generalization ability, developed by [PERSON] and [PERSON] based on the Gradient Boosting Decision Tree (GBDT) algorithm. The model incorporates key innovations including regularization terms and parallel computing techniques [34]. It operates by iteratively learning multiple decision functions through an ensemble of decision trees, in which each subsequent tree corrects errors from previous predictions. The final prediction is obtained by aggregating weighted outputs from all trees in the ensemble. The regression tree function used in XGBoost can be expressed as follows:  
\[\hat{y}_{i}=\varphi(X_{i})=\sum\limits_{k=1}^{K}f_{k}(X_{i}),f_{k}\in F \tag{2}\]  
where \(f(x)\) represents a regression tree function; \(\hat{y}_{i}\) is the predicted value of the model; \(K\) is the number of decision trees; \(f_{k}\) represents the \(k^{th}\) sub-model; \(X_{i}\) represents the \(i^{th}\) input sample; \(F=\left\{f(x)=\omega_{q(x)}\right\}(q:R^{m}\to T,\omega\in R^{T})\) indicates the space of the regression tree; and \(\omega_{q(x)}\) represents the fraction of leaf nodes \(q\). Based on the predicted values of the regression tree described above, an objective function of the model is available:  
\[O_{\text{bj}}=\sum\limits_{i=1}^{n}l(y_{i},\hat{y}_{i})+\sum\limits_{k=1}^{K} \Omega(f_{k}) \tag{3}\]  
where \(n\) is the total amount of data imported for the \(k^{th}\) regression tree; \(K\) represents all regression trees built; \(\sum\limits_{i=1}^{n}l(y_{i},\hat{y}_{i})\) represents the loss function of the objective function; and \(\sum\limits_{k=1}^{K}\Omega(f_{k})\) represents the regularization term of the objective function. The specific functions are as follows:  
\[\Omega(f_{k})=\gamma T+\frac{1}{2}\lambda\|\omega\|^{2} \tag{4}\]  
where \(T\) represents the number of leaf nodes of the regression tree; \(\omega\) represents the fraction of the leaf node of the regression tree; and \(\gamma_{t},\lambda\) represents the penalty coefficient of the regular term. The optimal value of the loss function in the \(t^{th}\) iteration when the structure of the tree is fixed is as follows:  
\[w_{j}^{*}=\sum\limits_{i\in I_{j}}\frac{g_{i}}{h_{i}+\lambda} \tag{5}\]According to the principle of XGBoost algorithm, the residues of the previous prediction need to be fitted into the newly generated tree. In order to improve the prediction accuracy of the model, the objective function of \(t^{th}\) prediction is subjected to second-order [PERSON] expansion to approximate the original objective and remove the constant term. Finally, \(w_{j}^{*}\) is brought into the objective function to obtain the optimal objective function value under the current tree structure:  
\[O_{\text{bj}}=-\frac{1}{2}\underset{j=1}{\overset{\tau}{\sum}}\frac{\left( \underset{i\in I_{j}}{\sum}g_{i}\right)^{2}}{\underset{i\in I_{j}}{\sum}h_{i}+ \lambda}+\gamma T \tag{6}\]  
In the iterative process, the XGBoost model ensures that each tree's contribution is effectively utilized through a weighting method. It also enhances the model's stability and generalization by incorporating regularization and feature selection [34]. Additionally, the XGBoost model uses a strategy to automatically handle missing feature values, eliminating the need for preprocessing to impute missing features. These improvements lead to higher prediction accuracy, faster processing speeds, and reduced computational costs and complexity [35].  
In this study, the XGBoost model was adopted to model and analyze the spatial distribution of population and its influencing factors in Shanghai. The prediction model construction and influencing factor analysis were conducted using Python's open-source libraries (scikit-learn, XGBoost, SHAP, etc.) on the PyCharm platform.