Python complete tutorial
  • Python Complete Tutorial
  • About this book
  • What you need to prepare
  • 1️⃣Try python for the first time
    • Install python
    • Hello world!
    • Hello world in a nutshell
    • The first simple python project
    • most useful libraries
    • Recommended websites
  • 2️⃣Data structure and basic operations
    • Python data structure
    • Data structure without hash table
    • Data structure with hash table
    • Variability and address
    • basic python programming
    • basic python programming 2
    • basic python programming 3
    • some additions
    • Fibonacci sequence
    • Judging prime numbers
    • txt/csv file operation
  • 🐍Practice program
    • 🚩fancy print
    • 🚩Remove duplicate elements
    • 🚩Palindrome detection
  • 😎leetcode
    • what is leetcode
  • 3️⃣Data mining and machine learning
    • What is data mining
    • iris data set
    • Mean median mode
    • Harmonic mean
    • Histogram
    • Correlation algorithm
    • Gaussian distribution data set
    • projection
    • PCA
    • MDS
    • Bayesian and Frequentist
    • Data normalization
    • binary SVM
    • One Hot Encoding
    • Multi-class SVM
    • Accuracy and error rate
    • Confusion matrix & Accuracy, Precision, Recall
    • F1 score
    • ROC and AUC
  • 4️⃣big data and data visualization
    • line chart
    • Parallel coordinates
    • Histogram chart
  • 5️⃣Mathematical algorithm and digital signal processing
    • Mathematical constants and basic operations
    • Normal distribution
    • Permutation and combination
    • Bernoulli distribution
    • Chaotic system
  • 6️⃣Classes and design patterns
    • Classes and design patterns
  • 7️⃣Operate the database with python
    • MySQL
      • Install MySQL
      • First try MySQL
      • MySQL Architecture
      • database operations
      • database
  • 8️⃣Cryptography
    • beginning of Cryptography
  • 9️⃣deep learning
    • What is Deep Learning
    • basic
  • 💔algorithm
    • Algorithms and Data Structures
Powered by GitBook
On this page
  1. Data mining and machine learning

Accuracy and error rate

Evaluate the model's quality

In the previous chapter, we used SVM to predict the handwritten digits in the mnist dataset. In this chapter, we will evaluate the quality of our model.

The most commonly used method to check the quality of a model is accuracy. This is also used a lot in our lives. For example, in our exams, students get scores if they answer correctly, but they don’t get scores if they don’t answer it correctly. In the end, students' scores determine their academic performance. The score here is the accuracy.

model_pred = [
    7, 0, 1, 0, 4, 1, 9, 4, 1, 9,
    0, 1, 9, 0, 1, 9, 7, 7, 2, 4,
    9, 6, 1, 5, 4, 0, 7, 4, 0, 1,
    3, 1, 3, 4, 9, 5, 7, 1, 1, 1
    ]

ture_label = [
    7, 2, 1, 0, 4, 1, 4, 9, 5, 9,
    0, 6, 9, 0, 1, 5, 9, 7, 3, 4,
    9, 6, 6, 5, 4, 0, 7, 4, 0, 1,
    3, 1, 3, 4, 7, 2, 7, 1, 2, 1
    ]

For example, the above is the predicted label obtained by the model and the real label of the item. The method of testing the accuracy is to compare the predicted label and the real label one by one. If it is correct, add one to the correct number, and do nothing if it is wrong. Finally get the correct number, and then divide the correct number by the total to get the correct rate.

model_pred = [
    7, 0, 1, 0, 4, 1, 9, 4, 1, 9,
    0, 1, 9, 0, 1, 9, 7, 7, 2, 4,
    9, 6, 1, 5, 4, 0, 7, 4, 0, 1,
    3, 1, 3, 4, 9, 5, 7, 1, 1, 1
    ]

ture_label = [
    7, 2, 1, 0, 4, 1, 4, 9, 5, 9,
    0, 6, 9, 0, 1, 5, 9, 7, 3, 4,
    9, 6, 6, 5, 4, 0, 7, 4, 0, 1,
    3, 1, 3, 4, 7, 2, 7, 1, 2, 1
    ]

acc = 0
for i in range(len(model_pred)):
    if model_pred[i] == ture_label[i]:
        acc += 1

acc_rate = acc/len(model_pred)
print('acc_rate =', acc_rate)
print('error_rate =', 1-acc_rate)

Of course, accuracy rate and error rate are antonyms, so the error rate is equal to 1 minus the accuracy rate. Run the program, you can get the following results:

acc_rate = 0.7
error_rate = 0.30000000000000004
>>> 

Of course, python provides functions for calculating accuracy and error rates, we only need to call them. For details, please refer to the confusion matrix in the next chapter.

Statistics

Start time of this page: December 26, 2021

Completion time of this page: December 26, 2021

PreviousMulti-class SVMNextConfusion matrix & Accuracy, Precision, Recall

Last updated 3 years ago

3️⃣
Page cover image