目前在ML项目中测试和训练模型,我得到了这个零除法错误在这一行。
p_bar.set_description('{}. Testing Data of phoneme "{}" against all models nResult: {}/{}
correct prediction;n accuracy: {:.2f}%'.format(
i+1,fc.get39Phon(i),count,len(test_lengths[i]),(count/len(test_lengths[i]))*100) #LINE ERROR
我不明白为什么它产生异常零。我该如何解决这个问题?
训练模型:
try:
for i in range(39):
p_bar.set_description('{}. Training "{}" Phoneme Model'.format(i,fc.get39Phon(i)))
models[i].fit(features[i].reshape(-1,1),lengths[i] )#Expected 2D array, got 1D array instead, I reshaped the data as suggested
traceback.print_stack()
p_bar.update()
except Exception:
print(traceback.format_exc())
测试模型
for i in range(39):
# --- adding missing length at end
tfeat_len = test_features[i].shape[0]
tlen_len = np.sum(test_lengths[i])
if tfeat_len != tlen_len:
test_lengths[i].append(tfeat_len-tlen_len)
predictions = []
for i in range(39):
#for each phon data
count = 0
s = 0
p_bar = tqdm(range(len(test_lengths[i])))
p_bar.set_description('{}. Testing Data of phoneme "{}" against all models'.format(i,fc.get39Phon(i)))
for j in test_lengths[i]:
# test in each phon model
max_prediction = -999999999999
max_index = 0
t_feat = test_features[i][s:j+s]
for k in range(39):
try:
score = math.floor(models[k].score(t_feat)*1000)
if(score > max_prediction):
max_prediction = score
max_index = k
if max_index > i:
break
except:
continue
p_bar.update()
count+= 1 if max_index == i else 0
s=j
predictions.append((count,len(test_lengths[i])))
<<h3>回溯/h3>traceback (most recent call last):
File "d:github-spacePhoneme-Recognizer-r.py", line 392, in <module>
models[i].fit(features[i].reshape(-1,1),lengths[i])
File "C:PythonPython39libsite-packageshmmlearnbase.py", line 496, in fit
X = check_array(X)
File "C:UsersAcerAppDataRoamingPythonPython39site-packagessklearnutilsvalidation.py", line 909, in check_array
raise ValueError(
ValueError: Found array with 0 sample(s) (shape=(0, 1)) while a minimum of 1 is required.
它会给你一个除零错误,因为count/len(test_lengths[i])*100
中的len(test_lengths[i])
是0,而你知道一个数除以0是没有定义的,所以它会给你这个错误。