在Sklearn中的Accuracy_Score

在Sklearn中的Accuracy_Score

在数据科学工作流程中,一个关键的阶段是使用适当的指标来衡量我们模型的准确性。在本教程中,我们将学习两种计算源样本预测类别准确性的方法:手动计算和使用Python的scikit-learn库。

这是我们在本教程中讨论的主题概述。

  • 手动计算准确性得分
  • 使用scikit-learn计算准确性得分
  • scikit-learn准确性得分的示例
  • scikit-learn的准确性得分是如何工作的?

什么是准确性

准确性是计算分类模型性能的广泛使用的指标之一。准确性表示我们的模型成功预测的标签的百分比。例如,如果我们的模型准确地分类了100个标签中的80个,它的准确性将为0.80。

创建计算准确性得分的函数

让我们创建一个Python函数来计算预测值的准确性得分,假设我们已经有了样本的真实标签和模型预测的标签。

代码

# Python program to define a function to compute accuracy score of model's predicted class

# Defining a function which takes true values of the sample and values predicted by the model
def compute_accuracy(Y_true, Y_pred):
    correctly_predicted = 0
    # iterating over every label and checking it with the true sample
    for true_label, predicted in zip(Y_true, Y_pred):
        if true_label == predicted:
            correctly_predicted += 1
    # computing the accuracy score
    accuracy_score = correctly_predicted / len(Y_true)
    return accuracy_score

上述函数接受分类模型预测标签和样本的真实标签作为参数,并计算准确率得分。在这里,我们通过并行迭代每对真实标签和预测标签来记录正确预测的数量。然后我们将该数量除以总标签数来计算准确率得分。

现在我们将在一个样本上应用该函数。

代码

# Python program to compute accuracy score using the function compute_accuracy

# Importing the required libraries
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.datasets import load_iris

# Loading the dataset
X, Y = load_iris(return_X_y = True)

# Splitting the dataset in training and test data
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.3, random_state = 0)

# Training the model using the Support Vector Classification class of sklearn
svc = SVC()
svc.fit(X_train, Y_train)

# Computing the accuracy score of the model
Y_pred = svc.predict(X_test)
score = compute_accuracy(Y_test, Y_pred)
print(score)

输出:

0.9777777777777777

我们得到0.978作为支持向量分类模型的预测准确率。

请注意,使用numpy数组来向量化相等性计算可以使上述提到的代码更高效。

使用Sklearn的accuracy_score()计算准确率

sklearn.metrics的accuracy_score()方法接受样本的真实标签和模型预测的标签作为参数,并将准确率作为浮点值计算,这可以用于获取Python中的准确率。sklearn.metrics类中有几个有用的函数可以计算典型的评估指标。让我们使用sklearn的accuracy_score()函数来计算使用与之前相同的样本数据集的支持向量分类模型的准确率。

sklearn.metrics.accuracy_score(y_true, y_pred, *, normalize=True, sample_weight=None)

我们使用它来计算分类的准确率。该方法计算了多标签分类中的子组准确度;预测的标签子集必须与y_true中的实际标签数据集完全匹配。

参数

  1. y_true(1D数组或指示标签/稀疏矩阵的数组): 这些是给定样本的真实标签。
  2. y_pred(1D数组或指示标签/稀疏矩阵的数组): 分类模型返回的预测标签。
  3. normalize(bool,默认=True): 如果为False,则给出成功分类的预测样本数量。如果为True,则返回正确分类的预测样本比例。
  4. sample_weight(形状为(n,)的数组样本权重,默认=None): 样本权重。

返回值

  1. score(float): 如果normaliseTrue,则返回成功分类的样本比例(浮点数);如果normaliseFalse,则返回成功分类的预测样本数(整数)。当normaliseTrue,并且提供了样本数量时,1表示100%的准确率。

准确率示例

代码

# Python program to compute accuracy score using the function accuracy_score

# Importing the required libraries
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score 
from sklearn.svm import SVC
from sklearn.datasets import load_iris

# Loading the dataset
X, Y = load_iris(return_X_y = True)

# Splitting the dataset in training and test data
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.3, random_state = 0)

# Training the model using the Support Vector Classification class of sklearn
svc = SVC()
svc.fit(X_train, Y_train)

# Computing the accuracy_score of the model
Y_pred = svc.predict(X_test)
score = accuracy_score(Y_test, Y_pred)
print(score)

输出:

0.9777777777777777

使用二进制标签指标和多个标签时:

代码

# Python program to show how to calculate accuracy score for multi-label data
import numpy as np
accuracy_score(np.array([[1, 1], [2, 1]]), np.ones((2, 2)))

输出:

0.5

scikit learn的accuracy_score工作原理

sklearn.metrics包中的accuracy_score方法用于在多标签分类中分配子集准确度。

需要模型预测的标签与样本的真实标签完全匹配。

准确性描述了模型在所有类别中的行为。如果所有类别都有相对重要的影响,这很有帮助。

使用准确预测的数量与样本总数或预测总数的比率来确定模型的准确性。

代码:

  1. 下面的代码导入了两个库。我们导入sklearn.metrics来预测模型的准确性以及numpy库。
  2. 样本的真实值为y_true = [“1”, “1”, “0”, “0”, “1”, “1”, “0”]。
  3. [“1”, “1”, “0”, “0”, “1”, “1”, “0”]这些是模型对样本数据的预测值。
  4. 使用Accuracy = (matrix[0][0] + matrix[-1][-1]) / numpy.sum(matrix)来获取分类模型的准确性得分。
  5. 使用print(accuracy)将准确度得分显示为输出。

    代码

# Pythpn program to show how accuracy_score works

# import libraries 
import numpy as np 
import sklearn.metrics

# Creating a true and predicted sample
Y_true = ["1", "1", "0", "0", "1", "1", "0"] 
Y_pred = ["1", "0", "1", "1", "0", "1", "1"] 

# finding a confusion matrix
matrix = sklearn.metrics.confusion_matrix(Y_true, Y_pred) 
matrix = np.flip(matrix) 
print("Confusion Matrix: \n", matrix)
accuracy = (matrix[0][0] + matrix[-1][-1]) / np.sum(matrix) 
print(accuracy)

输出:

Confusion Matrix: 
 [[2 2]
 [3 0]]
0.2857142857142857

所以,在本教程中,我们学习了Python中的scikit-learn准确度评分,并查看了一些实现示例。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程