Static software defect prediction method based on self-paced oversampling ensemble learning
By introducing a self-step oversampling integrated learning method in static software defect prediction, combined with SMOTE oversampling and random forest algorithm, the problems of insufficient training data and category imbalance are solved, and the performance of the defect prediction model is significantly improved.
Patent Information
- Application Number
- CN202411618826.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-13
- Publication Date
- 2025-03-18
- Estimated Expiration
- 2044-11-13
AI Technical Summary
Static software defect prediction tasks face problems such as insufficient training data and category imbalance, which leads to lack of targeted model training process and serious overfitting, which affects defect prediction performance.
Using a method based on self-step oversampling ensemble learning, random forests are used as the base classifier in the integrated learning framework, combined with SMOTE oversampling strategy, the sampling weight of samples is dynamically adjusted, the data distribution of the training subset is balanced, and the generalization performance of the model is improved.
It effectively alleviates the problem of category imbalance, reduces the risk of overfitting, improves the performance of defect prediction models, and improves the prediction accuracy of software defects.
Smart Images

Figure CN119645803A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to a software defect prediction technology for software engineering applications, and in particular to a static software defect prediction method based on self-paced oversampling ensemble learning. Background Art
[0002] Static software defect prediction technology analyzes software code or development process, extracts metrics related to software defects, mines software history warehouse, creates defect prediction data set, and then uses machine learning technology to train prediction model on the data set to identify modules that may have defects. This technology can not only help the testing team to clarify the focus of work before testing, but also provide valuable feedback to the development team, guiding them to pay more attention to and review potential error-prone modules during the development process, thereby improving code quality and stability. In recent years, with the continuous evolution of machine learning theory and technology, static software defect prediction technology based on machine learning has also developed rapidly, and has gradually become an auxiliary tool that can significantly improve testing efficiency and effectiveness, providing strong support for defect management in the software development life cycle.
[0003] Static software defect prediction mainly predicts the defect tendency of software program modules, which is different from dynamic software defect prediction, which predicts the distribution of software defects over time. The general process of static software defect prediction includes: first, according to the actual application scenario, determine the prediction granularity of the program module (such as files, packages, classes or functions, etc.), and mark each collected program module as a defective module (marked as 1) or a defect-free module (marked as 0). Next, extract the metrics related to software defects (such as Halstead metric, McCabe metric, CK metric, MOOD metric, code change metric and other object-oriented metrics), and represent each program module as a feature vector. The feature vectors of all program modules and their defect annotations together constitute a defect prediction data set. On this basis, the data set is subjected to necessary data preprocessing (such as data standardization, data resampling, etc.), and then the defect prediction model is trained using a machine learning algorithm. The new program module can extract software metrics and predict whether it has software defects based on the trained model, which can assist testers in reasonably allocating test resources and improve test efficiency.
[0004] However, static software defect prediction tasks often face challenges such as insufficient training data and class imbalance, which leads to overfitting problems in the later stage of model training due to excessive focus on noise samples, and the prediction results are biased towards the majority class (i.e., non-defect class). Existing static software defect prediction methods often adopt a segmentation processing strategy to deal with class imbalance and model overfitting problems. For example, class imbalance learning methods such as data resampling are used to deal with the former, and ensemble learning is used to deal with the latter. This method fails to fully explore and utilize the data complexity information contained in the defect dataset. Due to the lack of estimation methods for sample classification difficulty, it is difficult for the ensemble learning algorithm to distinguish samples of different difficulty levels when training the base model, resulting in a lack of targeting in the learning process, which in turn affects the generalization performance of the ensemble learning model and the defect prediction effect. Summary of the invention
[0005] The purpose of the present invention is to overcome the problems faced by static software defect prediction tasks, such as insufficient training data and category imbalance, which lead to the lack of pertinence in the model training process, and the problem of overfitting in the later stage of model training due to excessive attention to noise samples and abnormal samples, which affects the defect prediction performance.
[0006] The technical solution to achieve the purpose of the present invention is: a static software defect prediction method based on self-paced oversampling ensemble learning, comprising the following steps:
[0007] Step 1: Collect the software warehouse data of the current project, extract software metrics for the program modules in the software warehouse, 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, according to whether the program module has defects, mark it as defective or non-defective, so as to construct a defect prediction data set;
[0008] Step 2: Perform standardized preprocessing on the defect prediction data set, and divide the training samples into majority class set and minority class set according to the label information of the data set;
[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 base classifier weight;
[0010] Step 4: Bin the majority class set and the minority class set, calculate the classification difficulty of the prediction results of the ensemble classifier for the corresponding bin training set samples, and then determine the self-paced factor α. By dynamically adjusting the sampling weight of each bin, Bootstrap sampling with equal probability 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 a preset value, and combine all base classifiers according to the classifier weights to obtain an integrated classifier as a software defect prediction model.
[0013] Step 7: Extract software metrics for the program module to be predicted and represent them as feature vectors of the same dimension as the training samples. Use the trained integrated classifier to predict the defect tendency of the program module to be predicted. If the prediction probability is lower than 0.5, it indicates that the model predicts that the program module does not have defects, otherwise it is predicted to have defects.
[0014] Furthermore, in step 1, the software warehouse data of the current project is collected, and software metrics are extracted from the program modules in the software warehouse, including Halstead metric, McCabe metric, CK metric, MOOD metric, code change metric, etc., so that each program module is represented as a feature vector, and then the program module is marked as defective or non-defective according to whether it has defects, so as to construct a defect prediction data set 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. With the help of version control systems, defect tracking systems or emails of relevant developers, software warehouse data is mined; 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, and is divided into defective modules and non-defective modules according to whether the module has defects or not. Defective modules are recorded as 1 and non-defective modules are recorded as 0, thereby constructing a software defect prediction dataset.
[0016] Furthermore, in step 2, the defect prediction data set is preprocessed by standardization, and the training samples are divided into a majority class set and a minority class set according to the label information of the data set. The specific method is as follows:
[0017] Step 2.1: Standardization preprocessing is used 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. Assume that the mean and standard deviation of the j-th dimension feature of the sample are denoted by μ(x .j ) and σ(x .j ), then:
[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 data set, where the majority class samples are non-defect class samples and the minority class samples are defect class samples.
[0020] Furthermore, in step 3, 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, and the base classifier weight is updated. The specific method is:
[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 as the majority class and minority class samples in the original training set, and these sampled training subsets are 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, where the calculation formula of MCC is as follows:
[0023]
[0024] Among them, TP, TN, FP, and FN represent the number of true positive examples, true negative examples, false positive examples, and false negative examples, respectively;
[0025] When there is only one base classifier, the weight of the base classifier is 1. As the number of base classifiers gradually increases, the weight of the jth base classifier is normalized by the MCC values of all base classifiers:
[0026]
[0027] Among them, f j represents the jth classifier that has been trained. That is, the classifier is in the training set The prediction performance index value on .
[0028] Furthermore, in step 4, the majority class set and the minority class set are binned, and the classification difficulty of the prediction results of the ensemble classifier for the corresponding binned training set samples is calculated, and then the self-paced factor α is determined. By dynamically adjusting the sampling weight of each bin, the majority class set and the minority class set are respectively subjected to equal probability Bootstrap sampling, so as to specifically reduce the sampling probability of bins with too many samples. The specific method is as follows:
[0029] For the majority class set and minority class set Divide into k bins according to the sample classification difficulty estimate, the lth bin B l It is expressed as:
[0030]
[0031] Majority Class Set and minority class set The bins are denoted as B l,- and B l,+ ;
[0032] Calculate the average contribution of sample classification difficulty in the bins:
[0033]
[0034] Among them, x s Indicates bin B l Any sample in y s is the sample x s The category label of x, F represents the classifier s The predicted probability of is a sample pair (x s ,y s ) classification difficulty estimate, |B l | is bin B l The total number of samples. The majority class and minority class are divided into B l,- and B l,+ The average contribution of l,- and h l,+ ;
[0035] The self-pacing factor is defined as:
[0036]
[0037] Among them, m represents the total number of base classifiers in the ensemble learning model, i represents the serial number of the base classifier currently being trained (1≤i≤m), and the sampling weights of the lth bin of the majority class 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 for each bin in the minority class set is defined as follows:
[0042]
[0043] in, represents the total number of minority class samples in the training set, is the total number of majority class samples.
[0044] Furthermore, in step 5, the sampled minority class data is subjected to SMOTE oversampling to obtain a balanced training subset. The specific method is as follows:
[0045] The sampling rate of SMOTE oversampling is calculated by taking the ratio of the number of majority class and minority class samples in the original data set, that is,
[0046] For each minority class sample x min , calculate its Euclidean distance to all minority class samples and obtain its k nearest neighbors;
[0047] 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) ;
[0048] For each 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-paced oversampling ensemble learning implements the static software defect prediction method based on self-paced oversampling ensemble learning to achieve static software defect prediction based on self-paced oversampling ensemble learning.
[0051] A computer device comprises a memory, a processor and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, the static software defect prediction method based on self-paced oversampling ensemble learning is implemented to realize static software defect prediction based on self-paced oversampling ensemble learning.
[0052] A computer-readable storage medium stores a computer program. When the computer program is executed by a processor, the static software defect prediction method based on self-paced oversampling ensemble learning is implemented to achieve static software defect prediction based on self-paced oversampling ensemble learning.
[0053] Compared with the prior art, the present invention has the following significant advantages: 1) A self-paced learning strategy is introduced into the ensemble learning framework to estimate the classification difficulty of all samples, which is then introduced into the base classifier training process as data complexity information, thereby proposing a self-paced ensemble learning framework. The classification difficulty of all samples is estimated during the ensemble learning process, and different learning weights are assigned according to their classification difficulty levels, so as to achieve targeted learning of limited training samples. 2) In order to alleviate the category imbalance problem faced by the defect prediction task, a SMOTE oversampling strategy is introduced on the basis of the self-paced ensemble learning framework. Each time the base classifier is trained, an independent SMOTE oversampling operation is performed to balance the data distribution of the training subset. Each data oversampling process is performed independently, ensuring the diversity of the base classifier. 3) The prediction performance of the base classifier is estimated, and the corresponding weight is assigned accordingly, and then weighted combination is performed to finally form a static software defect prediction method based on self-paced oversampling ensemble learning. BRIEF DESCRIPTION OF THE DRAWINGS
[0054] Figure 1 This is a flow chart of a static software defect prediction method based on self-paced oversampling ensemble learning.
[0055] Figure 2 This is a comparison chart of experimental results. DETAILED DESCRIPTION
[0056] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0057] Combined with Figure 1 , the self-paced oversampling ensemble learning framework proposed in the present invention is described in detail.
[0058] Step 1: Collect the software warehouse data of the current project, extract software metrics from the program modules in the software warehouse, 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, according to whether the program module has defects, mark it as defective or non-defective, so as to construct a defect prediction data set for training the software defect prediction model.
[0059] For the current software development project, the granularity of defect prediction is set (it can be files, packages, classes or functions, etc.), and the software warehouse data is mined with the help of version control systems (such as CVS, SVN or Git, etc.), defect tracking systems (such as Bugzilla, Mantis, Jira or Trac, etc.) or emails of relevant developers. By analyzing the relevant information or source code of the software development process, the metrics related to software defects are extracted, and each program module is represented as a feature vector represented by a set of software metrics. According to whether the module has defects or not, it is divided into defective modules (represented as 1) and non-defective modules (represented as 0), thereby constructing a software defect prediction data set. When predicting a new program module, the 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 data set used for model training is the training set, and the data set used for actual prediction or model performance evaluation is the test set.
[0060] The embodiment uses five software defect data sets of the National Aeronautics and Space Administration (NASA) of the United States as static software defect prediction data sets of this embodiment. The brief information of these data sets is shown in Table 1.
[0061] Table 1
[0062]
[0063] Among them, #M represents the number of metrics, #T represents the type of metrics 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 samples, and %D represents the percentage of defect samples. It can be seen that the above defect prediction datasets have a serious class imbalance problem, among which the PC3 dataset has the highest class imbalance.
[0064] In order to train and test the defect prediction model, this embodiment uses a 5×5-fold cross-validation method to divide each data set into a training set and a test set. Each data set is divided into 5 equal parts, and then 4 parts are selected as training sets and 1 part is selected as a test set. A different part is selected as a test set each time until each part has served as a test set once, completing a 5-fold cross-validation. The above process is repeated 5 times.
[0065] Step 2: Perform standardized preprocessing on the defect prediction data set, and divide the training samples into a majority class set and a minority class set according to the label information of the data set;
[0066] To unify the dimensions, we first need to perform standardization preprocessing on the training set and the test set, so that the data of each dimension of the training sample and the test sample are converted into a distribution with a mean of 0 and a standard deviation of 1. Assume that the mean and standard deviation of the j-th dimension feature of the training set sample are denoted as μ(x.j ) and σ(x .j ), then:
[0067]
[0068] Similarly, the test set samples will also use 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 be samples with defects or samples without defects. According to the sample labels, the training set All samples in 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 with different weights from different sets.
[0070] Step 3: 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 weight of the base classifier;
[0071] In the ensemble learning framework, given any sample x and an ensemble classifier F, the confidence probability that the ensemble classifier F predicts x as a positive sample (i.e., a defective sample) is obtained. Among them, f j (x) represents the j-th base classifier prediction function, w j is the weight of the base classifier, and m is the total number of base classifiers. The classification difficulty of sample (x, y) can be obtained by the function Estimation is performed, where y∈{0,1} is the true label of the sample. The sample classification difficulty estimation function takes the integrated classifier F as input, which can dynamically estimate the classification difficulty of the sample. The sample classification difficulty distribution contains rich information, including the characteristics of noise samples, boundary samples, and the matching degree between model capacity and task difficulty.
[0072] In the initial stage of ensemble learning, since there is no base classifier yet, a pure random sampling method is used to sample the same number of samples from the majority class and minority class sets as the majority class and minority class samples in the original training set, and use these sampled training subsets to train the base classifier f1.
[0073] After the base classifier f1 is trained, its prediction performance on the training set is calculated (using the MCC value) to determine its weight w1. The MCC value is a commonly used prediction performance indicator for class imbalance classification tasks. Take the confusion matrix in Table 2 as an example, where TP, TN, FP, and FN are all elements in the confusion matrix, representing true positive examples, true negative examples, false positive examples, and false negative examples, respectively:
[0074] Table 2
[0075]
[0076] The calculation formula of MCC is as follows:
[0077]
[0078] When there is only one base classifier, the weight of the base classifier is 1. As the number of base classifiers gradually increases, the weight of the jth base classifier can be normalized by the MCC values of all base classifiers:
[0079]
[0080] Among them, f j represents the jth classifier that has been trained. That is, the classifier is in the training set The prediction performance index value on .
[0081] Then the prediction result of the ensemble classifier Estimate the classification difficulty of the training set samples. The sample classification difficulty is estimated by subtracting the difference between 1 and the prediction probability of the ensemble model. The model prediction probability represents the model's confidence in predicting the category. The lower the confidence, the more difficult it is to classify the sample. The sample classification difficulty estimation directly depends on the prediction result of the current ensemble model for the sample, and can dynamically reflect the classification difficulty of the sample under the current ensemble model, thereby discovering samples with poor model classification performance, and performing targeted learning on difficult-to-classify samples when training the base classifier later.
[0082] Step 4: Bin the majority class set and the minority class set, calculate the classification difficulty of the prediction results of the ensemble classifier for the corresponding bin training set samples, and then determine the self-stepping factor α. By dynamically adjusting the sampling weight of each bin, Bootstrap sampling with equal probability 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-paced learning strategy refers to the present invention using the training set The majority class samples (generally non-defective samples) and minority class samples (generally defective samples) in the bin are divided into boxes, that is, for the majority class set and minority class set Divide into k bins according to the sample classification difficulty estimate, the lth bin B l It is expressed as:
[0084]
[0085] Majority Class Set and minority class set The bins are denoted as B l,- and B l,+ The average contribution of the sample classification difficulty of these bins can be calculated by the following formula:
[0086]
[0087] Among them, x s Indicates bin B l Any sample in y s is the sample x s The category label of x, F represents the classifier s The predicted probability of is a sample pair (x s ,y s ) classification difficulty estimate, |B l | is bin B l The total number of samples. The majority class and minority class are divided into B l,- and B l,+ The average contribution of l,- and h l,+ .
[0088] As the training process progresses, the integrated classifier will gradually fit the training set, which will rapidly increase the number of simple samples and lead to a continuous increase in the overall difficulty contribution of simple samples. To this end, the present invention introduces a self-paced factor α, which dynamically adjusts the sampling weight of each bin to specifically reduce the sampling probability of bins with too many samples. Specifically, the present invention defines the self-paced factor as:
[0089]
[0090] Where m represents the total number of base classifiers in the ensemble learning model, and i represents the serial number of the base classifier currently being trained (1≤i≤m). The sampling weights of the lth bin of the majority class 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 for each bin in the minority class set is defined as follows:
[0095]
[0096] in, 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 estimation 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 estimations. 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. Therefore, 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 trains decision trees using the sampled samples. 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, forming 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 oversampling ratio is taken as 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 , N samples are randomly selected from its k nearest neighbors, denoted as x min (1) , x min (2) , …, x min (N), for each 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] Then go to step 1, and the training data of the base classifier is the balanced training subset sampled by SMOTE. This self-paced ensemble learning framework combined with the oversampling method is called the self-paced oversampling ensemble learning framework
[0102] Step 6: Repeat steps 3 to 5 above until the number of base classifiers reaches a preset value, and combine all base classifiers according to the classifier weights to obtain an integrated classifier as a software defect prediction model.
[0103] Step 7: Extract software metrics for the program module to be predicted and represent them as feature vectors of the same dimension as the training samples. Use the trained integrated classifier to predict the defect tendency of the program module to be predicted. If the prediction probability is lower than 0.5, it indicates that the model predicts that the program module does not have defects, otherwise it is predicted to have defects.
[0104] Static software defect prediction method based on self-paced oversampling ensemble learning Figure 1 As shown, the algorithm flow is as follows:
[0105] Algorithm 1: Static software defect prediction method based on self-paced oversampling ensemble learning
[0106] Input: training set Base learning algorithm f, number of base learners m, number of bins k.
[0107] Output: Ensemble learning model function F(x).
[0108] Program Start
[0109] initialization:
[0110] 1. Normalize the training set sample data according to formula (1);
[0111] 2. from Get minority class samples from from Get the majority class samples from
[0112] 3. Yes and Bootstrap sampling is performed, and the number of samples is and The number of samples is equal;
[0113] 4. According to formula (11), use the SMOTE method to obtain the oversampled training subset for the sampled data set
[0114] 5. Use the base learning algorithm f in Train the base classifier f1;
[0115] 6. Use f1 to Make predictions and calculate the MCC value and classifier weight w1 according to equations (4) and (5); for i = 1, 2, ..., m do
[0116] 7. Remember the current ensemble learning model function
[0117] 8. According to formula (2), The samples are divided into k bins: B 1,- ,B 2,- ,…,B k,- ;
[0118] 9. According to formula (2), The samples are divided into k bins: B 1,+ ,B 2,+ ,…,B k,+ ;
[0119] 10. Calculate according to formula (3) The average difficulty contribution h in the lth bin l,- ;
[0120] 11. Calculate according to formula (3) The average difficulty contribution h in the lth bin l,+ ;
[0121] 12. Update the self-pacing factor α according to formula (6);
[0122] 13. Calculate according to formula (7) The sampling weight p of the lth bin l,- ;
[0123] 14. Calculate according to formula (8) The sampling weight p of the lth bin l,+ ;
[0124] 15. Yes Weighted Bootstrap sampling is performed on each bin of , and the number of samples is determined by formula (9);
[0125] 16. Yes Weighted Bootstrap sampling is performed on each bin of , and the number of samples is determined by formula (10);
[0126] 17. Perform SMOTE balancing on the sampled subset to obtain a new training subset
[0127] 18. In the balance training subset Train the base classifier f i ;
[0128] 19. Use f i right Make predictions and calculate the MCC value and classifier weight w according to equations (4) and (5) i ;
[0129] end for
[0130] 20.return integrated model function
[0131] End of program
[0132] This example uses the random forest algorithm (tree size is 30) as the base learner, takes the number of bins as 10, and uses the self-paced oversampling ensemble learning framework to train the defect prediction model on the NASA defect prediction data set, and then tests it. In order to verify the effectiveness of the method proposed in the present invention, the example compares this method with other commonly used methods (including 3 oversampling methods SMOTE, SMOTEENN, MAHAKIL and 1 undersampling method RUS, the machine learning algorithm uses the random forest algorithm, and the tree size is 30) for training and testing on the NASA data set, and the test performance is compared as shown in the attached figure. Figure 2 As shown. Figure 2 It can be seen that the prediction performance of the self-paced oversampling ensemble learning method proposed in the present invention is higher than that of the comparison method, and the MCC values on the five defect prediction data sets are higher than those of the comparison method, which shows that the self-paced 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 above-described embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the present application. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the attached claims.
Claims
1. A static software defect prediction method based on self-paced oversampling ensemble learning, characterized in that: The following steps are involved: Step 1: Collect the software warehouse data of the current project, extract software metrics for the program modules in the software warehouse, 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, according to whether the program module has defects, mark it as defective or non-defective, so as to construct a defect prediction data set; Step 2: Perform standardized preprocessing on the defect prediction data set, and divide the training samples into majority class set and minority class set according to the label information of the data set; 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 weight; Step 4: Bin the majority class set and the minority class set, calculate the classification difficulty of the prediction results of the ensemble classifier for the corresponding bin training set samples, and then determine the self-paced factor α. By dynamically adjusting the sampling weight of each bin, Bootstrap sampling with equal probability 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 a preset value, and combine all base classifiers according to the classifier weights to obtain an integrated classifier as a software defect prediction model. Step 7: Extract software metrics for the program module to be predicted and represent them as feature vectors of the same dimension as the training samples. Use the trained integrated classifier to predict the defect tendency of the program module to be predicted. If the prediction probability is lower than 0.5, it indicates that the model predicts that the program module does not have defects, otherwise it is predicted to have defects.
2. The static software defect prediction method based on self-paced oversampling ensemble learning according to claim 1 is characterized in that: Step 1: Collect the software warehouse data of the current project, extract software metrics from the program modules in the software warehouse, 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, according to whether the program module has defects, mark it as defective or non-defective, so as to construct a defect prediction data set 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. With the help of version control systems, defect tracking systems or emails of relevant developers, software warehouse data is mined; 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, and is divided into defective modules and non-defective modules according to whether the module has defects or not. Defective modules are recorded as 1 and non-defective modules are recorded as 0, thereby constructing a software defect prediction dataset.
3. The static software defect prediction method based on self-paced oversampling ensemble learning according to claim 1 is characterized in that: Step 2: Standardize and preprocess the defect prediction data set, and divide the training samples into majority class set and minority class set according to the label information of the data set. The specific method is as follows: Step 2.1: Standardization preprocessing is used 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. Assume that the mean and standard deviation of the j-th dimension feature of the sample are denoted by μ(x .j ) and σ(x .j ), then: Step 2.2, divide the training samples into a majority class set and a minority class set according to the label information of the data set, where 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-paced 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 weight. The specific method is: 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 as the majority class and minority class samples in the original training set, and these sampled training subsets are 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, where the calculation formula of MCC is as follows: Among them, TP, TN, FP, and FN represent the number of true positive examples, true negative examples, false positive examples, and false negative examples, respectively; When there is only one base classifier, the weight of the base classifier is 1. As the number of base classifiers gradually increases, the weight of the jth base classifier is normalized by the MCC values of all base classifiers: Among them, f j represents the jth classifier that has been trained. That is, the classifier is in the training set The prediction performance index value on .
5. The static software defect prediction method based on self-paced oversampling ensemble learning according to claim 1, characterized in that: Step 4: Bin the majority class set and the minority class set, calculate the classification difficulty of the prediction results of the ensemble classifier for the corresponding bin training set samples, and then determine the self-stepping factor α. By dynamically adjusting the sampling weight of each bin, the majority class set and the minority class set are respectively subjected to equal probability Bootstrap sampling to specifically reduce the sampling probability of bins with too many samples. The specific method is as follows: For the majority class set and minority class set Divide into k bins according to the sample classification difficulty estimate, the lth bin B l It is expressed as: Majority Class Set and minority class set The bins are denoted as B l,- and B l,+ ; Calculate the average contribution of sample classification difficulty in the bins: Among them, x s Indicates bin B l Any sample in y s is the sample x s The category label of x, F represents the classifier s The predicted probability of is a sample pair (x s ,y s ) classification difficulty estimate, |B l | is bin B l The total number of samples. The majority class and minority class are divided into B l,- and B l,+ The average contribution of l,- and h l,+ ; The self-pacing factor is defined as: Among them, m represents the total number of base classifiers in the ensemble learning model, i represents the serial number of the base classifier currently being trained (1≤i≤m), and the sampling weights of the lth bin of the majority class 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 for each bin in the minority class set is defined as follows: in, represents the total number of minority class samples in the training set, is the total number of majority class samples.
6. The static software defect prediction method based on self-paced 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 sampling rate of SMOTE oversampling is calculated by taking the ratio of the number of majority class and minority class samples in the original data set, that is, For each minority class sample x min , calculate its Euclidean distance to all minority class samples and obtain its k nearest neighbors; 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 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)。 7. A static software defect prediction system based on self-paced oversampling ensemble learning, characterized in that: Implement the static software defect prediction method based on self-paced oversampling ensemble learning as described in any one of claims 1-6 to realize static software defect prediction based on self-paced oversampling ensemble learning.
8. 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, the static software defect prediction method based on self-paced oversampling ensemble learning according to any one of claims 1 to 6 is implemented to realize static software defect prediction based on self-paced oversampling ensemble learning.
9. A computer-readable storage medium having a computer program stored thereon, wherein when the computer program is executed by a processor, the static software defect prediction method based on self-paced oversampling ensemble learning according to any one of claims 1 to 6 is implemented to realize static software defect prediction based on self-paced oversampling ensemble learning.
Citation Information
Patent Citations
SMOTE+Boosting algorithm based software defect tendency prediction method
CN105589806A
Software defect prediction method based on class imbalance learning algorithm
CN112465040A
Software defect prediction method and system based on information entropy oversampling
CN114490386A
Self-paced semi-supervised ensemble classifier training method and system for unbalanced data
CN116910660A
Cross-project software defect prediction method and system thereof
WO2021093140A1