Unsupervised software defect prediction method based on interpretable clustering and related device

By employing a method of co-training variational autoencoders and decision trees, latent feature representations of software code modules are extracted, addressing the high cost and black-box issues of existing methods and achieving high-precision and interpretable defect prediction.

CN122196600APending Publication Date: 2026-06-12SHAANXI NORMAL UNIV

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
SHAANXI NORMAL UNIV
Filing Date
2026-03-04
Publication Date
2026-06-12

AI Technical Summary

Technical Problem

Existing unsupervised software defect prediction methods based on interpretable clustering rely on supervised learning, which suffers from high costs and limited prediction accuracy, or the black-box nature of deep learning methods makes them difficult to interpret.

Method used

A feature learner based on variational autoencoder is adopted. Through clustering consistency loss and decision tree co-training, the latent feature representation of code modules is extracted, and interpretable defect prediction rules are generated through clustering and decision tree.

Benefits of technology

It enables defect code module filtering without prior defect labels, reduces the dependence on labeled data, improves prediction accuracy and interpretability, and generates decision rules that are understandable and easy to transform into development suggestions.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122196600A_ABST
    Figure CN122196600A_ABST
Patent Text Reader

Abstract

The application belongs to the field of software analysis, and discloses an unsupervised software defect prediction method based on interpretable clustering and a related device, which comprises the following steps: obtaining a software to be predicted, and extracting metric elements of each code module in the software to be predicted; according to the metric elements of each code module, the latent feature representation of the metric elements is extracted through a pre-trained feature learner to obtain the latent feature representation of each code module; the latent feature representation of each code module is clustered to obtain two clustering clusters, and the clustering cluster with larger average value of feature values of all latent feature representations in the two clustering clusters is marked as a defect cluster, and the code module corresponding to each latent feature representation in the defect cluster is marked as defective. In the case of no labeled data, high-precision software defect prediction with high-quality interpretability can be realized, thereby helping to improve the practicality and reliability of the unsupervised software defect prediction result and assisting software optimization.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of software analysis and relates to an unsupervised software defect prediction method and related apparatus based on interpretable clustering. Background Technology

[0002] Unsupervised software defect prediction methods based on interpretable clustering utilize machine learning theories and algorithms to extract effective defect features from software and predict whether unknown software has defects based on the learned defect distribution patterns. This can identify high-risk code modules in the software early in the development or testing phase, helping to optimize the allocation of testing resources.

[0003] Currently, unsupervised software defect prediction methods based on interpretable clustering mainly rely on supervised learning methods. One type is based on traditional machine learning methods (such as decision trees and logistic regression). Although these methods have inherent interpretability, they usually require manual feature construction, which is costly and has limited prediction accuracy. The other type is based on deep learning methods (such as autoencoders, convolutional neural networks, and long short-term memory networks). Although they have improved accuracy, their black-box nature makes the decision-making process difficult to interpret, which is not conducive to defect analysis and targeted repair. Summary of the Invention

[0004] The purpose of this invention is to overcome the shortcomings of the prior art and provide an unsupervised software defect prediction method and related apparatus based on interpretable clustering.

[0005] To achieve the above objectives, the present invention employs the following technical solution: In a first aspect, this invention provides an unsupervised software defect prediction method based on interpretable clustering, comprising: acquiring the software to be predicted and extracting metrics of each code module in the software; extracting latent feature representations of the metrics of each code module using a pre-trained feature learner to obtain latent feature representations of each code module; clustering the latent feature representations of each code module to obtain two clusters, and marking the cluster with the larger average value of all latent feature representations in the two clusters as a defect cluster, and marking the code modules corresponding to each latent feature representation in the defect cluster as defective; wherein the feature learner is constructed based on a variational autoencoder; the loss function pre-trained by the feature learner includes a cluster consistency loss, which is the cross-entropy loss between the code module defect prediction distribution based on a decision tree and the code module defect soft assignment probability distribution based on clusters; wherein the decision tree is constructed based on the method of predicting code module defects using metrics and is co-trained with the feature learner.

[0006] Optionally, the metrics for each code module in the software to be predicted include code metrics and / or process metrics; wherein, code metrics include one or more of the following: cyclomatic complexity, number of lines of code, comment ratio, inheritance depth, class cohesion and class coupling; process metrics include one or more of the following: modification frequency, number of developers, commit size, number of code reviews and historical defect density.

[0007] Optionally, the extraction of metrics for each code module in the software to be predicted includes: extracting metrics for each code module in the software to be predicted, and converting each feature in the metrics into a distribution with a mean of 0 and a standard deviation of 1 using Z-score standardization.

[0008] Optionally, the step of clustering the latent feature representations of each code module to obtain two clusters includes: using the K-means clustering algorithm to cluster the latent feature representations of each code module to obtain two clusters; the feature learner includes an encoder and a decoder connected in sequence, the encoder includes an input layer, two fully connected layers and an output layer connected in sequence, and the decoder includes a latent layer, two fully connected layers and an output layer connected in sequence; all fully connected layers use the ReLU activation function.

[0009] Optionally, the loss function pre-trained by the feature learner for:

[0010]

[0011]

