简体   繁体   English

在没有 sklearn 的情况下计算机器学习模型的准确性

[英]Calculate the accuracy of a machine learning model without sklearn

I'm trying to calculate the accuracy of a model I created using the function below:我正在尝试使用以下函数计算我创建的模型的准确性:

def accuracy(y_true, y_pred):
    accuracy = np.mean(y_pred == y_true)
    return accuracy

Sometimes it displays the accuracy correctly and sometimes its incorrect.有时它正确显示准确性,有时它不正确。 Can someone explain how can i fix the function to have it display the same accuracy as sklearn accuracy_score.有人可以解释我如何修复该函数以使其显示与 sklearnaccuracy_score 相同的准确度。 Here's an example of the results I am getting from my method.这是我从我的方法中得到的结果的一个例子。

y_true
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0]

y_pred
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0]

KNN classification accuracy:  0.0
KNN classification accuracy sklearn:  0.9428571428571428

With numpy you can do the following:使用 numpy,您可以执行以下操作:

import numpy as np
acc = np.sum(np.equal(y_true,y_pred))/len(y_true)

The way you set it up, y_pred == y_true will always be False if even one value in your list is not the same.根据您的设置方式,即使列表中的一个值不相同, y_pred == y_true也将始终为 False。 When you do np.mean(False) you will get a value of 0.0.当您执行np.mean(False)您将获得 0.0 的值。

What you want to do is iteratively find whether each item in your lists are equal.您想要做的是迭代查找列表中的每个项目是否相等。 Here is the solution I made:这是我提出的解决方案:

def accuracy(y_true,y_pred,normalize=True):
    accuracy=[]
    for i in range(len(y_pred)):
        if y_pred[i]==y_true[i]:
            accuracy.append(1)
        else:
            accuracy.append(0)
    if normalize==True:
        return np.mean(accuracy)
    if normalize==False:
        return sum(accuracy)

The accuracy list will keep track of if each item of y_pred is equal to y_true.准确度列表将跟踪 y_pred 的每个项目是否等于 y_true。 Like in https://scikit-learn.org/stable/modules/model_evaluation.html#accuracy-score , if they are equal, then 1 should be appended.就像在https://scikit-learn.org/stable/modules/model_evaluation.html#accuracy-score 中一样,如果它们相等,则应附加 1。 If they are not, then 0 should be appended.如果不是,则应附加 0。

You did not include this in your original function, but just in case you were curious, I added the normalize option just like sklearn.您没有在原始函数中包含它,但以防万一您好奇,我像 sklearn 一样添加了 normalize 选项。 Normalize will automatically be true and will find the mean, but this is the code if you wanted the count of how many were accurate. Normalize 将自动为真,并会找到平均值,但如果您想计算准确的数量,这就是代码。

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

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