此代碼引發可被 0 整除的錯誤 for key in results_dic: # match (if dog then breed match) if results_dic[key][2] == 1: # isa dog (pet label) & breed match if results_dic[key][3] == 1: n_pet_dog += 1 # isa dog (classifier label) & breed match if results_dic[key][4] == 1: n_class_cdog += 1 n_match_breed += 1 # NOT dog (pet_label) else: # NOT dog (classifier label) if results_dic[key][4] == 0: n_class_cnotd += 1 # NOT - match (not a breed match if a dog) else: # NOT - match # isa dog (pet label) if results_dic[key][3] == 1: n_pet_dog += 1 # isa dog (classifier label) if results_dic[key][4] == 1: n_class_cdog += 1 # NOT dog (pet_label) else: # NOT dog (classifier label) if results_dic[key][4] == 0: n_class_cnotd += 1# calculates statistics based upon counters from aboven_pet_notd = n_images - n_pet_dogpct_corr_dog = ( n_class_cdog / n_pet_dog )*100pct_corr_notdog = ( n_class_cnotd / n_pet_notd )*100pct_corr_breed = ( n_match_breed / n_pet_dog )*100即使我使用if-else語句,它也會拋出相同的錯誤,我應該使用異常語句,我怎么能在這種情況下使用它,我已經被告知這是因為縮進錯誤,但我檢查了它,它應該沒有問題
3 回答

Cats萌萌
TA貢獻1805條經驗 獲得超9個贊
假設 和 是整數,我建議將分母更改為 和,以便您可以避免在那里使用 min 值,而是回退到最小值。示例:更改n_pet_dog
n_pet_notd
max(n_pet_notd, 1)
max(n_pet_dog, 1)
0
1
pct_corr_dog = ( n_class_cdog / n_pet_dog )*100
自
pct_corr_dog = ( n_class_cdog / max(n_pet_dog,1) )*100

手掌心
TA貢獻1942條經驗 獲得超3個贊
您可以使用三元運算符來獲取干凈的代碼。
pct_corr_dog = (n_class_cdog/n_pet_dog)*100 if (n_pet_dog != 0) else #what to do otherwise

藍山帝景
TA貢獻1843條經驗 獲得超7個贊
您可以通過不除以零來修復除以零。
n_pet_dog可能為零。這或多或少是不可避免的。可能沒有寵物狗。( n_class_cdog / n_pet_dog )
在這種情況下,試圖確定寵物狗的百分比是沒有意義的,所以你應該(a)不執行這個語句,并且(b)決定在沒有寵物狗的情況下,你想要你的代碼做什么。
添加回答
舉報
0/150
提交
取消