import numpy as np
from sklearn.metrics import roc_curve, auc, precision_recall_curve
import matplotlib.pyplot as plt
roc_data = []
pr_data = []
colors = plt.cm.rainbow(np.linspace(0, 1, 31))
file_name = np.array(['DH_HGTS', 'DH_HGS', 'DH_HIS', 'DH_HKS', 'DH_HTS', 'DH_HD',
'L_HGTS', 'L_HGS', 'L_HIS', 'L_HKS', 'L_HTS', 'L_HD',
'DTS_HGTS', 'DTS_HGS', 'DTS_HIS', 'DTS_HKS', 'DTS_HTS', 'DTS_HD',
'DISS_HGTS', 'DISS_HGS', 'DISS_HIS', 'DISS_HKS', 'DISS_HTS', 'DISS_HD',
'DIS_HGTS', 'DIS_HGS', 'DIS_HIS', 'DIS_HKS', 'DIS_HTS', 'DIS_HD',
'DP_HP'])
i = 0
for element in file_name:
i = i + 1
all_predictions = []
all_labels = []
print(element)
for j in range(1, 6):
file_name = f"正样本和负样本{element}_{j}.txt"
print(file_name)
data = np.loadtxt(file_name, dtype=float, delimiter=',')
positive_predictions = data[:852, 0]
positive_labels = data[:852, 1]
negative_predictions = data[852:, 0]
negative_labels = data[852:, 1]
all_predictions.extend([positive_predictions, negative_predictions])
all_labels.extend([positive_labels, negative_labels])
all_predictions = np.concatenate(all_predictions)
all_labels = np.concatenate(all_labels)
fpr, tpr, thresholds_roc = roc_curve(all_labels, all_predictions)
roc_auc = auc(fpr, tpr)
precision, recall, thresholds_pr = precision_recall_curve(all_labels, all_predictions)
pr_auc = auc(recall, precision)
roc_data.append((fpr, tpr, roc_auc, colors[i - 1], element))
pr_data.append((recall, precision, pr_auc, colors[i - 1], element))
```python
plt.figure()
for fpr, tpr, roc_auc, color, element in roc_data:
plt.plot(fpr, tpr, label=f'{element}_AUROC = {roc_auc:.4f}', color=color)
plt.plot([0, 1], [0, 1], 'k--')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver Operating Characteristic')
plt.legend(loc='lower right', prop={'size': 5})
plt.savefig('roc31_curve.png', dpi=300)
plt.figure()
for recall, precision, pr_auc, color, element in pr_data:
plt.plot(recall, precision, label=f'{element}_AUPR = {pr_auc:.4f}', color=color)
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.title('Precision-Recall Curve')
plt.legend(loc='lower right', prop={'size': 5})
plt.savefig('pr31_curve.png', dpi=300)