[0012]

[0013] in, To reconstruct the loss, For the metric element, For the reconstruction metric based on latent feature representation, For distributed KL divergence loss, Let be the probability distribution of the latent feature representations output by the feature learner. It follows a standard normal distribution. For cluster consistency loss, The cross-entropy function, For the defect prediction distribution of code modules based on decision trees, For the soft assignment probability distribution of code module defects based on clustering, and These are the weighting coefficients.

[0014] Optionally, the decision tree uses the Gini coefficient as the splitting criterion and generates decision rules by recursively splitting the feature space; the training process of the decision tree adopts a reverse hierarchical traversal strategy, and selects a splitting method that minimizes the mispredicted code module type for each internal node of the decision tree.

[0015] Optionally, it also includes: generating decision rules for each code module using a pre-trained decision tree based on the metrics of each code module; dividing the defective code modules into several risk clusters based on the decision rules of each code module, obtaining the key risk factors and key decision rules for each risk cluster, and generating improvement suggestions for each risk cluster.

[0016] In a second aspect, the present invention provides an unsupervised software defect prediction system based on interpretable clustering, comprising: a feature module for acquiring the software to be predicted and extracting metrics of each code module in the software; a learning module for extracting latent feature representations of the metrics of each code module using a pre-trained feature learner, thereby obtaining latent feature representations of each code module; and a prediction module for clustering the latent feature representations of each code module into two clusters, marking the cluster with the larger average value of all latent feature representations in the two clusters as a defect cluster, and marking the code modules corresponding to each latent feature representation in the defect cluster as defective; wherein the feature learner is constructed based on a variational autoencoder; the loss function pre-trained by the feature learner includes a cluster consistency loss, which is the cross-entropy loss between the code module defect prediction distribution based on a decision tree and the code module defect soft assignment probability distribution based on clusters; wherein the decision tree is constructed based on a method of predicting code module defects using metrics and is co-trained with the feature learner.

[0017] In a third aspect, the present invention provides a computer device including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor, when executing the computer program, implements the steps of the above-described unsupervised software defect prediction method based on interpretable clustering.

[0018] In a fourth aspect, the present invention provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the steps of the above-described unsupervised software defect prediction method based on interpretable clustering.

[0019] Compared with the prior art, the present invention has the following beneficial effects: This invention presents an unsupervised software defect prediction method based on interpretable clustering. Through a pre-trained feature learner built on a variational autoencoder, it effectively extracts deep latent feature representations of metrics from code modules in an unsupervised manner, overcoming the reliance on expert experience in traditional feature engineering and providing a more robust data foundation for subsequent analysis. Furthermore, by clustering latent feature representations and automatically identifying defect clusters, it achieves the goal of predicting defective code modules without prior defect labels, significantly reducing the method's dependence on labeled data and lowering the application threshold. Simultaneously, a clustering consistency loss is introduced into the loss function of the feature learner's pre-training, co-training and optimizing the interpretable decision tree with the feature learner and the clustering process. This design ensures that the defect predictions formed by clustering maintain a high degree of consistency with the explicit judgment logic based on the original features. Thus, while ensuring prediction accuracy, it yields understandable, traceable, and easily translatable decision rules into practical development suggestions, ultimately achieving a synergistic improvement in accuracy and interpretability. Attached Figure Description

[0020] Figure 1 This is a flowchart of an unsupervised software defect prediction method based on interpretable clustering, according to an embodiment of the present invention.

[0021] Figure 2 This is a decision rule diagram illustrating the interpretability of project derby-10.3.1.4 in an embodiment of the present invention.

[0022] Figure 3 This is a comparison chart of the AUC evaluation index between the method of the present invention and other methods in this embodiment of the invention.

[0023] Figure 4 This is a comparison chart of the G-means evaluation index between the method of the present invention and other methods in this embodiment of the invention.

[0024] Figure 5 This is a visual bar chart of the top 15 features of importance in embodiments of the present invention.

[0025] Figure 6 This is a heatmap showing the correlation distribution between the test items and the top 15 features of importance in this embodiment of the invention.

[0026] Figure 7 This is a block diagram of an unsupervised software defect prediction system based on interpretable clustering, according to an embodiment of the present invention. Detailed Implementation

[0027] To enable those skilled in the art to better understand the present invention, the technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings of the embodiments of the present invention. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort should fall within the scope of protection of the present invention.

[0028] It should be noted that the terms "first," "second," etc., in the specification, claims, and accompanying drawings of this invention are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of the invention described herein can be implemented in orders other than those illustrated or described herein. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover a non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.

[0029] The present invention will now be described in further detail with reference to the accompanying drawings: See Figure 1 In one embodiment of the present invention, an unsupervised software defect prediction method based on interpretable clustering is provided, specifically an unsupervised software defect prediction method based on interpretable clustering, which achieves a combined improvement in accuracy and interpretability.

[0030] Specifically, the unsupervised software defect prediction method based on interpretable clustering of this invention includes the following steps: S1: Obtain the software to be predicted and extract the metrics of each code module in the software to be predicted.

[0031] S2: Based on the metrics of each code module, extract the latent feature representations of the metrics through a pre-trained feature learner to obtain the latent feature representations of each code module.

