自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

終于把機器學習中的混淆矩陣搞懂了!

人工智能 機器學習
混淆矩陣是用于評估分類模型性能的表格。它通過將實際(真實)標簽與預測標簽進行比較,提供分類問題的預測結果摘要?;煜仃嚤旧硎钦叫危╪xn),其中 n 是模型中的類別數。

大家好,我是小寒

今天給大家分享一個機器學習中一個重要的概念,混淆矩陣

混淆矩陣是用于評估分類模型性能的表格。它通過將實際(真實)標簽與預測標簽進行比較,提供分類問題的預測結果摘要。

混淆矩陣本身是正方形(nxn),其中 n 是模型中的類別數。

對于二元分類問題,混淆矩陣由四個主要部分組成:

  • True Positive (TP, 真陽性):實際為正類,預測也為正類的數量。
  • True Negative (TN, 真陰性):實際為負類,預測也為負類的數量。
  • False Positive (FP, 假陽性):實際為負類,預測卻為正類的數量,通常稱為"Type I 錯誤"或"誤報"。
  • False Negative (FN, 假陰性):實際為正類,預測卻為負類的數量,通常稱為"Type II 錯誤"或"漏報"。

圖片圖片

為什么要使用混淆矩陣?

混淆矩陣是評估分類模型性能的基本工具。

  1. 錯誤分析
    它有助于識別模型所犯的錯誤類型,無論模型更容易出現假陽性還是假陰性,這在應用范圍內(例如在醫(yī)學診斷中)可能至關重要。
  2. 模型改進
    通過分析混淆矩陣,你可以專注于改進模型的特定方面,例如減少誤報或提高召回率。
  3. 類別不平衡處理
    在類別不平衡的情況下,一個類別出現的頻率高于另一個類別,單憑準確率可能會產生誤導。
    混淆矩陣可讓你更好地了解模型在每個類別中的表現。
  4. 性能指標計算

分類中的評估指標

1.準確率

準確率是分類任務中最簡單的評估指標之一,用來衡量模型預測正確的比例。

準確率的局限性

當處理不平衡的數據集時,一個類別的數量遠遠超過其他類別,準確率可能會產生誤導。

例如,在 95% 的樣本屬于同一類的數據集中,預測所有實例為多數類的模型的準確率為 95%,但在識別少數類時則無效。

import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import confusion_matrix, accuracy_score

# Example true labels (ytest) and predicted labels (ypred)
ytest = [0, 1, 0, 1, 0, 1, 0, 0, 1, 1]
ypred = [0, 1, 0, 0, 0, 1, 0, 1, 1, 1]

# Calculate confusion matrix
cm = confusion_matrix(ytest, ypred)

# Create a heatmap
plt.figure(figsize=(6, 4))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', cbar=False,
            xticklabels=['1', '0'],
            yticklabels=['1', '0'])

# Add labels and title
plt.xlabel('Predicted Classes')
plt.ylabel('Actual Classes')
plt.title('Confusion Matrix')

# Calculate and display accuracy
accuracy = accuracy_score(ytest, ypred)
plt.text(2.3, 1.5, f'Accuracy: {accuracy:.2f}', fontsize=14, color='black', weight='bold')

plt.show()

圖片圖片

2.精度

精度用來衡量模型預測為正類的樣本中實際為正類的比例。

import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import confusion_matrix, precision_score

# Example true labels (ytest) and predicted labels (ypred)
ytest = ['spam', 'spam', 'ham', 'spam', 'ham', 'spam', 'spam', 'ham', 'spam', 'spam', 'ham', 'spam', 'ham', 'ham', 'ham']
ypred = ['spam', 'spam', 'spam', 'spam', 'ham', 'spam', 'spam', 'ham', 'spam', 'spam', 'ham', 'ham', 'ham', 'ham', 'ham']

# Calculate the confusion matrix
cm = confusion_matrix(ytest, ypred, labels=['spam', 'ham'])
print("Confusion Matrix:\n", cm)

# Calculate precision
precision = precision_score(ytest, ypred, pos_label='spam')
print("Precision:", precision)

# Create a heatmap for the confusion matrix
plt.figure(figsize=(8, 6))
ax = sns.heatmap(cm, annot=True, fmt='d', cmap='viridis', cbar=False,
                 xticklabels=['Predicted Spam', 'Predicted Ham'],
                 yticklabels=['Actual Spam', 'Actual Ham'])

# Set labels and title
plt.xlabel('Predicted Classes')
plt.ylabel('Actual Classes')
plt.title(f'Confusion Matrix\nPrecision: {precision:.2f}')

# Show the plot
plt.show()

圖片圖片

3.召回率

召回率用來衡量實際為正類的樣本中模型預測為正類的比例。

import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import confusion_matrix, recall_score

# Example true labels (ytest) and predicted labels (ypred)
ytest = ['positive', 'positive', 'negative', 'positive', 'negative']
ypred = ['positive', 'negative', 'negative', 'positive', 'positive']

# Calculate the confusion matrix
cm = confusion_matrix(ytest, ypred, labels=['positive', 'negative'])


# Calculate recall
recall = recall_score(ytest, ypred, pos_label='positive')


# Create a heatmap for the confusion matrix
plt.figure(figsize=(6, 4))
ax = sns.heatmap(cm, annot=True, fmt='d', cmap='viridis', cbar=False,
                 xticklabels=['Predicted Positive', 'Predicted Negative'],
                 yticklabels=['Actual Positive', 'Actual Negative'])

# Set labels and title
plt.xlabel('Predicted Classes')
plt.ylabel('Actual Classes')
plt.title(f'Confusion Matrix\nRecall: {recall:.2f}')

# Show the plot
plt.show()

圖片圖片

4.F1-score

F1-score 是精度和召回率的調和平均數,用來綜合考慮精度和召回率的平衡。

責任編輯:武曉燕 來源: 程序員學長
相關推薦

2024-12-26 00:34:47

2024-10-28 15:52:38

機器學習特征工程數據集

2024-10-30 08:23:07

2024-10-08 15:09:17

2024-10-08 10:16:22

2025-01-15 11:25:35

2024-10-28 00:00:10

機器學習模型程度

2025-01-20 09:21:00

2024-11-05 12:56:06

機器學習函數MSE

2024-09-18 16:42:58

機器學習評估指標模型

2024-10-14 14:02:17

機器學習評估指標人工智能

2024-11-25 08:20:35

2025-01-20 09:00:00

2025-01-07 12:55:28

2025-02-17 13:09:59

深度學習模型壓縮量化

2024-07-17 09:32:19

2024-09-23 09:12:20

2025-03-03 01:50:00

深度學習微調遷移學習

2024-12-03 08:16:57

2024-10-16 07:58:48

點贊
收藏

51CTO技術棧公眾號