Abstract Although dissolved gas analysis (DGA) is an effective method for transformer fault diagnosis, problems with the quality and accuracy of DGA characterization datasets often arise in practical industrial applications and face difficulties in adjusting the parameters of fault diagnosis models. To address the above problems, this paper proposes a fault diagnosis model (MD-IQPSO-RF) based on Mahalanobis distance (MD) data cleaning and improved quantum particle swarm (IQPSO) optimization of random forest (RF) parameters. Specifically, the abnormal samples of the DGA dataset are first processed by MD to improve the quality and accuracy of the dataset. Then, the RF parameters were optimized using the IQPSO algorithm to adjust the model parameters in order to improve the diagnostic performance of the RF. Finally, the optimal parameters of RF are output, and the training data are used to train the RF algorithm to construct the MD-IQPSO-RF transformer fault diagnosis model. The experimental results show that the model achieves an average accuracy of 93.631% for fault diagnosis, which is 6.92% higher than the unoptimized RF model. Comparison with other similar methods also achieved good results, which further validated the effectiveness of the fault diagnosis model.