A static software defect prediction method based on self-walking oversampling ensemble learning
By employing a self-stepping oversampling ensemble learning method and utilizing random forest and SMOTE oversampling strategies, the difficulty and weight of sample classification are dynamically adjusted, which solves the problems of insufficient data and class imbalance in static software defect prediction and improves the prediction accuracy and generalization ability of the model.
Patent Information
- Application Number
- CN202411618826.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-13
- Publication Date
- 2025-11-21
- Estimated Expiration
- 2044-11-13
AI Technical Summary
In static software defect prediction tasks, there are problems such as insufficient training data and class imbalance, which leads to a lack of focus in the model training process, serious overfitting, and affects the defect prediction performance.
We employ a self-stepping oversampling ensemble learning method, using random forest as the base classifier and combining it with the SMOTE oversampling strategy to dynamically adjust the classification difficulty and weight of samples, perform self-stepping factor adjustment, balance the distribution of training data, and improve the generalization performance of the model.
It effectively alleviates the class imbalance problem, reduces the risk of overfitting, and improves the accuracy and generalization ability of the defect prediction model.
Smart Images

Figure CN119645803B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to software defect prediction technology for software engineering applications, specifically to a static software defect prediction method based on self-stepping oversampling ensemble learning. Background Technology
[0002] Static software defect prediction technology analyzes software code or the development process to extract metrics related to software defects, mines historical software repositories, and creates a defect prediction dataset. Machine learning techniques are then used to train a prediction model on this dataset to identify modules that may contain defects. This technology not only helps testing teams clarify their priorities before testing but also provides valuable feedback to development teams, guiding them to pay closer attention to and review potentially error-prone modules during development, thereby improving code quality and stability. In recent years, with the continuous evolution of machine learning theory and technology, machine learning-based static software defect prediction technology has also developed rapidly and is gradually becoming an auxiliary tool that can significantly improve testing efficiency and effectiveness, providing strong support for defect management throughout the software development lifecycle.
[0003] Static software defect prediction primarily predicts the defect predisposition of software program modules, which differs from dynamic software defect prediction, which predicts the distribution of software defects over time. The general process of static software defect prediction includes: First, determining the prediction granularity of program modules (e.g., files, packages, classes, or functions) based on the actual application scenario, and marking each collected program module as either defective (denoted as 1) or defect-free (denoted as 0). Next, extracting metrics related to software defects (e.g., Halstead metric, McCabe metric, CK metric, MOOD metric, code change metric, and other object-oriented metrics), representing each program module as a feature vector. The feature vectors of all program modules and their defect labels together constitute a defect prediction dataset. Based on this, necessary data preprocessing is performed on the dataset (e.g., data standardization, data resampling), and then machine learning algorithms are used to train a defect prediction model. New program modules can then be predicted to have software defects based on the extracted software metrics and the trained model, assisting testers in rationally allocating test resources and improving testing efficiency.
[0004] However, static software defect prediction tasks often face challenges such as insufficient training data and class imbalance, leading to overfitting in the later stages of model training due to excessive focus on noisy samples, resulting in predictions biased towards the majority class (i.e., non-defect classes). Existing static software defect prediction methods often adopt a segmented approach to address class imbalance and overfitting, such as using data resampling and other class imbalance learning methods to handle the former, and ensemble learning to handle the latter. This approach fails to fully explore and utilize the data complexity information inherent in the defect dataset. Due to the lack of means to estimate the difficulty of sample classification, ensemble learning algorithms struggle to distinguish between samples of different difficulty levels when training the base model, resulting in a lack of specificity in the learning process, which in turn affects the generalization performance and defect prediction accuracy of the ensemble learning model. Summary of the Invention
[0005] The purpose of this invention is to overcome the problems faced by static software defect prediction tasks, such as insufficient training data and class imbalance, which lead to a lack of focus in the model training process, and the overfitting that occurs in the later stages of model training due to excessive attention to noisy and abnormal samples, thus affecting the defect prediction performance.
[0006] The technical solution to achieve the purpose of this invention is: a static software defect prediction method based on self-stepping oversampling ensemble learning, comprising the following steps:
[0007] Step 1: Collect software repository data for the current project, extract software metrics from the program modules in the software repository, including Halstead metric, McCabe metric, CK metric, MOOD metric, and code change metric, so that each program module is represented as a feature vector. Then, based on whether the program module has defects, it is marked as defective or non-defective, thereby constructing a defect prediction dataset.
[0008] Step 2: Perform standardization preprocessing on the defect prediction dataset, and divide the training samples into majority class set and minority class set according to the label information of the dataset;
[0009] Step 3: Use random forest as the base classifier in the ensemble learning framework, train the base classifier based on the majority class set and the minority class set, calculate the MCC value of the base classifier on the training set, and update the weights of the base classifier.
[0010] Step 4: Binning is performed on the majority class set and the minority class set. The classification difficulty of the prediction results of the ensemble classifier for the corresponding bin training set samples is calculated, and then the auto-stepping factor α is determined. By dynamically adjusting the sampling weight of each bin, equal probability Bootstrap sampling is performed on the majority class set and the minority class set respectively, so as to specifically reduce the sampling probability of bins with too many samples.
[0011] Step 5: Perform SMOTE oversampling on the sampled minority class data to obtain a balanced training subset;
[0012] Step 6: Repeat steps 3 to 5 above until the number of base classifiers reaches the preset value. Then, weight all the base classifiers according to their classifier weights to obtain an ensemble classifier as the software defect prediction model.
[0013] Step 7: Extract software metrics from the program module to be predicted, representing them as feature vectors of the same dimension as the training samples. Use the trained ensemble classifier to predict the defect tendency of the program module to be predicted. If the prediction probability is less than 0.5, it indicates that the model predicts that the program module has no defects; otherwise, it is predicted to have defects.
[0014] Further, in step 1, collect the software repository data of the current project, extract software metrics from the program modules in the software repository, including Halstead metrics, McCabe metrics, CK metrics, MOOD metrics, code change metrics, etc., so that each program module is represented as a feature vector. Then, based on whether the program module has defects, it is marked as defective or not defective, thereby constructing a defect prediction dataset for training the software defect prediction model. The specific method is as follows:
[0015] For the current software development project, the granularity of defect prediction is set, including files, packages, classes, and functions. Software repository data is mined using version control systems, defect tracking systems, or emails from relevant developers. Metrics related to software defects are extracted, including Halstead metrics, McCabe metrics, CK metrics, MOOD metrics, and code change metrics. Each program module is represented as a feature vector represented by a set of software metrics. Based on whether the module has defects, it is divided into defective modules and defect-free modules, with defective modules denoted as 1 and defect-free modules denoted as 0. This constructs a software defect prediction dataset.
[0016] Further, in step 2, the defect prediction dataset is standardized and preprocessed by dividing the training samples into a majority class set and a minority class set based on the label information of the dataset. The specific method is as follows:
[0017] Step 2.1, standardization preprocessing, transforms the data of each dimension of the training and test samples into a distribution with a mean of 0 and a standard deviation of 1. Let μ(x) be the mean and standard deviation of the j-th feature of the sample. .j ) and σ(x .j Then we have:
[0018]
[0019] Step 2.2: Divide the training samples into a majority class set and a minority class set according to the label information of the dataset. The majority class samples are non-defect class samples, and the minority class samples are defect class samples.
[0020] Furthermore, in step 3, a random forest is used as the base classifier in the ensemble learning framework. The base classifier is trained based on the majority class set and the minority class set, and the MCC value of the base classifier on the training set is calculated. The weights of the base classifier are then updated. The specific method is as follows:
[0021] In the initial stage of ensemble learning, a purely random sampling method is used to sample samples from the majority class and minority class sets, respectively, with the same number of samples from the majority class and minority class in the original training set. These sampled training subsets are then used to train the base classifier f1.
[0022] After the base classifier f1 is trained, the MCC value of the base classifier f1 on the training set is calculated to determine its weight w1. The formula for calculating the MCC is as follows:
[0023]
[0024] Wherein, TP, TN, FP, and FN represent the number of true positives, true negatives, false positives, and false negatives, respectively;
[0025] When there is only one base classifier, its weight is 1. As the number of base classifiers gradually increases, the weight of the j-th base classifier is obtained by normalizing the MCC values of all base classifiers.
[0026]
[0027] Among them, f j This represents the j-th classifier that has been trained. That is, the classifier in the training set The predictive performance index value.
[0028] Further, in step 4, binning is performed on the majority and minority class sets. The classification difficulty of the ensemble classifier's prediction results for the corresponding bin training set samples is calculated, and the auto-stepping factor α is determined. By dynamically adjusting the sampling weight of each bin, equal-probability bootstrap sampling is performed on the majority and minority class sets respectively, in order to specifically reduce the sampling probability of bins with an excessive number of samples. The specific method is as follows:
[0029] For majority class collections and minority class set Based on the estimated difficulty of sample classification, the sample is divided into k bins, with the l-th bin being B. l Represented as:
[0030]
[0031] majority class set and minority class set The sub-boxes are respectively labeled B l,- and B l,+ ;
[0032] Calculate the average contribution of binning to the classification difficulty of the samples:
[0033]
[0034] Where, x s Indicates box B l For any sample in y s It is sample x s The category label, F represents the classifier for x. s The predicted probability, It is a sample pair (x) s ,y s The classification difficulty estimate of |B) l |This is compartment B l The total number of samples. Binning of majority and minority classes B. l,- and B l,+ The average contribution is denoted as h. l,- and h l,+ ;
[0035] The self-synchronization factor is defined as:
[0036]
[0037] Where m represents the total number of base classifiers in the ensemble learning model, and i represents the index of the base classifier currently being trained (1≤i≤m). The sampling weights of the l-th bin of the majority and minority class sets are as follows:
[0038]
[0039] Based on the above weights, the number of samples for each bin in the majority class set is defined as follows:
[0040]
[0041] Similarly, the number of samples per bin in the minority class set is defined as follows:
[0042]
[0043] in, This represents the total number of minority class samples in the training set. It is the total number of samples in the majority class.
[0044] Furthermore, in step 5, the sampled minority class data is oversampled using SMOTE to obtain a balanced training subset. The specific method is as follows:
[0045] The SMOTE oversampling factor is calculated as the ratio of the number of majority class samples to the number of minority class samples in the original dataset.
[0046] For each minority class sample x min Calculate its Euclidean distance to all minority class samples to obtain its k nearest neighbors;
[0047] For each minority class sample x min From its k nearest neighbors, N samples are randomly selected, denoted as x. min (1) ,x min (2) , ..., x min (N) ;
[0048] For each nearest neighbor x min (i) (i = 1, 2, ..., N), synthesize new samples according to the following formula:
[0049] x new =x min +rand(0,1)*(x min (i) -x min )#(11).
[0050] A static software defect prediction system based on self-stepping oversampling ensemble learning is provided, which implements the static software defect prediction method based on self-stepping oversampling ensemble learning to achieve static software defect prediction based on self-stepping oversampling ensemble learning.
[0051] A computer device includes a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it implements the static software defect prediction method based on self-stepping oversampling ensemble learning, thereby realizing static software defect prediction based on self-stepping oversampling ensemble learning.
[0052] A computer-readable storage medium having a computer program stored thereon, wherein when the computer program is executed by a processor, the described static software defect prediction method based on self-stepping oversampling ensemble learning is implemented to achieve static software defect prediction based on self-stepping oversampling ensemble learning.
[0053] Compared with existing technologies, this invention has the following significant advantages: 1) It introduces a self-stepping learning strategy into the ensemble learning framework to estimate the classification difficulty of all samples. This estimation is then used as data complexity information to train the base classifier, thus proposing a self-stepping ensemble learning framework. During the ensemble learning process, the classification difficulty of all samples is estimated, and different learning weights are assigned based on their classification difficulty level, achieving targeted learning for a limited number of training samples. 2) To alleviate the class imbalance problem faced by defect prediction tasks, a SMOTE oversampling strategy is introduced on the basis of the self-stepping ensemble learning framework. An independent SMOTE oversampling operation is performed each time a base classifier is trained to balance the data distribution of the training subset. Each data oversampling process is performed independently, ensuring the diversity of the base classifiers. 3) The predictive performance of the base classifiers is estimated, and corresponding weights are assigned accordingly. These weighted combinations ultimately form a static software defect prediction method based on self-stepping oversampling ensemble learning. Attached Figure Description
[0054] Figure 1 This is a flowchart of a static software defect prediction method based on self-stepping oversampling ensemble learning.
[0055] Figure 2 This is a comparison chart of the experimental results. Detailed Implementation
[0056] To make the objectives, technical solutions, and advantages of this application clearer, the following detailed description is provided in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the scope of this application.
[0057] Combined with appendix Figure 1 The self-stepping oversampling ensemble learning framework proposed in this invention will be described in detail.
[0058] Step 1: Collect software repository data for the current project, extract software metrics from the program modules in the software repository, including Halstead metric, McCabe metric, CK metric, MOOD metric, code change metric, etc., so that each program module is represented as a feature vector. Then, based on whether the program module has defects, it is marked as defective or non-defective, thereby constructing a defect prediction dataset for training the software defect prediction model.
[0059] For the current software development project, the granularity of defect prediction is defined (it can be files, packages, classes, or functions, etc.). Software repository data is mined using version control systems (such as CVS, SVN, or Git), defect tracking systems (such as Bugzilla, Mantis, Jira, or Trac), or information such as emails from relevant developers. By analyzing relevant information from the software development process or source code, metrics related to software defects are extracted. Each program module is represented as a feature vector represented by a set of software metrics, and based on whether the module has defects, it is divided into defective modules (denoted as 1) and defect-free modules (denoted as 0), thus constructing a software defect prediction dataset. When predicting new program modules, software metrics are extracted in the same way, and the program module to be predicted is represented as a feature vector of the same dimension. The dataset used for model training is the training set, while the dataset used for actual prediction or model performance evaluation is the test set.
[0060] The example uses five software defect datasets from NASA as the static software defect prediction datasets for this example. Brief information about these datasets is shown in Table 1.
[0061] Table 1
[0062]
[0063] Where #M represents the number of metrics, #T represents the type of metric used (the NASA dataset uses code metrics (CMs)), #I represents the total number of samples in the dataset, #D represents the total number of defect class samples, and %D represents the percentage of defect class samples. It can be seen that the above defect prediction datasets suffer from a relatively serious class imbalance problem, with the PC3 dataset exhibiting the highest degree of class imbalance.
[0064] To train and test the defect prediction model, this embodiment uses a 5×5-fold cross-validation method to divide each dataset into a training set and a test set. Each dataset is divided into 5 equal parts, and then 4 parts are selected as the training set and 1 part as the test set. Different parts are selected as the test set each time, until each part has served as the test set once, which completes one cycle of 5-fold cross-validation. The above process is repeated 5 times.
[0065] Step 2: Standardize and preprocess the defect prediction dataset, and divide the training samples into majority class and minority class sets according to the label information of the dataset;
[0066] To standardize the units, the training and test sets first need to be preprocessed to convert the data of each dimension of the training and test samples into a distribution with a mean of 0 and a standard deviation of 1. Let μ(x) be the mean and standard deviation of the j-th feature of the training set samples..j ) and σ(x .j Then we have:
[0067]
[0068] Similarly, the test set samples will be processed using the same standardized preprocessing method as the training set, and then the ensemble classifier will be trained on the training set.
[0069] The samples in the training set may contain defective samples or non-defective samples. Based on the sample labels, the training set is divided... All samples are divided into two sets: one set stores majority class samples (generally non-defective classes), and the other set stores minority class samples (generally defective classes), so that the base learner can sample and learn from different sets with different weights.
[0070] Step 3: Train base classifiers based on the majority class set and the minority class set, calculate the MCC value of the base classifiers on the training set, and update the weights of the base classifiers;
[0071] In an ensemble learning framework, given any sample x and an ensemble classifier F, the confidence probability that the ensemble classifier F will predict x as a positive sample (i.e., a defective sample) is obtained. Among them, f j (x) represents the prediction function of the j-th base classifier, w j Here, m represents the weights of the base classifier, and m is the total number of base classifiers. The classification difficulty of a sample (x, y) can be expressed by a function. An estimation is performed, where y∈{0,1} is the true label of the sample. The sample classification difficulty estimation function takes the ensemble classifier F as input and can dynamically estimate the classification difficulty of the samples. The sample classification difficulty distribution contains rich information, including the characteristics of noisy samples and boundary samples, as well as the degree of matching between model capacity and task difficulty.
[0072] In the initial stage of ensemble learning, since there is no base classifier yet, a purely random sampling method is used to sample samples from the majority and minority class sets, respectively, with the same number of samples from the majority and minority classes in the original training set. These sampled training subsets are then used to train the base classifier f1.
[0073] After the base classifier f1 is trained, its predictive performance on the training set is calculated (using the MCC value), and its weight w1 is determined. The MCC value is a commonly used predictive performance metric for imbalanced classification tasks. Taking the confusion matrix in Table 2 as an example, TP, TN, FP, and FN are elements in the confusion matrix, representing true positives, true negatives, false positives, and false negatives, respectively.
[0074] Table 2
[0075]
[0076] The formula for calculating MCC is as follows:
[0077]
[0078] When there is only one base classifier, its weight is 1. As the number of base classifiers gradually increases, the weight of the j-th base classifier can be obtained by normalizing the MCC values of all base classifiers.
[0079]
[0080] Among them, f j This represents the j-th classifier that has been trained. That is, the classifier in the training set The predictive performance index value.
[0081] The prediction results from the ensemble classifier are then used. The classification difficulty of the training set samples is estimated. The sample classification difficulty is estimated by subtracting the predicted probability of the ensemble model from 1. The model's predicted probability represents its confidence in predicting a sample as belonging to that class; the lower the confidence, the greater the classification difficulty. The estimated sample classification difficulty directly depends on the current ensemble model's prediction results, thus dynamically reflecting the classification difficulty of samples under the current ensemble model. This allows for the identification of samples with poor model performance, enabling targeted learning for difficult-to-classify samples during subsequent training of the base classifier.
[0082] Step 4: Binning is performed on the majority class set and the minority class set. The classification difficulty of the prediction results of the ensemble classifier for the corresponding bin training set samples is calculated, and then the auto-stepping factor α is determined. By dynamically adjusting the sampling weight of each bin, equal probability bootstrap sampling is performed on the majority class set and the minority class set respectively, so as to specifically reduce the sampling probability of bins with too many samples.
[0083] The self-learning strategy refers to the fact that this invention uses the training set The majority class samples (generally non-defective samples) and minority class samples (generally defective samples) are binned, that is, the majority class set is binned separately. and minority class set Based on the estimated difficulty of sample classification, the sample is divided into k bins, with the l-th bin being B. l Represented as:
[0084]
[0085] majority class set and minority class set The sub-boxes are respectively labeled B l,- and B l,+ The average contribution of the classification difficulty of these bins can be calculated using the following formula:
[0086]
[0087] Where, x s Indicates box B l For any sample in y s It is sample x s The category label, F represents the classifier for x. s The predicted probability, It is a sample pair (x) s ,y s The classification difficulty estimate of |B) l |This is compartment B l The total number of samples. Binning of majority and minority classes B. l,- and B l,+ The average contribution is denoted as h. l,- and h l,+ .
[0088] As the training process progresses, the ensemble classifier gradually fits the training set, causing the number of simple samples to increase rapidly, leading to a continuous increase in the overall difficulty contribution of simple samples. To address this, this invention introduces a self-synchronization factor α, which dynamically adjusts the sampling weight of each bin to specifically reduce the sampling probability of bins with an excessive number of samples. Specifically, this invention defines the self-synchronization factor as:
[0089]
[0090] Where m represents the total number of base classifiers in the ensemble learning model, and i represents the index of the base classifier currently being trained (1≤i≤m). The sampling weights of the l-th bin of the majority and minority class sets are as follows:
[0091]
[0092] Based on the above weights, the number of samples for each bin in the majority class set is defined as follows:
[0093]
[0094] Similarly, the number of samples per bin in the minority class set is defined as follows:
[0095]
[0096] in, This represents the total number of minority class samples in the training set. is the total number of samples in the majority class. As the training progresses, the self-paced factor α increases, and the classifier pays more attention to the difficult-to-classify samples. In addition, in each iteration of the present invention, the classification difficulty estimate of the samples is updated again to ensure that the sampled majority-class samples are all difficult-to-classify samples for the current ensemble classifier.
[0097] The above process is carried out in the ensemble learning framework. That is, in each iteration of the ensemble learning (i.e., the training of the base classifier), the prediction results of the ensemble classifier composed of the current base classifiers are used to estimate the classification difficulty of the training samples, and binning and weighted sampling are performed according to their classification difficulty estimates. This learning framework that combines ensemble learning and self-paced learning strategies is also called self-paced ensemble learning. Since the estimation of the sample classification difficulty depends on the classifier used. And the training data for the defect prediction task is usually scarce. When the training data is insufficient, a single classifier is prone to overfitting, resulting in its prediction results being unable to accurately estimate the sample classification difficulty. For this reason, the present invention uses random forest (RF) as the base learner in the self-paced ensemble learning framework. The random forest uses the Bootstrap sampling technique to randomly sample from the training set and uses the sampled samples to train decision trees. In the process of constructing a decision tree, if each sample has T attributes, a specified number t << T is specified. When splitting at each node, only t attributes are randomly selected from these T attributes as candidate attributes, and then an optimal attribute is selected from these t candidate attributes based on the Gini index as the splitting attribute of this node. Each node in the decision tree construction process is split in the above manner, and no pruning is performed during the decision tree construction process. The above steps are repeatedly executed to construct a large number of decision trees to form a random forest classifier.
[0098] Step 5, perform SMOTE oversampling on the sampled minority-class data to obtain a balanced training subset.
[0099] In addition, to overcome the influence of the class imbalance phenomenon on the random forest algorithm, the present invention introduces a data oversampling method in the self-paced ensemble learning framework to balance the class distribution by resampling the data space. Specifically, the present invention uses the SMOTE synthetic minority over-sampling method. The sampling magnification is the ratio of the number of samples in the majority class to the number of samples in the minority class in the original dataset, that is The SMOTE method first calculates the Euclidean distance from each minority-class sample x min , to all minority-class samples to obtain its k nearest neighbors. Subsequently, for each minority-class sample x min , randomly select N samples from its k nearest neighbors, denoted as x min (1) , x min (2) , …, x min (N)For each nearest neighbor x min (i) (i = 1, 2, ..., N), synthesize new samples according to the following formula:
[0100] x new =x min +rand(0,1)*(x min (i) -x min )#(11)
[0101] The next step involves training the base classifiers on balanced training subsets sampled using SMOTE. This self-stepping ensemble learning framework, which incorporates oversampling, is known as the self-stepping oversampling ensemble learning framework.
[0102] Step 6: Repeat steps 3 to 5 above until the number of base classifiers reaches the preset value. Then, weight all the base classifiers according to their classifier weights to obtain an ensemble classifier as the software defect prediction model.
[0103] Step 7: Extract software metrics from the program module to be predicted, representing them as feature vectors of the same dimension as the training samples. Use the trained ensemble classifier to predict the defect tendency of the program module to be predicted. If the prediction probability is less than 0.5, it indicates that the model predicts that the program module has no defects; otherwise, it is predicted to have defects.
[0104] Static software defect prediction methods based on self-walking oversampling ensemble learning, such as... Figure 1 As shown, the algorithm flow is as follows:
[0105] Algorithm 1: A Static Software Defect Prediction Method Based on Self-Stepping Oversampling Ensemble Learning
[0106] Input: training set The base learning algorithm is f, the number of base learners is m, and the number of bins is k.
[0107] Output: Ensemble learning model function F(x).
[0108] Program Start
[0109] initialization:
[0110] 1. Normalize the training set sample data according to equation (1);
[0111] 2. from Obtain minority class samples from from Obtain majority class samples from the middle;
[0112] 3. Regarding and Perform Bootstrap sampling, sample size and and The sample sizes are equal;
[0113] 4. Obtain the oversampled training subset by applying the SMOTE method to the sampled dataset according to equation (11).
[0114] 5. Using the basis learning algorithm f in Train the base classifier f1;
[0115] 6. Using f1 To make a prediction, calculate the MCC value and classifier weight w1 according to equations (4) and (5); for i = 1, 2, ..., m do
[0116] 7. Describe the function of the current ensemble learning model.
[0117] 8. According to formula (2), The medium sample is divided into k bins: B 1,- B 2,- ,…,B k,- ;
[0118] 9. According to formula (2), The medium sample is divided into k bins: B 1,+ B 2,+ ,…,B k,+ ;
[0119] 10. Calculate according to formula (3) The average difficulty contribution h in the l-th bin l,- ;
[0120] 11. Calculate according to formula (3) The average difficulty contribution h in the l-th bin l,+ ;
[0121] 12. Update the self-synchronization factor α according to equation (6);
[0122] 13. Calculate according to formula (7) The sampling weight p of the l-th bin l,- ;
[0123] 14. Calculate according to formula (8) The sampling weight p of the l-th bin l,+ ;
[0124] 15. Yes Each bin is subjected to weighted Bootstrap sampling, and the sample size is determined by equation (9);
[0125] 16. Yes Each bin is subjected to weighted Bootstrap sampling, and the sample size is determined by equation (10);
[0126] 17. Perform SMOTE balancing on the sampled subset to obtain a new training subset.
[0127] 18. In the balanced training subset Train base classifier f i ;
[0128] 19. Using f i right To make a prediction, the MCC value and classifier weight w are calculated according to equations (4) and (5). i ;
[0129] end for
[0130] 20. Return the ensemble model function
[0131] Program ended
[0132] This embodiment uses a random forest algorithm (tree size 30) as the base learner, with 10 bins. A self-stepping oversampling ensemble learning framework is used to train the defect prediction model on the NASA defect prediction dataset, followed by testing. To verify the effectiveness of the proposed method, this embodiment trains and tests it on the NASA dataset with other commonly used methods (including three oversampling methods: SMOTE, SMOTEENN, and MAHAKIL, and one undersampling method: RUS; the machine learning algorithm used is a random forest algorithm with a tree size of 30). The performance comparison is shown in the appendix. Figure 2 As shown. (From the appendix) Figure 2 As can be seen, the prediction performance of the self-stepping oversampling ensemble learning method proposed in this invention is higher than that of the comparative method. The MCC values on the five defect prediction datasets are all higher than those of the comparative method. This indicates that the self-stepping oversampling ensemble learning method can effectively alleviate the class imbalance problem and reduce the risk of overfitting, thereby improving the performance of the defect prediction model.
[0133] The embodiments described above are merely illustrative of several implementation methods of this application, and while the descriptions are specific and detailed, they should not be construed as limiting the scope of this application. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of this application, and these modifications and improvements all fall within the protection scope of this application. Therefore, the protection scope of this application should be determined by the appended claims.
Claims
1. A static software defect prediction method based on self-stepping oversampling ensemble learning, characterized in that, Includes the following steps: Step 1: Collect software repository data for the current project, extract software metrics from the program modules in the software repository, including Halstead metric, McCabe metric, CK metric, MOOD metric, and code change metric, so that each program module is represented as a feature vector. Then, based on whether the program module has defects, it is marked as defective or non-defective, thereby constructing a defect prediction dataset. Step 2: Perform standardization preprocessing on the defect prediction dataset, and divide the training samples into majority class set and minority class set according to the label information of the dataset; Step 3: Use random forest as the base classifier in the ensemble learning framework, train the base classifier based on the majority class set and the minority class set, calculate the MCC value of the base classifier on the training set, and update the weights of the base classifier. Step 4: Binning is performed on the majority class set and the minority class set. The classification difficulty of the prediction results of the ensemble classifier for the corresponding bin training set samples is calculated, and then the auto-stepping factor α is determined. By dynamically adjusting the sampling weight of each bin, equal probability Bootstrap sampling is performed on the majority class set and the minority class set respectively, so as to specifically reduce the sampling probability of bins with too many samples. Step 5: Perform SMOTE oversampling on the sampled minority class data to obtain a balanced training subset; Step 6: Repeat steps 3 to 5 above until the number of base classifiers reaches the preset value. Then, weight all the base classifiers according to their classifier weights to obtain an ensemble classifier as the software defect prediction model. Step 7: Extract software metrics from the program module to be predicted, representing them as feature vectors of the same dimension as the training samples. Use the trained ensemble classifier to predict the defect tendency of the program module to be predicted. If the prediction probability is less than 0.5, it indicates that the model predicts that the program module has no defects; otherwise, it is predicted to have defects. in, Step 4: Binning is performed on the majority and minority class sets. The classification difficulty of the ensemble classifier's prediction results for the corresponding bin training set samples is calculated, and the auto-stepping factor α is determined. By dynamically adjusting the sampling weight of each bin, equal-probability bootstrap sampling is performed on the majority and minority class sets respectively, in order to specifically reduce the sampling probability of bins with an excessive number of samples. The specific method is as follows: For majority class collections and minority class set Based on the estimated difficulty of sample classification, the sample is divided into k bins, with the l-th bin being B. l Represented as: majority class set and minority class set The sub-boxes are respectively labeled B l,- and B l,+ ; Calculate the average contribution of binning to the classification difficulty of the samples: Where, x s Indicates box B l For any sample in y s It is sample x s The category label, F represents the classifier for x. s The predicted probability, It is a sample pair (x) s ,y s The classification difficulty estimate of |B) l |This is compartment B l The total number of samples, binned into majority and minority classes B l,- and B l,+ The average contribution is denoted as h. l,- and h l,+ ; The self-synchronization factor is defined as: Where m represents the total number of base classifiers in the ensemble learning model, and i represents the index of the base classifier currently being trained (1≤i≤m). The sampling weights of the l-th bin of the majority and minority class sets are as follows: Based on the above weights, the number of samples for each bin in the majority class set is defined as follows: Similarly, the number of samples per bin in the minority class set is defined as follows: in, This represents the total number of minority class samples in the training set. It is the total number of samples in the majority class.
2. The static software defect prediction method based on self-stepping oversampling ensemble learning according to claim 1, characterized in that, Step 1: Collect software repository data for the current project. Extract software metrics from the program modules in the software repository, including Halstead metric, McCabe metric, CK metric, MOOD metric, and code change metric. Represent each program module as a feature vector. Then, based on whether the program module has defects, mark it as defective or not defective, thereby constructing a defect prediction dataset for training the software defect prediction model. The specific method is as follows: For the current software development project, the granularity of defect prediction is set, including files, packages, classes, and functions. Software repository data is mined using version control systems, defect tracking systems, or emails from relevant developers. Metrics related to software defects are extracted, including Halstead metrics, McCabe metrics, CK metrics, MOOD metrics, and code change metrics. Each program module is represented as a feature vector represented by a set of software metrics. Based on whether the module has defects, it is divided into defective modules and defect-free modules, with defective modules denoted as 1 and defect-free modules denoted as 0. This constructs a software defect prediction dataset.
3. The static software defect prediction method based on self-stepping oversampling ensemble learning according to claim 1, characterized in that, Step 2: Perform standardization preprocessing on the defect prediction dataset. Based on the label information of the dataset, divide the training samples into a majority class set and a minority class set. The specific method is as follows: Step 2.1, standardization preprocessing, transforms the data of each dimension of the training and test samples into a distribution with a mean of 0 and a standard deviation of 1. Let μ(x) be the mean and standard deviation of the j-th feature of the sample. .j ) and σ(x .j Then we have: Step 2.2: Divide the training samples into a majority class set and a minority class set according to the label information of the dataset. The majority class samples are non-defect class samples, and the minority class samples are defect class samples.
4. The static software defect prediction method based on self-stepping oversampling ensemble learning according to claim 1, characterized in that, Step 3: Use Random Forest as the base classifier in the ensemble learning framework. Train the base classifier based on the majority class set and the minority class set, calculate the MCC value of the base classifier on the training set, and update the base classifier weights. The specific method is as follows: In the initial stage of ensemble learning, a purely random sampling method is used to sample samples from the majority class and minority class sets, respectively, with the same number of samples from the majority class and minority class in the original training set. These sampled training subsets are then used to train the base classifier f1. After the base classifier f1 is trained, the MCC value of the base classifier f1 on the training set is calculated to determine its weight w1. The formula for calculating the MCC is as follows: Wherein, TP, TN, FP, and FN represent the number of true positives, true negatives, false positives, and false negatives, respectively; When there is only one base classifier, its weight is 1. As the number of base classifiers gradually increases, the weight of the j-th base classifier is obtained by normalizing the MCC values of all base classifiers. Among them, f j This represents the j-th classifier that has been trained. That is, the prediction performance index of the classifier on the training set D.
5. The static software defect prediction method based on self-stepping oversampling ensemble learning according to claim 1, characterized in that, Step 5: Perform SMOTE oversampling on the sampled minority class data to obtain a balanced training subset. The specific method is as follows: The SMOTE oversampling factor is calculated as the ratio of the number of majority class samples to the number of minority class samples in the original dataset. For each minority class sample x min Calculate its Euclidean distance to all minority class samples to obtain its k nearest neighbors; For each minority class sample x min From its k nearest neighbors, N samples are randomly selected, denoted as x. min (1) ,x min (2) , ..., x min (N) ; For each nearest neighbor x min (i) (i = 1, 2, ..., N), synthesize new samples according to the following formula: x new =x min +rand(0,1)*(x min (i) -x min ) (11)。 6. A static software defect prediction system based on self-stepping oversampling ensemble learning, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, it implements the static software defect prediction method based on self-stepping oversampling ensemble learning as described in any one of claims 1-5, thereby realizing static software defect prediction based on self-stepping oversampling ensemble learning.
7. A computer device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, it implements the static software defect prediction method based on self-stepping oversampling ensemble learning as described in any one of claims 1-5, thereby realizing static software defect prediction based on self-stepping oversampling ensemble learning.
8. A computer-readable storage medium having a computer program stored thereon, wherein when the computer program is executed by a processor, it implements the static software defect prediction method based on self-stepping oversampling ensemble learning as described in any one of claims 1-5, thereby realizing static software defect prediction based on self-stepping oversampling ensemble learning.
Citation Information
Patent Citations
Software defect prediction method based on class imbalance learning algorithm
CN112465040A
Software defect prediction method and system based on information entropy oversampling
CN114490386A