Software defect prediction constructs classification models by mining software history repositories to predict potentially defective program modules within a project for the ultimate purpose of improving software quality. Among them, most software defect prediction methods are based on machine learning, and the performance of machine learning model prediction relies on the hypothesis that the training data has the same distribution as the test data. In practical applications, however, we often cannot guarantee the unknown test data will have the same distribution as the training data. To address the current problem of unstable prediction results in the field of software defect prediction, this paper introduces DWLR algorithm to defect prediction which jointly optimizes the Causal Regularizer and a weighted logistic regression model. Finally, the algorithm is validated on the PROMISE dataset, and the experimental results show that the algorithm effectively improves the F1 scores and achieves stable within-project prediction.