[0032] S3: Cluster the latent feature representations of each code module to obtain two clusters, and mark the cluster with the larger average value of all latent feature representations in the two clusters as the defect cluster, and mark the code module corresponding to each latent feature representation in the defect cluster as defective.

[0033] The feature learner is constructed based on a variational autoencoder. The loss function pre-trained by the feature learner includes cluster consistency loss, which is the cross-entropy loss between the code module defect prediction distribution based on the decision tree and the soft assignment probability distribution of code module defects based on clusters. The decision tree is constructed based on the method of predicting code module defects using metrics and is co-trained with the feature learner.

[0034] This invention presents an unsupervised software defect prediction method based on interpretable clustering. Through a pre-trained feature learner built on a variational autoencoder, it effectively extracts deep latent feature representations of metrics from code modules in an unsupervised manner, overcoming the reliance on expert experience in traditional feature engineering and providing a more robust data foundation for subsequent analysis. Furthermore, by clustering latent feature representations and automatically identifying defect clusters, it achieves the goal of predicting defective code modules without prior defect labels, significantly reducing the method's dependence on labeled data and lowering the application threshold. Simultaneously, a clustering consistency loss is introduced into the loss function of the feature learner's pre-training, co-training and optimizing the interpretable decision tree with the feature learner and the clustering process. This design ensures that the defect predictions formed by clustering maintain a high degree of consistency with the explicit judgment logic based on the original features. Thus, while ensuring prediction accuracy, it yields understandable, traceable, and easily translatable decision rules into practical development suggestions, ultimately achieving a synergistic improvement in accuracy and interpretability.

[0035] In one possible implementation, the metrics for each code module in the software to be predicted include code metrics and / or process metrics; wherein, the code metrics include one or more of the following: cyclomatic complexity, number of lines of code, comment ratio, inheritance depth, class cohesion and class coupling; and the process metrics include one or more of the following: modification frequency, number of developers, commit size, number of code reviews and historical defect density.

[0036] Interpretive metrics (also known as features) are extracted from the source code of each code module in the software under test, such as code metrics and process metrics, to construct the original feature matrix. Among them, code metrics refer to metrics obtained from static code analysis, including but not limited to: cyclomatic complexity, number of lines of code, comment ratio, inheritance depth, class cohesion and class coupling, etc.; process metrics refer to metrics obtained from software development activity records, including but not limited to: modification frequency, number of developers, commit size, number of code reviews and historical defect density, etc.

[0037] In one possible implementation, the extraction of metrics for each code module in the software to be predicted includes: extracting metrics for each code module in the software to be predicted, and converting each feature value in the metrics into a distribution with a mean of 0 and a standard deviation of 1 using Z-score standardization.

[0038] Interpretively, the extracted metrics undergo data preprocessing. Z-score standardization is used to transform each feature of the metric into a distribution with a mean of 0 and a standard deviation of 1. The standardization formula is:

[0039] in, The original value, yes Standardized value The characteristic average value, The characteristic standard deviation is denoted as .

[0040] After Z-score processing, a standardized feature matrix is ​​obtained. :

[0041] in, For the number of code modules, For feature dimensions.

[0042] In one possible implementation, the step of clustering the latent feature representations of each code module to obtain two clusters includes: using the K-means clustering algorithm to cluster the latent feature representations of each code module to obtain two clusters; the feature learner includes an encoder and a decoder connected in sequence, the encoder includes an input layer, two fully connected layers and an output layer connected in sequence, and the decoder includes a latent layer, two fully connected layers and an output layer connected in sequence; all fully connected layers use the ReLU activation function.

[0043] The core of this invention's unsupervised software defect prediction method based on interpretable clustering is the construction and iterative updating of an interpretable clustering framework, including a feature learner, clustering operations, and an interpreter (decision tree). Specifically, the feature learner is a variational autoencoder, which comprises an encoder and a decoder; the encoder of the variational autoencoder processes the input... Dimensional features, i.e., metrics Mapped to the normal distribution parameters of the latent space and Latent feature representations are obtained through reparameterization techniques. The decoder of the variational autoencoder represents the latent features. Reconstructed into the original feature space, the reconstruction metric .

