从 python 中的文本标签中删除零值

remove zeros vlaues from test labels in python

我已经训练了一个模型,我想通过将误差除以我的数据集的 测试标签 来找到它的百分比精度。但是,测试标签中有一些零值,它们是缺失值的原因。因此,将相应的误差除以这些值将导致无穷大。

mape = 100 * (errors / y_test)
# Calculate and display accuracy
accuracy = 100 - np.mean(mape)
print('Accuracy:', round(accuracy, 2), '%.')

上面的代码片段将打印“inf”作为输出。我应该以某种方式摆脱“y_test”系列中的零值。一种方法是找到该系列中零值的索引,然后删除 error 数组中的相应值。

erry = np.array([errors,y_test])

现在,我想知道如何编写代码来删除 erry 中第二列为零的那些元素?

如果你知道一些更明智的方法来计算模型的准确性,同时注意缺失值,请指出它们

我会使用 y_test 为两个数组创建布尔索引:

idx = y_test != 0
mape = 100 * (errors[idx] / y_test[idx])
# Calculate and display accuracy
accuracy = 100 - np.mean(mape)
print('Accuracy:', round(accuracy, 2), '%.')