python - 值错误 : continuous format is not supported

标签 python scikit-learn

我写了一个简单的函数,我正在使用 average_precision_scorescikit-learn 计算平均精度。

我的代码:

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]

我在这里做错了什么?报错是什么意思?

最佳答案

看看sklearn 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).

因此,您的第一个参数必须是二进制标签数组,但您传递的是某种 float 组作为第一个参数。所以我认为您需要颠倒传递的参数的顺序。

关于python - 值错误 : continuous format is not supported,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44468172/

相关文章:

python - HTTP/1.1 与 HTTP/1.0 Python 套接字

python - 为每行生成特定数量的 1,但仅限于 x = 0 的情况

python - 故障训练 SVM(scikit-learn 包)

machine-learning - SKlearn (scikit-learn) 用于回归的多元特征选择

python - 如何绘制 K 均值并打印集群外的点

python - 如何在 Scikit 上训练时获取权重矩阵

python - "How to convert a Duration(ex. 2h 50m, 8h 35m) an object column in a dataframe into a datetime column with only minutes?"

python - Groupby 并使用 pandas 中的多个函数执行聚合

python - 如何用python创建透明的径向渐变?

python - 多次使用 Sklearn Fit 模型