简体   繁体   中英

Why am I getting an IndexError for best_precision_threshold?

I've ran the following code without an issue on a different machine, however, when I try running it on another machine I run into the following error:

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)

I expect four precision-recall curves for the four classes, however, instead I get the following error message:

---------------------------------------------------------------------------
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

Figured out the issue that I was having with the code snippet above. Please make sure you have the right versions: tensorflow==1.14.0 and keras=2.3.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM