Python implements PSO particle swarm optimization support vector machine classification model (svc 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

PSO is the English abbreviation of Particle Swarm Optimization (Particle Swarm Optimization), which is a population-based stochastic optimization technique proposed by Eberhart and Kennedy in 1995. Particle swarm algorithm imitates the flocking behavior of insects, herds of animals, birds and fish, etc. These groups look for food in a cooperative way, and each member of the group constantly changes by learning its own experience and the experience of other members. its search mode.

This project builds a classification model through the PSO particle swarm optimization support vector machine classification 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 View 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 9 variables, no missing values in the data, and 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 y variable histogram

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

4.2 y=1 sample x1 variable distribution histogram

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

4.3 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 Create feature data and label data

The key code is as follows:

5.2 Dataset Split

Use the train_test_split() method to divide according to 80% training set and 20% test set. The key code is as follows:

6. Construct PSO particle swarm optimization algorithm to optimize support vector machine classification model

Mainly use the PSO particle swarm optimization algorithm to optimize the support vector machine classification algorithm for object classification.

6.1 Optimal parameters for PSO particle swarm optimization algorithm

Optimal parameters:

6.2 Optimal parameter value construction model

7. Model evaluation

7.1 Evaluation indicators and results

Evaluation indicators mainly include accuracy rate, precision rate, recall rate, F1 score and so on.

It can be seen from the above table that the F1 score is 0.967, indicating that the model works well.

The key code is as follows:

7.2 Classification report

As can be seen from the above figure, the F1 score of classification 0 is 0.97; the F1 score of classification 1 is 0.97.

7.3 Confusion Matrix

As can be seen from the figure above, there are 2 samples that are actually 0 and predicted to be not 0; there are 4 samples that are actually 1 and predicted to be not 1, and the overall prediction accuracy is good.

8. Conclusion and prospect

To sum up, this paper uses the PSO particle swarm optimization algorithm to find the optimal parameter values of the support vector machine SVC algorithm to build a classification model, and finally proves that the model we proposed works well. This model can be used for forecasting of everyday products.

# objective function
def svcPso(params):
    # SVM parameters
    if abs(params[0]) > 0: # Judgment
        c = round(params[0], 2) # assignment
    else:
        c = round(params[0], 2) + 1.0 # assignment


# *************************************************** *******************************
 
# The materials required for the actual combat of this machine learning project, the project resources are as follows:
 
# project instruction:
 
# Link: https://pan.baidu.com/s/1c6mQ_1YaDINFEttQymp2UQ
 
# Extract code: thgk
 
# *************************************************** *******************************


print('----------------PSO particle swarm optimization algorithm optimizes support vector machine classification model - optimal result display -------------- ---')
print("The best C is " + str(abs(best_C)))
print("The best gamma is " + str(abs(best_gamma)))

# Apply the optimized optimal parameter values to build a support vector machine classification model
svc_model = SVC(C=best_C, gamma=best_gamma) # modeling
svc_model.fit(X_train, y_train) # Fitting
y_pred = svc_model.predict(X_test) # prediction

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

List of actual combat collections of machine learning projects

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge algorithm skill treeHome pageOverview 48758 people are studying systematically