Python implements the SSA intelligent sparrow search algorithm to optimize the LightGBM regression model (LGBMRegressor algorithm) project practice

Note: This is a practical machine learning project (comes with data + code + document + video explanation). If you need data + code + document + video explanation, you can go directly to the end of the article. Obtain.

1. Project background

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

In the process of foraging for sparrows, they are divided into discoverers (explorers) and joiners (followers). Discoverers are responsible for finding food in the population and providing foraging areas and directions for the entire sparrow population, while joiners use Discoverers come to get food. In order to obtain food, sparrows can usually adopt two behavioral strategies: finder and joiner for foraging. Individuals in a 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 own predation rate. Additionally, sparrow populations engage in anti-predator behavior when they perceive danger.

This project optimizes the LightGBM 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). The statistics of the data items are as follows:

The data details are as follows (partially displayed):

3. Data preprocessing

3.1 Use Pandas tool to view data

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

Key code:

3.2 Missing data view

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

As you can see from the picture above, there are a total of 11 variables, no missing values in the data, and a total of 1,000 pieces of data.

Key code:

3.3 Data descriptive statistics

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

The key code is as follows:

4. Exploratory data analysis

4.1 y variable distribution histogram

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

4.2 Relevance Analysis

As can be seen from the figure above, the larger the value, the stronger the correlation. Positive values are positive correlations, and negative values are negative correlations.

5. Feature engineering

5.1 Create feature data and label data

The key code is as follows:

5.2 Data set split

The train_test_split() method is used to divide 80% of the training set and 20% of the validation set. The key code is as follows:

6. Construct SSA Sparrow search algorithm to optimize LightGBM regression model

The SSA Sparrow search algorithm is mainly used to optimize the LightGBM algorithm for target regression.

6.1 SSA sparrow search algorithm to find optimal parameter values

Optimal parameter values:

6.2 Optimal parameter construction model

Here, the LightGBM regression model is constructed with optimal parameters:

7. Model evaluation

7.1 Evaluation indicators and results

Evaluation indicators mainly include R-squared, mean square error, explained variance, absolute error, etc.

As can be seen from the table above, the R-squared score is 0.9436, and the model effect is good.

The key code is as follows:

7.2 Comparison chart between true value and predicted value

It can be seen from the above figure that the fluctuations of the real value and the predicted value are basically consistent, and the model effect is good.

8. Conclusion and outlook

To sum up, this paper uses the SSA Sparrow search algorithm to find the optimal parameter values of the LightGBM algorithm to build a regression model, which ultimately proves that the model we proposed works well. This model can be used for daily product modeling work.

# Define boundary function
def Bounds(s, Lb, Ub):
    temp=s
    for i in range(len(s)):
        if temp[i] < Lb[0, i]: # Less than the minimum value
            temp[i] = Lb[0, i] # Take the minimum value
        elif temp[i] > Ub[0, i]: # Greater than the maximum value
            temp[i] = Ub[0, i] # take the maximum value
 
 
#************************************************ *******************************
 
# The materials required for the actual implementation of this machine learning project, the project resources are as follows:
 
# project instruction:
 
# Link: https://pan.baidu.com/s/1-P7LMzRZysEV1WgmQCpp7A

# Extraction code: 5fv7
 
#************************************************ *******************************
 
 
# Build features and labels
X = data.drop(columns=['y']) # Build features
y = data['y'] # Build label

# Division of data set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# SSA initialization parameters
SearchAgents_no = 10 # Number of populations
Max_iteration = 1 #Number of iterations

For more practical projects, please see the list of practical machine learning projects:

Machine learning project practical collection list_Machine learning practical project_Pang Ge’s really good blog-CSDN blog

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Algorithm skill tree Home page Overview 51862 people are learning the system