[0044] For example, the encoder structure of the feature learner is as follows: input layer ( Dimensional), fully connected layer ( Dimensional, ReLU activation), fully connected layer ( (Dimensional, ReLU activation) and output layer (mean) and variance each (dimensional), among which, As a potential spatial dimension, in this embodiment The decoder of the feature learner has a symmetric structure, consisting of: a latent layer ( Dimensional), fully connected layer ( Dimensional, ReLU activation), fully connected layer ( (Dimension, ReLU activation) and output layer ( (Dimension, Sigmoid activation).

[0045] In one possible implementation, the loss function pre-trained by the feature learner for:

[0046]

[0047]

[0048]

[0049] in, To reconstruct the loss, For the metric element, For the reconstruction metric based on latent feature representation, For distributed KL divergence loss, Let be the probability distribution of the latent feature representations output by the feature learner. It follows a standard normal distribution. For cluster consistency loss, The cross-entropy function, For the defect prediction distribution of code modules based on decision trees, For the soft assignment probability distribution of code module defects based on clustering, and These are the weighting coefficients.

[0050] In one possible implementation, the decision tree uses the Gini coefficient as the splitting criterion and generates decision rules by recursively splitting the feature space; the training process of the decision tree adopts a reverse hierarchical traversal strategy, and selects a splitting method that minimizes the type of mispredicted code module for each internal node of the decision tree.

[0051] For example, the training process of the feature learner and decision tree includes: Step 1: First, construct a feature learner based on a variational autoencoder. This feature learner can be pre-trained directly, or initial training can be performed using reconstruction loss and distribution KL divergence loss. In this implementation, during initial training, the Adam optimizer is used to update parameters, the learning rate is set to 0.001, and pre-training is performed for 200 epochs. The loss function for the initial training of the feature learner is... for:

[0052] in, Set to 0.01 to control the degree of decoupling of the latent feature representation.

[0053] Step 2: Extract latent feature representations for all code modules using the initially trained feature learner. Representation of latent features Initial clustering analysis was performed using the K-means clustering algorithm, resulting in... K There are 1 initial defect risk clusters, with 1 cluster number of clusters. K Setting it to 2 yields the initial pseudo-tags for each code module. .

[0054] Step 3: Based on the standardized feature matrix and the initial pseudo-tags of each code module A decision tree is constructed based on the method of predicting defects in code modules using metrics. .

[0055] Step 4: Alternately update the feature learner and decision tree (i.e., pre-train the feature learner), with a maximum of 10 iterations. Each iteration includes the following steps: Step 41: Update the feature learner. Apply the current decision tree to the standardized feature matrix. Predictions are made to obtain defect prediction labels for each code module based on decision trees. This generates a defect prediction distribution for code modules based on decision trees. Simultaneously, cluster consistency loss is introduced into the loss function of the feature learner. This loss measures the consistency between the latent feature representations learned by the feature learner and the defect prediction labels of the decision tree. The cluster consistency loss is obtained by calculating the cross-entropy loss between the decision tree-based code module defect prediction distribution and the cluster-based code module defect soft-assignment probability distribution. The specific formula is as follows:

[0056] in, For the first Each code module is based on one-hot encoding of defect prediction labels using decision trees. For the first The code module belongs to the cluster. The soft assignment probability, Number of code modules This represents the number of clusters.

[0057] Based on this, the loss function for feature learner pre-training for:

[0058] in, The weight parameter for cluster consistency loss is set to 0.1 in this implementation.

[0059] When updating the feature learner, based on the loss function Update the parameters of the feature learner using the Adam optimizer, with the learning rate set to 0.001, and train for 200 epochs.

[0060] Step 42: Extract new latent feature representations for all code modules using the updated feature learner. And on The K-means clustering algorithm was used to re-cluster the code modules, and the modules were then labeled to obtain new pseudo-labels for each module. When labeling clusters, based on the assumption that code modules with high defect risk often have higher metric values, the feature average labeling method is used to label the two generated clusters.

[0061] Specifically: (1) Based on clustering results ( and (Two clusters), calculate the comprehensive average of all code modules across all features within each cluster. and Specifically, for clusters... Calculate all the code modules it contains in all The average value of each feature dimension For clusters Similarly, calculate all the code modules it contains in all The average value of each feature dimension (2) Comparison and Size; (3) Label defect clusters: if Then the clusters will be Labeled as defect clusters, clusters If it is marked as a defect-free cluster, Then Marked as a defect cluster, (4) Based on this annotation result, assign a defect prediction label to each code module: code modules belonging to the defect cluster are predicted as defective code modules (label 1), and code modules belonging to the defect-free cluster are predicted as defect-free code modules (label 0). and It is a scalar value, not a vector. The calculation process involves merging all feature values ​​of all code modules in the cluster into a large numerical set, and then calculating its arithmetic mean.

[0062] Step 43: Update the decision tree. Based on the standardized feature matrix. and new pseudo-tags Update the decision tree The update process employs a reverse hierarchical traversal strategy. For each internal node of the decision tree, different splitting features and splitting values ​​are tried, and the splitting method that minimizes the number of misclassified samples is selected. This allows for the extraction of interpretable decision rules, enabling the generated decision rules to more accurately distinguish the cluster structure formed by the current latent feature representations, thus improving interpretability. For the internal nodes of the decision tree... The optimization objective is:

[0063] in, It is a characteristic of splitting. For the split value, For nodes Subtree rooted for samples The prediction This is an indicator function.

[0064] Finally, the optimized decision tree is pruned to remove empty and redundant nodes.

[0065] Step 44: Repeat steps 41, 42 and 43 above until the structure of the decision tree no longer changes or the maximum number of iterations is reached, thus achieving convergence and completing the training of the feature learner and the decision tree.

[0066] Interpretively, once the iterative process converges, the framework reaches a stable state. During inference, the final optimized feature learner can be used to extract the final latent feature representations of each code module. and to Clustering is performed to obtain the final cluster division of the code modules. Then, the final clustering results are labeled with defects according to the feature average labeling method to confirm which cluster is the defect cluster. At this point, all code modules have been assigned a final predicted label of either defective or non-defective.

[0067] In one possible implementation, the unsupervised software defect prediction method based on interpretable clustering further includes: generating decision rules for each code module using a pre-trained decision tree based on the metrics of each code module; dividing the defective code modules into several risk clusters based on the decision rules of each code module; obtaining the key risk factors and key decision rules of each risk cluster; and generating improvement suggestions for each risk cluster.

[0068] Interpretive, based on the metrics of each code module, from the final optimized decision tree Extract interpretable decision rules. Each decision rule corresponds to a path from the root node to a leaf node in the decision tree, in the form of: "IF condition 1 AND condition 2 AND … THEN cluster identifier". Here, the condition is a feature comparison condition, in the form of: "feature name comparison operator threshold". For example, "IF cyclomatic complexity > 15 AND modification frequency > 5 times / month THEN defective".

[0069] Interpretively, based on the decision rules of each code module, defective code modules are divided into several risk clusters, and the key risk factors and key decision rules of each risk cluster are obtained. Key risk factors are the typical characteristic value range of the risk cluster, i.e., the main characteristics and their values ​​affecting risk assessment; key decision rules are generally sub-decision rules included in the decision rules of most code modules within the risk cluster. Furthermore, improvement suggestions can be adaptively designed for each risk cluster based on its key risk factors and key decision rules.

[0070] For example, an explanatory report on the software to be predicted can also be generated, which can be presented in tabular and textual form, and generally includes the following: 1) Risk cluster identifiers (e.g., defective or non-defective) and the number and proportion of code modules they contain; 2) Key risk factors, i.e., the main characteristics that affect the risk determination and their values; 3) Key decision rules for determining each risk cluster in the complete decision rules from the root node to the leaf node; 4) Specific improvement suggestions for different risk clusters, such as: for defective clusters, code refactoring and increasing test coverage are recommended, while for non-defective clusters, maintaining the current coding practices is recommended.

