写一个auc的测试代码
时间: 2024-06-10 13:06:54 浏览: 197
好的,下面是一个简单的AUC测试代码:
```python
import numpy as np
def evaluate_AUC(predictions, targets):
"""
Computes the Area Under Curve (AUC) score for binary classification.
Args:
predictions (np.array): predicted class probabilities, shape (num_examples,)
targets (np.array): true class labels, shape (num_examples,)
Returns:
float: AUC score
"""
# Sort examples by predicted probability
sorted_indices = np.argsort(-predictions)
sorted_predictions = predictions[sorted_indices]
sorted_targets = targets[sorted_indices]
# Compute true positive and false positive rates at different thresholds
num_positives = np.sum(sorted_targets == 1)
num_negatives = np.sum(sorted_targets == 0)
tp_rate = np.zeros(num_positives+1)
fp_rate = np.zeros(num_positives+1)
tp = 0
fp = 0
for i in range(len(sorted_targets)):
if sorted_targets[i] == 1:
tp += 1
else:
fp += 1
tp_rate[tp] = tp / num_positives
fp_rate[tp] = fp / num_negatives
# Compute AUC as the area under the ROC curve
auc = np.trapz(tp_rate, fp_rate)
return auc
```
这个代码实现了一个简单的AUC评估函数,输入模型对样本的预测概率值和目标标签,返回AUC得分。请注意,此代码并未经过完整测试,可能存在错误或不足之处。
阅读全文