代码之家  ›  专栏  ›  技术社区  ›  Roman

预测概率是否高于某个值

  •  0
  • Roman  · 技术社区  · 5 年前

    我使用MLP模型进行分类。

    当我预测新数据时,我只想保留那些预测概率大于0.5的预测,并将所有其他预测更改为0类。

    在凯拉斯我怎么做?

    我使用最后一层,如下所示 model.add(layers.Dense(7 , activation='softmax'))

    使用softmax获得概率大于0.5的预测是否有意义?

    newdata = (nsamples, nfeatures)
    predictions = model.predict (newdata)
    print (predictions.shape)
    (500, 7)
    
    0 回复  |  直到 5 年前
        1
  •  1
  •   Gerry P    5 年前

    你可以这样做:

    preds=model.predict etc
    index=np.argmax(preds)
    probability= preds(index)
    if probability >=.75:
        print (' class is ', index,' with high confidence')
    elif probability >=.5:
        print (' class is ', index,' with medium confidence')
    else:
        print (' class is ', index,' with low confidence')
    
        2
  •  1
  •   Frightera    5 年前

    Softmax功能输出 概率 在你的例子中,你将有7个类,它们的概率和将等于1。

    现在考虑一个案例 [0.1, 0.1, 0.1, 0.1, 0.1, 0.2, 0.3] 这是softmax的输出。如您所见,在这种情况下应用阈值是没有意义的。

    阈值0.5与n类预测无关。对于二进制分类来说,这是一种特殊的东西。

    要获取类,应该使用argmax。

    编辑:如果你想在预测低于某个阈值时放弃预测,你可以使用,但这是不可能的 这不是正确的处理方式 多类预测:

    labels = []
    threshold = 0.5
    
    for probs_thresholded in out:
       labels.append([])
       
       for i in range(len(probs_thresholded)):
          if probs_thresholded[i] >= threshold:
             labels[-1].append(1)
          else:
             labels[-1].append(0)