[0071] In one possible implementation, to fully demonstrate the feasibility of the unsupervised software defect prediction method based on interpretable clustering of the present invention, verification experiments were conducted. Specifically, the experimental environment configuration included: Windows 10 system, Python 3.9.25, and Matplotlib visualization library; the data used was JIRA, the most commonly used public dataset in the field of software defect prediction, and the specific dataset information is shown in Table 1 below. Table 1

[0072] The specific implementation process includes: Input: The project to be tested. Output: Defect prediction labels (defective / no defect) for each code module of the project to be tested, a complete explanatory report, and visualization results.

[0073] The first step is to perform Z-score standardization on the test item to obtain standardized test item data.

[0074] The second step is to construct a feature learner based on a variational autoencoder. The encoder structure of the feature learner is set to a three-layer fully connected network, and the decoder is a symmetric structure. The decision tree is initialized using default parameters, and the clustering consistency loss weights are set. The KL divergence tradeoff parameter of the feature learner =0.01, the maximum number of iterations is 10, and the random seed is 42 to ensure that the results are reproducible.

[0075] The third step is to first train the VAE using reconstruction loss and distribution KL divergence loss for 200 epochs, and then use the Adam optimizer to set the learning rate to 0.001.

[0076] The fourth step involves collaboratively updating and training the feature learner, clustering process, and decision tree, repeating the following steps until the maximum number of iterations is reached: 1. Update the feature learner: Calculate the predicted label of the current decision tree for the standardized test item data. Add cluster consistency loss to the loss function of the feature learner. (Measures the consistency between the latent feature representation of the feature learner and the prediction of the decision tree), forming the total loss. 1. Update the parameters of the feature learner accordingly. 2. Update the clustering process and perform cluster labeling: Fix the updated feature learner and extract standardized new latent feature representations for the test items. .right K-means clustering (K=2) is performed to obtain new cluster partitions. The feature mean labeling method is used to calculate the comprehensive arithmetic mean of the features for each cluster. The cluster with the higher mean is labeled as a defective cluster, and the other is labeled as a non-defective cluster, thus generating new pseudo-labels for the standardized test items. 3. Update the decision tree: Retrain and update the decision tree, using reverse level-order traversal and pruning strategies to ensure that the rules are simple and have strong generalization ability; 4. Repeat the above three sub-steps until convergence.

[0077] Step 5: Generate the final defect prediction and use the final feature learner to extract the standardized final latent feature representation of the item to be tested. The system then performs final K-means clustering (K=2) on the data. Using the feature mean labeling method from step four, the final defect clusters and non-defect clusters are determined, thereby assigning defect prediction labels to each code module of the standardized test item.

[0078] Step 6: Extract the path from the root node to each leaf node from the final optimal decision tree, convert each path into an interpretable decision rule in the form of "IF-condition-AND-condition-THEN-conclusion", and match the decision rules of the code module with the annotation results of the cluster to which it belongs.

[0079] Step 7: Generate an explanatory report and visualization results for the project under test.

