# Cross-Project Defect Prediction Based on Domain Adaptation and LSTM Optimization
## 3 Methodology
### Proposed Features Selection Approach
The merit of a feature f in a dataset \(S\) can be calculated using the following Equation (1):

\[\textit{merit}(f,S)=\textit{Metric}(f,S), \tag{1}\]

where \(\textit{Metric}(f,S)\) is a feature selection correlation metric. The selected features from all source datasets are combined to create the _S_selected_ dataset. Then the CFS algorithm selects the features from the target dataset \(T\) that correspond to the features selected for the source dataset _S_[n]. The selected features from all target datasets are combined to create the _T_selected_ dataset. The _T_selected_ dataset is created as in Equation (2):

\[\textit{T\_selected}=\{f\mid f\ \in\ T\ \textit{and}\ f\ \in\ S\_selected\}, \tag{2}\]

where \(f\) is a feature that exists in both the target dataset \(T\) and the _S_selected_ dataset. To address class imbalance issues and remove noisy and irrelevant instances in the source and target datasets, the SMOTE-ENN (Synthetic Minority Over-sampling Technique--Edited Nearest Neighbors) technique is applied. SMOTE-ENN works by:

1. Generating synthetic samples of the minority class using the k-nearest neighbors' algorithm. 2. Removing any noisy or redundant samples from the resampled dataset using the Edited Nearest Neighbors (ENN) algorithm. The resampled datasets are denoted as _S_resampled_ and _T_resampled_. Then split the resampled source and target datasets into training and testing sets. The split is carried out using an 80:20 ratio, where 80% of the data are used for training, and 20% is used for testing. The training and testing sets are denoted as:

\[\textit{S\_x},\textit{S\_y}\ (\text{Source dataset})\]

\[\textit{T\_x},\textit{T\_y}\ (\text{Target dataset})\]

The algorithm builds a Sequential Neural Network model with the following layers:

1. Bi-LSTM (Bidirectional Long Short-Term Memory) with 220 nodes
2. Bi-GRU (Bidirectional Gated Recurrent Unit) with 220 nodes
3. LSTM with 220 nodes
4. Attention Layer

The Bi-LSTM and Bi-GRU layers are combined to optimize LSTM to capture the sequential and contextual information in the data. The attention layer is added to allow the model to focus on the most relevant parts of the input data when making predictions. Then, it trains the model using the _T_resampled_ dataset. The training process can be represented by the following Equation (3):

Figure 1:0

where model is the sequential neural network model built in the previous step, and _T_resampled_ is the resampled target dataset. Then, it uses the trained model to predict the defects in the _T_y_ (testing) dataset. The prediction can be represented by the following Equation (4):

Figure 1:1

where \(\textit{Result}\) is the predicted defects for the _T_y_ dataset. The algorithm returns the Result as the final output, which represents the predicted defects for the target dataset _T_y_. The Bi-directional Long Short-Term Memory (LSTM) and Gated Recurrent (GRU) Layer: The suggested model first leverages a bi-directional LSTM network to learn the contextual and semantic features. Due to its ability to ease the vanishing gradient problem and depict long-term dependency, this model has been selected [46]. Bi-LSTM is an expansion of LSTMm which comprises two LSTM layers, one processing the sequence in the forward direction and the other in the backward. The sequence is processed in reverse order by the backward LSTM layer, whereas the sequence is processed from start to finish by the forward LSTM layer. To create the final output, the hidden states from both layers are concatenated [47]. The bi-LSTM is defined by Equations (5)-(7), where \(\overset{\rightarrow}{h_{t}}\) is the state of the forward LSTM, \(\overset{\rightarrow}{h_{t}}\) is the state of the backward LSTM, and \(\oplus\) signifies the operation of concatenating two vectors. To generate the final hidden state \(h_{t}=[\overset{\rightarrow}{h_{t}},\overset{\leftarrow}{h_{t}}]\) at time step t, the forward layer's \(\overset{\rightarrow}{h_{t}}\) final output and the \(\overset{\leftarrow}{h_{t}}\) backward layer's reverse output are merged.