Python 为什么我要找一个索引器来获得最佳精度阈值?
我在另一台机器上运行了以下代码,但在另一台机器上运行时遇到以下错误:Python 为什么我要找一个索引器来获得最佳精度阈值?,python,python-3.x,Python,Python 3.x,我在另一台机器上运行了以下代码,但在另一台机器上运行时遇到以下错误: 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
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
---------------------------------------------------------------------------
索引器回溯(最后一次最近调用)
在里面
37精度_阈值=dict()
38对于i,TQM中的类名称(枚举(类名称)):
--->39调用阈值[类名称],精度阈值[类名称]=获取调用阈值(y\u真,y\u pred,i,plot=真)
用于调用的获取阈值(y\u真、y\u pred、类i、调用阈值、精度阈值、绘图)
12
13#一致性,即使不需要通过所有值
--->14最佳精度阈值=[prec的阈值,如果prec>精度阈值,则zip中的阈值(精度,阈值)[0]
15
16如果绘图:
索引器:列表索引超出范围
解决了我在上面的代码片段中遇到的问题。请确保您有正确的版本:tensorflow==1.14.0和keras=2.3.0显然,[thres for prec,thres in zip(精度,阈值),如果prec>precision\u threshold]
正在创建一个空列表。