[0080] The method of this invention generates a clear and interpretable software defect decision rule diagram through a decision tree. See [link / reference]. Figure 2 The diagram illustrates the decision rule graph for one of the datasets, derby10.3.1.4, visually representing the correlation between code features and defective modules. The graph initially partitions the code metric CountineCodeDec (≤0.186) as the root node. Then, it refines the branch conditions layer by layer using code metrics familiar to developers, such as AvgLineCode and MAJOR_COMMIT (e.g., average lines of code and major commit frequency), ultimately outputting a "Defective" or "Non-defective" judgment. These branching rules not only align with the actual measurement dimensions of the code but also possess strong interpretability: for example, when CountineCodeDec≤0.186 and AvgLineCode≤2.002, SumCyclomaticStrict≤10.32 but MaxNesting_Min>0.296, the corresponding code module is judged as defective; while when CountineCodeDec>0.186 and MAJOR_COMMIT≤-0.02, CountDeclInstanceVariable≤0.004, the code module is judged as defect-free. This explicit path of "feature threshold - judgment result" not only verifies the effectiveness of the interpreter in the interpretable clustering framework of this invention but also allows developers to quickly locate high-risk code modules directly through code metrics, solving the problems of black-box nature and difficulty in practical application of traditional unsupervised defect prediction methods.

[0081] Under the same experimental environment, the performance of the unsupervised software defect prediction method (INTC) based on interpretable clustering of this invention was compared with various supervised and unsupervised benchmark methods. (See attached text.) Figure 3 Based on the AUC comparison results, the INTC method proposed in this invention exhibits significant performance advantages. Figure 3 The solid red line represents the median, and the dashed blue line represents the mean. AUC, as a core metric for measuring the overall performance of a classification task, directly reflects the model's ability to distinguish between defective and non-defective code modules. Figure 3 As can be seen, the median and mean AUC of the INTC method are not only significantly higher than those of traditional unsupervised methods, but also superior to most supervised learning methods (such as DT and GBM). This result confirms the effectiveness of the core design in this invention: the feature learner constructed through the variational autoencoder can learn low-dimensional, decoupled, high-quality feature representations, and combined with the joint iterative training of the clustering process and decision tree, the model obtains a more accurate clustering capability, thereby surpassing existing methods in overall classification performance.

[0082] See Figure 4 This paper presents a comparison of the G-mean evaluation index of the INTC method of this invention with other supervised and unsupervised benchmark methods. Figure 4 The solid red line represents the median, and the dashed blue line represents the mean, further highlighting the practical value of this invention in real-world applications. In software defect prediction scenarios, defective code modules often account for a very small percentage, representing a typical imbalanced data problem. The G-mean metric accurately reflects the model's ability to identify the minority class (defective code modules). Comparative data shows that the G-mean performance of the INTC method far surpasses traditional unsupervised methods and still holds a significant advantage over mainstream supervised methods such as XGBoost. This is attributed to the joint optimization framework of "feature learning-clustering-interpretation" in this invention, as well as the reasonable design of defect and non-defect labeling based on the average feature value within each cluster. This ensures the discriminative power of feature representations for defective code modules and strengthens the rationality of cluster partitioning through the rule constraints of decision trees. This allows the model to stably capture the characteristic patterns of defective code modules even in imbalanced data scenarios, fully validating the reliability and superiority of this unsupervised method in practical software defect prediction tasks.

[0083] Furthermore, regarding the visualization of feature importance, this invention also identified 15 features that were most important in cross-project defect prediction through comprehensive analysis of data from 28 projects. See also Figure 5The chart shows a visual bar graph of the top 15 features by importance. The features, from top to bottom, are: number of lines of executed code, total number of lines, number of statements, sum of strict cyclomatic complexity, maximum strict cyclomatic complexity, sum of corrected cyclomatic complexity, average number of lines of code, maximum cyclomatic complexity, average number of paths, sum of cyclomatic complexities, number of major commits, maximum corrected cyclomatic complexity, number of declaration statements, maximum number of outputs, and number of executable code. It can be seen that these 15 features are arranged from highest to lowest average importance. The top three features show high importance values ​​in all projects, indicating that these features have good universality in software defect prediction. Further analysis of feature frequency reveals that the top 5 features appear in over 90% of projects, the middle 5 features appear in 60%–90% of projects, and the bottom 5 features appear in 40%–60% of projects. This reflects that the applicability of different features varies across different types of projects.

[0084] See Figure 6 The heatmap shows the correlation distribution between each project and the top 15 most important features. A comparison reveals that the top features exhibit strong color depth in the vast majority of projects, indicating that these features do indeed have high predictive value across different projects. Simultaneously, the heatmap also shows that some projects display unique patterns on specific features, which may be due to project-specific development characteristics or defect patterns. Normalization eliminates the differences in feature importance value ranges between different projects, making cross-project comparisons more intuitive and verifying the stability and reliability of the method presented in this invention.

[0085] The following are embodiments of the apparatus of the present invention, which can be used to execute embodiments of the method of the present invention. For details not disclosed in the apparatus embodiments, please refer to the embodiments of the method of the present invention.

[0086] See Figure 7 In another embodiment of the present invention, an unsupervised software defect prediction system based on interpretable clustering is provided, which can be used to implement the above-mentioned unsupervised software defect prediction method based on interpretable clustering. Specifically, the unsupervised software defect prediction system based on interpretable clustering includes a feature module, a learning module, and a prediction module.

