Note: This is amachine learning practical project (comes with data + code + documentation + video Explanation), if you needdata + code + documentation + video explanation, you can go directly to the end of the article to get it.
1.Item background
Fire-fly algorithm (FA) was proposed by Yang of Cambridge University in 2009. As one of the latest swarm intelligence optimization algorithms, this algorithm has the advantages of better convergence speed and convergence accuracy, and is easy to implement in engineering.
This project optimizes the random forest classification model through the FA firefly optimization algorithm.
2.number 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:
serial number |
variable name |
describe |
1 |
x1 |
|
2 |
x2 |
|
3 |
x3 |
|
4 |
x4 |
|
5 |
x5 |
|
6 |
x6 |
|
7 |
x7 |
|
8 |
x8 |
|
9 |
x9 |
|
10 |
x10 |
|
11 |
and |
dependent variable |
The data details are as follows (partially displayed):
3.Data preprocessing
3.1 用PandasTools
Use the head() method of the Pandas tool to view the first five rows of data:
Key code:
3.2 View with missing data
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 2,000 pieces of data.
Key code:
3.3Number prescriptiveness calculation
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.1y change column pattern
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 Compatibility 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.Special expedition process
5.1 Create feature data and label data
The key code is as follows:
5.2 Dataset Splitting
The train_test_split() method is used to divide 80% of the training set and 20% of the test set. The key code is as follows:
6. Construct FA firefly optimization algorithm to optimize random forest classification model
The FA firefly optimization algorithm is mainly used to optimize the random forest classification algorithm for target classification.
6.1 FA Firefly Optimization Algorithm Finds Optimal Parameter Values
Optimal parameters:
6.2 Build model with optimal parameter values
serial number |
Model name |
parameter |
1 |
Random forest classification model |
max_depth=best_max_depth |
2 |
min_samples_leaf=best_min_samples_leaf |
7. Model evaluation
7.1 Evaluation indicators and results
Evaluation indicators mainly include accuracy, precision, recall, F1 score, etc.
Model name |
Indicator name |
Index value |
test set |
||
Random forest classification model |
Accuracy |
0.9300 |
precision |
0.974 |
|
recall rate |
0.8905 |
|
F1 score |
0.9303 |
As can be seen from the table above, the F1 score is 0.9303, indicating that the model is effective.
The key code is as follows:
7.2 Classification report
As can be seen from the above figure, the F1 score for classification 0 is 0.93; the F1 score for classification 1 is 0.93.
7.3 Confusion matrix
As can be seen from the above figure, there are 5 samples that are actually 0 and are not predicted to be 0; there are 23 samples that are actually 1 and are not predicted to be 1. The overall prediction accuracy is good.
8.Conclusion outlook
To sum up, this article uses the FA firefly optimization algorithm to find the optimal parameter values of the random forest algorithm to build a classification model, which ultimately proves that the model we proposed works well. This model can be used for predictions of everyday products.
# 本次机器学习项目实战所需的资料,项目资源如下:
# 项目说明:
链接:https://pan.baidu.com/s/1mgc6qEX13l8CPk149m9grQ
提取码:0p12
For more practical projects, please see the list of practical machine learning projects: