Python implements SSA intelligent sparrow search algorithm to optimize XGBoost regression model (XGBRegressor algorithm) project combat

Explanation: This is a machine learning practical project (with data + code + documentation + video explanation ). If you need data + code + documentation + video explanation, you can go directly to the end of the article to get it.




1. Project Background

The Sparrow Search Algorithm (SSA) is a new type of swarm intelligence optimization algorithm, which was proposed in 2020 and is mainly inspired by the foraging behavior and anti-predation behavior of sparrows.

In the process of sparrow foraging, it is divided into discoverers (explorers) and joiners (followers). Finders are responsible for finding food and providing foraging areas and directions for the entire sparrow population, while joiners use Finders come to get food. In order to obtain food, sparrows can usually adopt two behavioral strategies of finder and joiner to forage. Individuals in the population monitor the behavior of other individuals in the population, and attackers in the population compete for food resources with high-intake peers to increase their predation rate. In addition, sparrow populations engage in anti-predation behavior when they perceive danger.

This project optimizes the XGBoost regression model through the SSA sparrow search algorithm.

2. Data acquisition

The modeling data for this time comes from the Internet (compiled by the author of this project), and the statistics of the data items are as follows:

The data details are as follows (partial display):

3. Data preprocessing

3.1 View data with Pandas tools

Use the head() method of the Pandas tool to view the first five rows of data:

key code:

3.2 Check missing data

Use the info() method of the Pandas tool to view data information:

  

As can be seen from the above figure, there are a total of 11 variables, and there are no missing values ​​in the data, with a total of 1000 data.

key code:  

3.3 Data descriptive statistics

Use the describe() method of the Pandas tool to view the mean, standard deviation, minimum, quantile, and maximum of the data.

The key code is as follows:  

4. Exploratory Data Analysis

4.1 Histogram of y variable distribution

Use the hist() method of the Matplotlib tool to draw a histogram:

4.2 Correlation analysis

As can be seen from the figure above, the larger the value, the stronger the correlation. A positive value is a positive correlation, and a negative value is a negative correlation.

5. Feature engineering  

5.1 Establish feature data and label data

The key code is as follows:

5.2 Dataset splitting

Use the train_test_split() method to divide according to 80% of the training set and 20% of the verification set. The key codes are as follows:

6. Construct the SSA sparrow search algorithm to optimize the XGBoost regression model

Mainly use the SSA sparrow search algorithm to optimize the XGBoost algorithm for target regression.

6.1 SSA sparrow search algorithm to find the optimal parameter value

Optimal parameter values:

6.2 Optimal parameter construction model

Here, the XGBoost regression model is constructed through the optimal parameters:

7. Model Evaluation

7.1 Evaluation indicators and results

Evaluation indicators mainly include R square, mean square error, explanatory variance, absolute error and so on.

It can be seen from the above table that the R-square score is 0.8201, and the model works well.

The key code is as follows:

7.2 Comparison chart of actual value and predicted value

From the figure above, it can be seen that the fluctuations of the actual value and the predicted value are basically the same, and the model works well.

8. Conclusion and Outlook

To sum up, this paper uses the SSA sparrow search algorithm to find the optimal parameter values ​​of the XGBoost algorithm to build a regression model, and finally proves that the model we proposed works well. This model can be used for modeling work of everyday products.

# 定义边界函数
def Bounds(s, Lb, Ub):
    temp = s
    for i in range(len(s)):
        if temp[i] < Lb[0, i]:  # 小于最小值
            temp[i] = Lb[0, i]  # 取最小值
        elif temp[i] > Ub[0, i]:  # 大于最大值
            temp[i] = Ub[0, i]  # 取最大值
 
 
# ******************************************************************************
 
# 本次机器学习项目实战所需的资料,项目资源如下:
 
# 项目说明:
 
# 链接:https://pan.baidu.com/s/1-P7LMzRZysEV1WgmQCpp7A 
 
# 提取码:5fv7
 
# ******************************************************************************
 
 
# 构建特征和标签
X = data.drop(columns=['y'])  # 构建特征
y = data['y']  # 构建标签
 
# 数据集的划分
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
 
# SSA初始化参数
SearchAgents_no = 10  # 种群数量
Max_iteration = 1  # 迭代次数

For more project practice, see the list of machine learning project practice collections:

List of actual combat collections of machine learning projects


 

Guess you like

Origin blog.csdn.net/weixin_42163563/article/details/132708515