值错误:不支持连续格式

2024-04-19 00:41:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经编写了一个简单的函数,其中使用scikit-learn中的average_precision_score来计算平均精度。

我的代码:

def compute_average_precision(predictions, gold):
    gold_predictions = np.zeros(predictions.size, dtype=np.int)
    for idx in range(gold):
        gold_predictions[idx] = 1
    return average_precision_score(predictions, gold_predictions)

当函数被执行时,它会产生以下错误。

Traceback (most recent call last):
  File "test.py", line 91, in <module>
    total_avg_precision += compute_average_precision(np.asarray(probs), len(gold_candidates))
  File "test.py", line 29, in compute_average_precision
    return average_precision_score(predictions, gold_predictions)
  File "/if5/wua4nw/anaconda3/lib/python3.5/site-packages/sklearn/metrics/ranking.py", line 184, in average_precision_score
    average, sample_weight=sample_weight)
  File "/if5/wua4nw/anaconda3/lib/python3.5/site-packages/sklearn/metrics/base.py", line 81, in _average_binary_score
    raise ValueError("{0} format is not supported".format(y_type))
ValueError: continuous format is not supported

如果我打印两个numpy数组predictionsgold_predictions,比如说一个例子,它看起来很好。[下面提供一个例子。]

[ 0.40865014  0.26047812  0.07588802  0.26604077  0.10586583  0.17118802
  0.26797949  0.34618672  0.33659923  0.22075308  0.42288553  0.24908153
  0.26506338  0.28224747  0.32942101  0.19986877  0.39831917  0.23635269
  0.34715138  0.39831917  0.23635269  0.35822859  0.12110706]
[1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]

我在这里做错了什么?错误的含义是什么?


Tags: 函数inpyformatreturnnplineprecision
1条回答
网友
1楼 · 发布于 2024-04-19 00:41:43

只是看看^{} docs

Parameters:

y_true : array, shape = [n_samples] or [n_samples, n_classes] True binary labels in binary label indicators.

y_score : array, shape = [n_samples] or [n_samples, n_classes] Target scores, can either be probability estimates of the positive class, confidence values, or non-thresholded measure of decisions (as returned by “decision_function” on some classifiers).

所以第一个参数必须是一个二进制标签数组,但是要传递某种浮点数组作为第一个参数。所以我相信你需要颠倒你所传递的论点的顺序。

相关问题 更多 >