题解|计算二元分类的Jaccard指数

计算二元分类的Jaccard指数

https://www.nowcoder.com/practice/fb2664dc29f94f73af37a1ce3d5be3bc?tpId=377&tags=&title=&difficulty=&judgeStatus=&rp=0&sourceUrl=%2Fexam%2Foj&gioEnter=menu

Jaccard指数是一种衡量分类模型性能的指标,其计算公式为:

其中,TP是真阳性,FP是假阳性,FN是假阴性。 TP = True Positives (真正例): 正确预测为正类的样本数量 FP = False Positives (假阳性): 错误预测为正类的负类样本数量 FN = False Negatives (假阴性): 错误预测为负类的正类样本数量

标准代码如下

def jaccard_index(y_true, y_pred):
    intersection = np.sum((y_true == 1) & (y_pred == 1))
    union = np.sum((y_true == 1) | (y_pred == 1))
    result = intersection / union
    if np.isnan(result):
        return 0.0
    return round(result, 3)

在sklearn库中也有对应的计算方法,可以使用metrics模块中的jaccard_score函数:

def jaccard_index(y_true, y_pred):
    from sklearn.metrics import jaccard_score
    jaccard = jaccard_score(y_true, y_pred)
    return round(float(jaccard), 3)
全部评论

相关推荐

自来熟的放鸽子能手面...:等捞就好了,你这种情况被捞的概率挺大的
投递字节跳动等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务