[0087] The system comprises the following modules: a feature module for acquiring the software to be predicted and extracting metrics from each code module; a learning module for extracting latent feature representations of each code module using a pre-trained feature learner; and a prediction module for clustering the latent feature representations of each code module into two clusters, marking the cluster with the larger average feature value of all latent feature representations in the two clusters as the defect cluster, and marking the code modules corresponding to each latent feature representation in the defect cluster as defective. The feature learner is constructed based on a variational autoencoder. The pre-trained loss function of the feature learner includes cluster consistency loss, which is the cross-entropy loss between the decision tree-based code module defect prediction distribution and the cluster-based soft-assignment probability distribution of code module defects. The decision tree is constructed based on the method of predicting code module defects using metrics and is co-trained with the feature learner.

[0088] In one possible implementation, the metrics for each code module in the software to be predicted include code metrics and / or process metrics; wherein, the code metrics include one or more of the following: cyclomatic complexity, number of lines of code, comment ratio, inheritance depth, class cohesion and class coupling; and the process metrics include one or more of the following: modification frequency, number of developers, commit size, number of code reviews and historical defect density.

[0089] In one possible implementation, the extraction of metrics for each code module in the software to be predicted includes: extracting metrics for each code module in the software to be predicted, and converting each feature in the metrics into a distribution with a mean of 0 and a standard deviation of 1 using Z-score normalization.

[0090] In one possible implementation, the step of clustering the latent feature representations of each code module to obtain two clusters includes: using the K-means clustering algorithm to cluster the latent feature representations of each code module to obtain two clusters; the feature learner includes an encoder and a decoder connected in sequence, the encoder includes an input layer, two fully connected layers and an output layer connected in sequence, and the decoder includes a latent layer, two fully connected layers and an output layer connected in sequence; all fully connected layers use the ReLU activation function.

[0091] In one possible implementation, the loss function pre-trained by the feature learner for:

[0092]

[0093]

[0094]

[0095] in, To reconstruct the loss, For the metric element, For the reconstruction metric based on latent feature representation, For distributed KL divergence loss, Let be the probability distribution of the latent feature representations output by the feature learner. It follows a standard normal distribution. For cluster consistency loss, The cross-entropy function, For the defect prediction distribution of code modules based on decision trees, For the soft assignment probability distribution of code module defects based on clustering, and These are the weighting coefficients.

[0096] In one possible implementation, the decision tree uses the Gini coefficient as the splitting criterion and generates decision rules by recursively splitting the feature space; the training process of the decision tree adopts a reverse hierarchical traversal strategy, and selects a splitting method that minimizes the type of mispredicted code module for each internal node of the decision tree.

[0097] In one possible implementation, the unsupervised software defect prediction system based on interpretable clustering further includes an interpretation module, configured to: generate decision rules for each code module using a pre-trained decision tree based on the metrics of each code module; divide the defective code modules into several risk clusters based on the decision rules of each code module; obtain the key risk factors and key decision rules for each risk cluster; and generate improvement suggestions for each risk cluster.

[0098] All relevant content of each step involved in the aforementioned embodiments of the unsupervised software defect prediction method based on interpretable clustering can be referenced to the functional description of the corresponding functional module of the unsupervised software defect prediction system based on interpretable clustering in the embodiments of the present invention, and will not be repeated here.

[0099] The module division in this embodiment of the invention is illustrative and represents only one logical functional division. In actual implementation, other division methods may be used. Furthermore, the functional modules in the various embodiments of the invention can be integrated into a single processor, exist as separate physical entities, or be integrated into a single module. The integrated modules described above can be implemented in hardware or as software functional modules.

[0100] In another embodiment of the present invention, a computer device is provided, comprising a processor and a memory. The memory stores a computer program, which includes program instructions. The processor executes the program instructions stored in the computer storage medium. The processor may be a Central Processing Unit (CPU), or other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. It is the computing and control core of the terminal, suitable for implementing one or more instructions, specifically suitable for loading and executing one or more instructions in the computer storage medium to achieve a corresponding method flow or corresponding function. The processor described in this embodiment of the present invention can be used for the operation of an unsupervised software defect prediction method based on interpretable clustering.

[0101] In another embodiment of the present invention, a storage medium is provided, specifically a computer-readable storage medium (Memory), which is a memory device in a computer device used to store programs and data. It is understood that the computer-readable storage medium here can include both the built-in storage medium in the computer device and extended storage media supported by the computer device. The computer-readable storage medium provides storage space that stores the terminal's operating system. Furthermore, the storage space also stores one or more instructions suitable for loading and execution by a processor. These instructions can be one or more computer programs (including program code). It should be noted that the computer-readable storage medium here can be high-speed RAM or non-volatile memory, such as at least one disk storage device. The processor can load and execute one or more instructions stored in the computer-readable storage medium to implement the corresponding steps of the unsupervised software defect prediction method based on interpretable clustering in the above embodiments.

[0102] Those skilled in the art will understand that embodiments of the present invention can be provided as methods, systems, or computer program products. Therefore, the present invention can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, the present invention can take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.

