# Cross-Project Defect Prediction Based on Domain Adaptation and LSTM Optimization
## 3 Methodology
### Proposed Features Selection Approach
Data sampling is the most widely used balancing approach. While undersampling approaches can lower the number of instances from the majority class [42], oversampling strategies can boost the representation of the minority class [43]. Our proposed approach uses the Synthetic Minority Oversampling Technique (SMOTE) [44] with Edited Nearest Neighbor (ENN) [45], which combines the ability of Edited Nearest Neighbor to clean data and the ability of SMOTE to generate synthetic data in order to produce a dataset that is more representative and balanced. As seen in Figure 3, the SMOTE-ENN approach consists of two basic steps: oversampling the minority class using SMOTE and cleaning the generated dataset using Edited Nearest Neighbor. Figure 3: Flowchart of SMOTE-ENN. The pseudocode of SMOTE-ENN is demonstrated in Algorithm 2. The SMOTE algorithm first separates minority _m_i_ and majority _m_a_ samples based on \(y\) class labels and then determines the \(k\) closest neighbors from the same class for each instance in the minority class _m_i_. Then randomly choose one of the k nearest neighbors \(\hat{x}\) and compute the difference between the feature vectors of the instance and the selected neighbor \((\hat{x}-x_{i})\). Multiply this difference by a random value \(\delta\) between 0 and 1 and add it to the feature vector of the instance \(x_{i}\). This builds a synthetic instance \(x_{new}\). After oversampling the minority class _m_i_, the final dataset may still contain noisy and irrelevant instances. This is when the Edited Nearest Neighbor (ENN) algorithm comes into play. The ENN algorithm finds three nearest neighbors for each instance \(x_{j}\) and removes instances \(x_{j}\) that are misclassified by their nearest neighbors, hence improving the quality of the dataset. ```
Input: -  X: Feature (samples(n) X features[n]) for both majority and minority classes -  Y: Target variable (samples(n)) indicating class labels Output: -  X_resampled: Resampled feature matrix after applying SMOTE-ENN -  Y_resampled: Resampled target variable after applying SMOTE-ENN
1. Separate minority and majority class as _m_i_ and _m_a_
2. Identify _m_i_ and _m_a_ samples based on class labels in \(y\)
3. For each _m_i_ sample \(x_{i}\), calculate, \(k\)-nearest neighbors. 4. Select one of the random nearest neighbors \(\hat{x}\)
5. Generate a new synthetic sample \(x_{new}=x_{i}+\delta(\hat{x}-x_{i})\)_where \(\delta e[1]\)
6. Repeat the above steps until the desired balance ratio is achieved
7. For each instance \(x_{j}\) in the augmented dataset:
8. Find the three nearest neighbors of \(x_{j}\)
9. If \(x_{j}\) is misclassified by its three nearest neighbors, delete \(x_{j}\) Return Resampled Dataset
```

**Algorithm 2**Pseudocode of SMOTE Edited Nearest Neighbor (SMOTE-ENN)

### Model Building

Our learning model architecture comprises four layers, starting with the Bi-LSTM layer which consists of 220 nodes. The second and third layers are Bi-GRU and LSTM, having 220 nodes each. For acquiring important features, we used the attention layer. All dense layers utilize the tanh as their activation function. The activation function applied in the last layer is softmax. The term \"dropout\" describes the possibility that any given node will be eliminated or dropped out, as seen in Figure 4. Algorithm 3 illustrates our model's detail processing. In the proposed approach, for each source dataset \(S[n]\), the CFS algorithm:

1. [label=(0)]
2. Calculates the merit of each feature and selects the top features based on the highest merit. 3. [label=(0)]
4. Selects the top k features based on the highest merit and creates the _S_selected_ dataset. Figure 4: Architecture of the proposed model. The top k features are selected based on the highest merit values, and the _S_selected_ dataset is created by combining the selected features from all source datasets.