简体   繁体   English

如何在 python 中使用多处理计算 f1 分数?

[英]How to calculate f1 score using multiprocessing in python?

I've got an array of paired binary labels: y_true , y_pred .我有一组成对的二进制标签: y_truey_pred My array contains ~50 million elements, and I wish to evaluate success using f1 score preferably, or AUC.我的数组包含约 5000 万个元素,我希望最好使用f1 分数或 AUC 来评估成功。

However, calculating f1 using sklearn takes relatively long time – about half the time needed for an entire epoch.但是,使用sklearn计算 f1 需要相对较长的时间——大约是整个 epoch 所需时间的一半。 Calculating AUC was faster, but too slow as well.计算 AUC 更快,但也太慢了。 Similar question yielded Faster AUC in sklearn or python , but I'm not sure I can try this one.类似的问题在 sklearn 或 python 中产生了更快的 AUC ,但我不确定我可以试试这个。

Is there a way to speed up those calculations, perhaps with multiprocessing?有没有办法加快这些计算,也许是多处理?

Alright, so apparently Scikit-learn implementation ran too slow on my two vectors, so I modified this implementation , so it fits numpy arrays and it is now much faster (0.25 seconds compared to 50+ seconds on sk-learn).好吧,显然 Scikit-learn 实现在我的两个向量上运行得太慢了,所以我修改了这个实现,所以它适合 numpy arrays 现在比 50+ 秒快得多(0.25 秒)。 Original implementation using torch.Tensors.使用 torch.Tensors 的原始实现。

def f1_loss(y_true, y_pred, beta=1) -> numpy.float32:
    '''Calculate F1 score.
    
    The original implmentation is written by Michal Haltuf on Kaggle.
    
    Reference
    ---------
    - https://www.kaggle.com/rejpalcz/best-loss-function-for-f1-score-metric
    - https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html#sklearn.metrics.f1_score
    - https://discuss.pytorch.org/t/calculating-precision-recall-and-f1-score-in-case-of-multi-label-classification/28265/6
    
    '''
    assert y_true.shape[1] == 1
    assert y_pred.shape[1] == 1
        
    
    tp = (y_true * y_pred).sum()
    tn = ((1 - y_true) * (1 - y_pred)).sum()
    fp = ((1 - y_true) * y_pred).sum()
    fn = (y_true * (1 - y_pred)).sum()
    
    epsilon = 1e-7
    
    precision = tp / (tp + fp + epsilon)
    recall = tp / (tp + fn + epsilon)
    
    f1 = (1 + beta**2)* (precision*recall) / (beta**2 * precision + recall + epsilon)

    return f1 

  
    
    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在测试集评估期间计算 f1 分数? - How to calculate f1 score during evaluation on test set? 如何在 python 中找到已经训练和保存的 model 的 f1 分数 - How to find f1 score of a already trained & saved model in python 使用sklearn计算宏f1分数 - Computing macro f1 score using sklearn 使用 Gridsearch 获得最佳 f1 分数 - Getting best f1 score using Gridsearch 如何使用python计算Precision、Recall和F-score? - How to calculate Precision, Recall and F-score using python? 如何提高分类的 F1 分数 - How to improve F1 score for classification 在 tf.Estimator 设置中使用 tf.metrics.precision/recall 计算 F1 分数 - Calculate F1 Score using tf.metrics.precision/recall in a tf.Estimator setup 如何在 Keras 模型中使用 F1 Score? - How to use F1 Score with Keras model? 如何找出在 Python 中网格搜索中选择的最佳 model 的精度、召回率、特异性和 F1 分数? - How to find out precision, recall, specificity & F1 score of the best model which is selected in Grid Search in Python? 如何计算 python 中平衡逻辑回归 model 的精度、召回率和 f1 分数 - How to compute precision,recall and f1 score of an balanced logistic regression model in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM