简体   繁体   English

在Python中使用Logistic回归的预测矢量的准确性得分

[英]Accuracy Score for a vector of predictions using Logistic Regression in Python

I'm doing the multiclass classification using Logistic Regression approach. 我正在使用Logistic回归方法进行多类分类。 Basically I know that if I use accuracy_score () function (for example, from sklearn library) it will calculate me the accuracy of distinct value to distinct value like this: 基本上我知道,如果我使用precision_score()函数(例如,来自sklearn库),它将像下面这样计算出不同值到不同值的准确性:

y_pred = [0, 2, 1, 3]
y_true = [0, 1, 2, 3]
accuracy_score(y_true, y_pred)
0.5

But I want to get the accuracy_score_new () function for a vector of top Logistic Regression predictions for each label (from predict_proba) and calculates whether the true label is in this interval like this: 但是我想为每个标签(来自predict_proba)的顶级Logistic回归预测的向量获取precision_score_new()函数,并计算真实标签是否在此间隔内,如下所示:

y_pred = [[0,1,3] [2,1,4], [1,2,5] [3,7,9]]
y_true = [0, 1, 2, 3]
accuracy_score_new(y_true, y_pred)
1

The accuracy_score_new in this example will be equal to 1 because the classifier predicts that the label is in the interval. 此示例中的precision_score_new等于1,因为分类器预测标签在间隔中。 How can this function be done? 如何完成此功能?

Yes you can do that using the make_scorer function in sklearn. 是的,您可以使用sklearn中的make_scorer函数来实现。 The idea is that you define your custom function assuming it gets the two parameters y_true and y_pred. 想法是,您在定义自定义函数时假设它获得了两个参数y_true和y_pred。 You can also add any additional parameters if you want. 如果需要,还可以添加任何其他参数。

Here is an example : Custom scoring function 这是一个示例: 自定义评分功能

Here is another example : Using MSE and R2 score at the same time 这是另一个示例:同时使用MSE和R2分数

This answer might be of some help too. 这个答案可能也会有所帮助。

Accuracy is just (matching values /total values). 准确性就是(匹配值/总值)。

So in your case it will be something like: 因此,在您的情况下,它将类似于:

def accuracy_score_new(y_pred, y_true):
    matched = 0
    for y_p, y_t in zip(y_pred, y_true):
        if y_t in y_p:
            matched = matched + 1

    return (matched / (float) len(y_true))

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM