【问题标题】:What is the easiest and fastest way of converting probabilities into prediction labels? [closed]将概率转换为预测标签的最简单和最快的方法是什么? [关闭]
【发布时间】:2018-05-22 06:57:45
【问题描述】:

简而言之,我正在寻找以下代码的一/两行代码:

for i in range(A.shape[1]):

    # Convert probabilities A[0,i] to actual predictions p[0,i]
    ### START CODE HERE ### (≈ 4 lines of code)
    if(A[i] > .5)
       Y_prediction[i] = 1
    else 
        Y_prediction[i] = 0

【问题讨论】:

  • 添加具有预期输出的示例案例?
  • Andrew Ng 的 DL 专业的代码...?

标签: numpy machine-learning classification vectorization logistic-regression


【解决方案1】:

您希望将概率值转换为 0-1 标签。分配V_prediction[0, :] = A[0, :] > 0.5 应该足够了;如果目标数组 V_prediction 是数字,则布尔值 A[0, :] > 0.5 (True/False) 将变为数字 1、0。一个例子:

V_prediction = np.zeros((3, 10))
A = np.random.uniform(size=(3, 10))
V_prediction[0, :] = A[0, :] > 0.5 

V_prediction 现在(随机)

array([[ 1.,  0.,  1.,  1.,  0.,  1.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])

如果所有标签都是整数,V_prediction 可以声明为整数数据类型。

一种更精细的方法,它扩展到更复杂的场景,是使用numpy.piecewise

V_prediction[0, :] = np.piecewise(A[0, :], [A[0, :] > 0.5, A[0, :] <= 0.5], [0, 1])

【讨论】:

    【解决方案2】:

    numpy argmax 函数在这里应该派上用场。只需将您的数组作为参数传递给 argmax 函数,它就会根据概率为您提供标签。

    更多信息,see here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-11
      • 2015-08-19
      • 2014-05-09
      • 2015-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多