In-depth understanding of from sklearn.metrics import accuracy_score, novices can understand it at a glance

Insert image description here


1. What does from sklearn.metrics import accuracy_score mean?

sklearn.metrics is one of the modules developed by scikit-learn for evaluating the performance of classification, regression and clustering algorithms. It includes a variety of commonly used evaluation index functions. accuracy_score(y_true, y_pred) is one of them, used to calculate the accuracy of the classification model, that is, the number of correctly classified samples divided by the total number of samples. The parameters of the function have the following meanings:

y_true:真实的分类标签,可以是列表、数组、Pandas Series或其他可迭代对象。
y_pred:模型预测的分类标签,需要与y_true格式相同。
accuracy_score()函数将返回一个01之间的单精度浮点数,表示分类模型的准确率。

2. Specific cases teach us how to use

from sklearn.metrics import accuracy_score
y_true = [0, 1, 2, 1, 3]
y_pred = [0, 1, 2, 2, 3]
acc = accuracy_score(y_true, y_pred)
print(acc)

The output result is

0.8

Summarize

  1. In the above example, y_true is the true classification label and y_pred is the model-predicted classification label.

  2. Among them, the classification label of 4 samples is predicted correctly and one sample is predicted incorrectly, so the accuracy is 0.8 (4/5).

  3. The accuracy_score() function is widely used in machine learning classification tasks. It can measure the overall performance of the classification model, but it also needs to be combined with other indicators for comprehensive analysis.

  4. It should be noted that classification accuracy is not necessarily a good evaluation metric, especially when the data categories are imbalanced.
    In this case, other evaluation metrics, such as recall, precision, and F1-score, may be more appropriate.

Guess you like

Origin blog.csdn.net/qlkaicx/article/details/131205871