gpt4 book ai didi

python - 为什么我会收到 best_ precision_threshold 的 IndexError ?

转载 作者:行者123 更新时间:2023-12-02 09:58:38 24 4
gpt4 key购买 nike

我在另一台计算机上运行了以下代码,没有出现任何问题,但是,当我尝试在另一台计算机上运行它时,我遇到了以下错误:

class_names = ['Fish', 'Flower', 'Sugar', 'Gravel']

def get_threshold_for_recall(y_true, y_pred, class_i, recall_threshold=0.94, precision_threshold=0.90, plot=False):

precision, recall, thresholds = precision_recall_curve(y_true[:, class_i], y_pred[:, class_i])
i = len(thresholds) - 1
best_recall_threshold = None
while best_recall_threshold is None:
next_threshold = thresholds[i]
next_recall = recall[i]
if next_recall >= recall_threshold:
best_recall_threshold = next_threshold
i -= 1

# consice, even though unnecessary passing through all the values
best_precision_threshold = [thres for prec, thres in zip(precision, thresholds) if prec >= precision_threshold][0]

if plot:
plt.figure(figsize=(10, 7))
plt.step(recall, precision, color='r', alpha=0.3, where='post')
plt.fill_between(recall, precision, alpha=0.3, color='r')
plt.axhline(y=precision[i + 1])
recall_for_prec_thres = [rec for rec, thres in zip(recall, thresholds)
if thres == best_precision_threshold][0]
plt.axvline(x=recall_for_prec_thres, color='g')
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.ylim([0.0, 1.05])
plt.xlim([0.0, 1.0])
plt.legend(['PR curve',
f'Precision {precision[i + 1]: .2f} corresponding to selected recall threshold',
f'Recall {recall_for_prec_thres: .2f} corresponding to selected precision threshold'])
plt.title(f'Precision-Recall curve for Class {class_names[class_i]}')
return best_recall_threshold, best_precision_threshold


y_pred = model.predict_generator(data_generator_val, workers=num_cores)
y_true = data_generator_val.get_labels()
recall_thresholds = dict()
precision_thresholds = dict()

for i, class_name in tqdm(enumerate(class_names)):
recall_thresholds[class_name], precision_thresholds[class_name] = get_threshold_for_recall(y_true, y_pred, i, plot=True)

我预计这四个类别有四个精确召回曲线,但是,我收到以下错误消息:

---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-79-422044a4f5da> in <module>
37 precision_thresholds = dict()
38 for i, class_name in tqdm(enumerate(class_names)):
---> 39 recall_thresholds[class_name], precision_thresholds[class_name] = get_threshold_for_recall(y_true, y_pred, i, plot=True)

<ipython-input-79-422044a4f5da> in get_threshold_for_recall(y_true, y_pred, class_i, recall_threshold, precision_threshold, plot)
12
13 # consice, even though unnecessary passing through all the values
---> 14 best_precision_threshold = [thres for prec, thres in zip(precision, thresholds) if prec > precision_threshold][0]
15
16 if plot:

IndexError: list index out of range

最佳答案

解决了我在上面的代码片段中遇到的问题。请确保您拥有正确的版本:tensorflow==1.14.0 和 keras=2.3.0

关于python - 为什么我会收到 best_ precision_threshold 的 IndexError ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58455703/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com