半監督式分類法 Semi-Supervised Classification

半監督式分類法/範例3 : Label Propagation digits: Demonstrating performance

本範例目的:

  • 利用少量標籤的手寫數字資料集進行模型訓練,展現半監督式學習的能力

一、半監督式學習

在實際的應用上,大部分的資料沒有標籤且數量會遠多於有標籤的資料,而將這些沒有標籤的資料一一標籤是非常耗時的,相對而言,蒐集無標籤的資料更容易,因此可以利用半監督式學習(Semi-supervised learning)對少部分的資料進行標籤,透過這些有標籤的資料擷取特徵,然後再對其他資料進行分類。

二、引入函式與模型

  • stats用來進行統計與分析

  • LabelSpreading為半監督式學習的模型

  • confusion_matrix為混淆矩陣

  • classification_report用於觀察預測和實際數值的差異,包含precision、recall、f1-score及support

import numpy as np
import matplotlib.pyplot as plt

from scipy import stats
from sklearn import datasets
from sklearn.semi_supervised import LabelSpreading
from sklearn.metrics import confusion_matrix, classification_report

三、建立dataset

  • Dataset取自sklearn.datasets.load_digits,內容為0~9的手寫數字,共有1797筆

  • 使用其中的340筆進行訓練,其中40筆為labeled,其餘為unlabeled

  • 複製一組340筆的target (y_train)作為訓練集,並將第40筆之後的label都設為-1

四、模型訓練與預測

  • 利用訓練過後的模型進行預測,得到predicted_labels,並與true_labels計算混淆矩陣

  • 列出classification report

  • support為每個標籤出現的次數

  • precision(精確度)為true positives/(true positivies + false positivies)

  • recall(召回率)為true positivies/(true positivies + false negatives)

  • f1值為精確度與召回率的調和均值,為2 x precision x recall/(precision + recall)

  • micro avg為所有數據中,正確預測的比率

  • macro avg為每個評估項目未加權的平均值

  • weighted avg為每個評估項目加權平均值

Out:

五、結果觀察與分析

  • 利用stats進行數據的統計,並找出前10筆預測結果最不佳的結果

png

六、原始碼列表

Python source code: plot_label_propagation_digits.py

https://scikit-learn.org/stable/auto_examples/semi_supervised/plot_label_propagation_digits.html

Last updated