如何避免警告:UndefinedMetricWarning:
Posted
技术标签:
【中文标题】如何避免警告:UndefinedMetricWarning:【英文标题】:How to avoid warning: UndefinedMetricWarning: 【发布时间】:2021-02-04 23:59:19 【问题描述】:我在运行代码时收到警告,见下文。 但是,结果将打印accuracy1、precision1 和recall1。如何避免警告?我正在使用 python 2.7。
警告:UndefinedMetricWarning:精度定义不明确,由于没有预测样本,因此设置为 0.0。 'precision', 'predicted', average, warn_for)
acc = []
pre = []
recall = []
for i in range(iters):
features_train, features_test, labels_train, labels_test = \
train_test_split(features, labels, test_size = 0.3, random_state = i)
grid_search.fit(features_train, labels_train)
predicts = grid_search.predict(features_test)
acc = acc + [accuracy_score(labels_test, predicts)]
pre = pre + [precision_score(labels_test, predicts)]
recall = recall + [recall_score(labels_test, predicts)]
print "accuracy1: ".format(np.mean(acc))
print "precision1: ".format(np.mean(pre))
print "recall1: ".format(np.mean(recall))
best_params = grid_search.best_estimator_.get_params()
for param_name in params.keys():
print("%s = %r, " % (param_name, best_params[param_name]))
【问题讨论】:
【参考方案1】:import warnings
warnings.simplefilter('ignore')
上述模块导入解决了我的问题。
【讨论】:
【参考方案2】:你可以这样做:
import warnings
warnings.filterwarnings("ignore")
【讨论】:
以上是关于如何避免警告:UndefinedMetricWarning:的主要内容,如果未能解决你的问题,请参考以下文章