[0103] This invention is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the invention. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart illustrations and / or block diagrams. Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.

[0104] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.

[0105] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.

[0106] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention and not to limit it. Although the present invention has been described in detail with reference to the above embodiments, those skilled in the art should understand that modifications or equivalent substitutions can still be made to the specific implementation of the present invention. Any modifications or equivalent substitutions that do not depart from the spirit and scope of the present invention should be covered within the scope of protection of the claims of the present invention.

Claims

1. An unsupervised software defect prediction method based on interpretable clustering, characterized in that, include: Obtain the software to be predicted and extract the metrics of each code module in the software to be predicted; Based on the metrics of each code module, the latent feature representations of the metrics are extracted through a pre-trained feature learner to obtain the latent feature representations of each code module. The latent feature representations of each code module are clustered to obtain two clusters. The cluster with the larger average value of the feature values ​​of all latent feature representations in the two clusters is marked as the defect cluster. The code modules corresponding to each latent feature representation in the defect cluster are marked as defective. The feature learner is constructed based on a variational autoencoder. The loss function pre-trained by the feature learner includes cluster consistency loss, which is the cross-entropy loss between the code module defect prediction distribution based on the decision tree and the soft assignment probability distribution of code module defects based on clusters. The decision tree is constructed based on the method of predicting code module defects using metrics and is co-trained with the feature learner.

2. The unsupervised software defect prediction method based on interpretable clustering according to claim 1, characterized in that, The metrics for each code module in the software to be predicted include code metrics and / or process metrics; The code metrics include one or more of the following: cyclomatic complexity, lines of code, comment ratio, inheritance depth, class cohesion, and class coupling; the process metrics include one or more of the following: modification frequency, number of developers, commit size, number of code reviews, and historical defect density.

3. The unsupervised software defect prediction method based on interpretable clustering according to claim 1, characterized in that, The metrics extracted from each code module in the software to be predicted include: Extract the metrics of each code module in the software to be predicted, and transform each feature in the metrics into a distribution with a mean of 0 and a standard deviation of 1 using Z-score standardization.

4. The unsupervised software defect prediction method based on interpretable clustering according to claim 1, characterized in that, The step of clustering the latent feature representations of each code module to obtain two clusters includes: using the K-means clustering algorithm to cluster the latent feature representations of each code module to obtain two clusters; The feature learner includes an encoder and a decoder connected in sequence. The encoder includes an input layer, two fully connected layers and an output layer connected in sequence. The decoder includes a latent layer, two fully connected layers and an output layer connected in sequence. All fully connected layers use the ReLU activation function.

5. The unsupervised software defect prediction method based on interpretable clustering according to claim 1, characterized in that, The loss function of the feature learner pre-training for: in, To reconstruct the loss, For the metric element, For the reconstruction metric based on latent feature representation, For distributed KL divergence loss, Let be the probability distribution of the latent feature representations output by the feature learner. It follows a standard normal distribution. For cluster consistency loss, The cross-entropy function, For the defect prediction distribution of code modules based on decision trees, For the soft assignment probability distribution of code module defects based on clustering, and These are the weighting coefficients.

6. The unsupervised software defect prediction method based on interpretable clustering according to claim 1, characterized in that, The decision tree uses the Gini coefficient as the splitting criterion and generates decision rules by recursively splitting the feature space. The training process of the decision tree adopts a reverse hierarchical traversal strategy, and selects a splitting method that minimizes the mispredicted code module type for each internal node of the decision tree.

7. The unsupervised software defect prediction method based on interpretable clustering according to claim 1, characterized in that, Also includes: Based on the metrics of each code module, decision rules for each code module are generated using a pre-trained decision tree. Based on the decision rules of each code module, the defective code modules are divided into several risk clusters, and the key risk factors and key decision rules of each risk cluster are obtained, as well as improvement suggestions for each risk cluster are generated.

8. An unsupervised software defect prediction system based on interpretable clustering, characterized in that, include: The feature module is used to acquire the software to be predicted and extract the metrics of each code module in the software to be predicted. The learning module is used to extract the latent feature representations of the metrics of each code module through a pre-trained feature learner, thereby obtaining the latent feature representations of each code module. The prediction module is used to cluster the latent feature representations of each code module to obtain two clusters, and to mark the cluster with the larger average value of the feature values ​​of all latent feature representations in the two clusters as the defect cluster, and to mark the code module corresponding to each latent feature representation in the defect cluster as defective; The feature learner is constructed based on a variational autoencoder. The loss function pre-trained by the feature learner includes cluster consistency loss, which is the cross-entropy loss between the code module defect prediction distribution based on the decision tree and the soft assignment probability distribution of code module defects based on clusters. The decision tree is constructed based on the method of predicting code module defects using metrics and is co-trained with the feature learner.

9. A computer device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the computer program, it implements the steps of the unsupervised software defect prediction method based on interpretable clustering as described in any one of claims 1 to 7.

10. A computer-readable storage medium storing a computer program, characterized in that, When the computer program is executed by a processor, it implements the steps of the unsupervised software defect prediction method based on interpretable clustering as described in any one of claims 1 to 7.