機器學習概念及分類識別
一、閱讀“機器學習”(周志華著)第二章“模型評估與旋轉”,理解“查準率”、“查全率”、“F1-Score”、“ROC”、“混淆矩陣”的定義。
1.查準率定義
查準率(Precision)(精度)是衡量某一檢索系統的信號噪聲比的一種指標,即檢出的相關文獻與檢出的全部文獻的百分比
2.查全率定義
查全率(召回率,recall),是衡量某一檢索系統從文獻集合中檢出相關文獻成功度的一項指標,即檢出的相關文獻與全部相關文獻的百分比。
3.F1-Score定義
F1分數(F1 Score),是統計學中用來衡量二分類模型精確度的一種指標。它同時兼顧了分類模型的精確率和召回率。F1分數可以看作是模型精確率和召回率的一種加權平均,它的最大值是1,最小值是0。
數學定義:F1分數(F1-Score),又稱爲平衡F分數(BalancedScore),它被定義爲精確率和召回率的調和平均數。
4.ROC定義
ROC的全名叫做Receiver Operating Characteristic,其主要分析工具是一個畫在二維平面上的曲線——ROC 曲線。平面的橫座標是false positive rate(FPR),縱座標是true positive rate(TPR)。對某個分類器而言,我們可以根據其在測試樣本上的表現得到一個TPR和FPR點對。這樣,此分類器就可以映射成ROC平面上的一個點。調整這個分類器分類時候使用的閾值,我們就可以得到一個經過(0, 0),(1, 1)的曲線,這就是此分類器的ROC曲線。一般情況下,這個曲線都應該處於(0, 0)和(1, 1)連線的上方。
5.混淆矩陣定義
混淆矩陣也稱誤差矩陣,是表示精度評價的一種標準格式,用n行n列的矩陣形式來表示。具體評價指標有總體精度、製圖精度、用戶精度等,這些精度指標從不同的側面反映了圖像分類的精度。
在人工智能中,混淆矩陣(confusion matrix)是可視化工具,特別用於監督學習,在無監督學習一般叫做匹配矩陣。
在圖像精度評價中,主要用於比較分類結果和實際測得值,可以把分類結果的精度顯示在一個混淆矩陣裏面。混淆矩陣是通過將每個實測像元的位置和分類與分類圖像中的相應位置和分類相比較計算的。
二、學習“機器學習實戰”第三章-分類器,Jupyter編程完成對手寫體Mnist數據集中10個字符 (0-9)的分類識別
1.使用sklearn的函數來獲取MNIST數據集
# 使用sklearn的函數來獲取MNIST數據集
from sklearn.datasets import fetch_openml
import numpy as np
import os
import datetime
# to make this notebook's output stable across runs
np.random.seed(42)
# To plot pretty figures
%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rc('axes', labelsize=14)
mpl.rc('xtick', labelsize=12)
mpl.rc('ytick', labelsize=12)
# 爲了顯示中文
mpl.rcParams['font.sans-serif'] = [u'SimHei']
mpl.rcParams['axes.unicode_minus'] = False
# 耗時巨大
def sort_by_target(mnist):
reorder_train=np.array(sorted([(target,i) for i, target in enumerate(mnist.target[:60000])]))[:,1]
reorder_test=np.array(sorted([(target,i) for i, target in enumerate(mnist.target[60000:])]))[:,1]
mnist.data[:60000]=mnist.data[reorder_train]
mnist.target[:60000]=mnist.target[reorder_train]
mnist.data[60000:]=mnist.data[reorder_test+60000]
mnist.target[60000:]=mnist.target[reorder_test+60000]
2.由於sklearn獲取數據集所需時間較長,我們可以把這個程序運行時間計算出來,運行速度取決於電腦的性能。
starttime=datetime.datetime.now()
mnist=fetch_openml('mnist_784',version=1,cache=True)
mnist.target=mnist.target.astype(np.int8)
sort_by_target(mnist)
endtime=datetime.datetime.now()
print("運行時間:",endtime-starttime)
運行結果:
3.數據集排序,生成數據集矩陣
mnist["data"], mnist["target"]
運行結果:
4.查看數據集維度
方法一:
mnist.data.shape
運行結果:
方法二:
X,y=mnist["data"],mnist["target"]
X.shape
運行結果:
方法三:
y.shape
28*28
5.展示圖片
# 展示圖片
def plot_digit(data):
image = data.reshape(28, 28)
plt.imshow(image, cmap = mpl.cm.binary,
interpolation="nearest")
plt.axis("off")
some_digit = X[36000]
plot_digit(X[36000].reshape(28,28))
運行結果:
y[36000]
運行結果:
說明上圖的的數字爲5
6.完成對手寫體Mnist數據集中10個字符 (0-9)的分類識別
# 更好看的圖片展示
def plot_digits(instances,images_per_row=10,**options):
size=28
# 每一行有一個
image_pre_row=min(len(instances),images_per_row)
images=[instances.reshape(size,size) for instances in instances]
# 有幾行
n_rows=(len(instances)-1) // image_pre_row+1
row_images=[]
n_empty=n_rows*image_pre_row-len(instances)
images.append(np.zeros((size,size*n_empty)))
for row in range(n_rows):
# 每一次添加一行
rimages=images[row*image_pre_row:(row+1)*image_pre_row]
# 對添加的每一行的額圖片左右連接
row_images.append(np.concatenate(rimages,axis=1))
# 對添加的每一列圖片 上下連接
image=np.concatenate(row_images,axis=0)
plt.imshow(image,cmap=mpl.cm.binary,**options)
plt.axis("off")
plt.figure(figsize=(9,9))
example_images=np.r_[X[:12000:600],X[13000:30600:600],X[30600:60000:590]]
plot_digits(example_images,images_per_row=10)
plt.show()
運行結果: