圖形識別 - 利用SVM進行人臉辨識預測並使用PCA&LDA進行降維
===
github:https://github.com/Hank860809/Base-on-SVM-on-Face-Recognize-with-PCA-LDA-Dimension-Reduction
## 一、資料來源
人臉數據庫:https://mitlab.ndhu.edu.tw/~ccchiang/PR/att_faces.zip
資料夾內共有40個資料夾 每個資料分別為獨立的一個人
檔案格式為92*112 = 10304個像素點 檔案為pgm格式
每人分別為十張圖片編號為1~10 總共具有400個pgm檔案
## 二、資料預處理
我將使用matplotlib.pyplot取得pgm的灰度值
並將每個資料夾內的1~5的圖片作為訓練用圖片
6~10作為測試圖片
```python
import matplotlib.pyplot as plt
# 資料預處理:將資料1~5當成訓練資料用的圖片,6~10作為訓練用的圖片
for i in range(1,41):
for j in range(1,11):
if j <= 5:
train_data.append(plt.imread('att_faces/s{}/{}.pgm'.format(i,j), -1))
train_true.append(i)
else:
test_data.append(plt.imread('att_faces/s{}/{}.pgm'.format(i,j), -1))
test_true.append(i)
```
將資料轉換為1*10304的矩陣
```python=
train_data = []
test_data = []
train_true = [] # 真實訓練資料標籤
test_true = [] # 真實訓練資料標籤
train_img = [] # 圖片訓練集
test_img = [] # 圖片測試集
# 將訓練資料轉成一維陣列
for image in train_data:
img_to_1D = []
for x in range(image.shape[0]):
for y in range(image.shape[1]):
img_to_1D.append(image[x][y])
train_img.append(img_to_1D)
# 將測試資料轉成一維
for image in test_data:
img_to_1D = []
for x in range(image.shape[0]):
for y in range(image.shape[1]):
img_to_1D.append(image[x][y])
test_img.append(img_to_1D)
# 轉成陣列形式
train_img = np.array(train_img)
test_img = np.array(test_img)
train_true = np.array(train_true)
test_true = np.array(test_true)
```
## 三、主程式
### 使用到的套件
```python=
import numpy as np
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.svm import SVC
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
```
### 顯示圖片函式
方便看降維後逆轉換的圖片結果
```python=
def show_img(figure_num, n,img_set):
fig = plt.figure(figure_num)
for i in range(n):
tmp_img = img_set[i * 5].reshape(112,92)
plt.subplot(int(np.ceil(n/5)),5,i+1)
plt.imshow(tmp_img,cmap='gray')
plt.show()
```
### 完整程式碼
```python=
# Hackmd:https://hackmd.io/9X_MuHSERz-_sfctp7ThJw
# github:https://github.com/Hank860809/Base-on-SVM-on-Face-Recognize-with-PCA-LDA-Dimension-Reduction
import numpy as np
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.svm import SVC
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
def show_img(figure_num, n,img_set):
fig = plt.figure(figure_num)
for i in range(n):
tmp_img = img_set[i * 5].reshape(112,92)
plt.subplot(int(np.ceil(n/5)),5,i+1)
plt.imshow(tmp_img,cmap='gray')
plt.show()
train_data = []
test_data = []
train_true = [] # 真實訓練資料標籤
test_true = [] # 真實測試資料標籤
train_img = [] # 訓練集
test_img = [] # 測試集
# 資料預處理:將資料1~5當成訓練資料用的圖片,6~10作為訓練用的圖片
for i in range(1,41):
for j in range(1,11):
if j <= 5:
train_data.append(plt.imread('att_faces/s{}/{}.pgm'.format(i,j), -1))
train_true.append(i)
else:
test_data.append(plt.imread('att_faces/s{}/{}.pgm'.format(i,j), -1))
test_true.append(i)
# 將訓練資料轉成一維陣列
for image in train_data:
img_to_1D = []
for x in range(image.shape[0]):
for y in range(image.shape[1]):
img_to_1D.append(image[x][y])
train_img.append(img_to_1D)
# 將測試資料轉成一維
for image in test_data:
img_to_1D = []
for x in range(image.shape[0]):
for y in range(image.shape[1]):
img_to_1D.append(image[x][y])
test_img.append(img_to_1D)
# 轉成陣列形式
train_img = np.array(train_img)
test_img = np.array(test_img)
train_true = np.array(train_true)
test_true = np.array(test_true)
# param_grid 為GridSearchCV 預設參數
param_grid = {'C': [1e3, 5e3, 1e4, 5e4, 1e5],
'gamma': [0.0001, 0.0005, 0.001, 0.005, 0.01, 0.1], }
pca_clf = GridSearchCV(SVC(kernel='linear', class_weight='balanced'),param_grid)
lda_clf = GridSearchCV(SVC(kernel='linear', class_weight='balanced'),param_grid)
# PCA 10,20,30,40,50維訓練
for i in range(10,51,10):
print()
print('第{}維:'.format(i))
myPCA = PCA(n_components=i).fit(train_img)
pca_train_img = myPCA.transform(train_img)
pca_test_img = myPCA.transform(test_img)
pca_pred = pca_clf.fit(pca_train_img, train_true)
pca_test_pred = pca_pred.predict(pca_test_img)
pca_result = confusion_matrix(test_true, pca_test_pred)
# 準確率
pca_accuracy = str(accuracy_score(pca_test_pred,test_true)*100)+"%"
pca_report = classification_report(test_true,pca_test_pred)
# 列印混合矩陣
# print('PCA混淆矩陣:')
# for j in range(len(pca_result)):
# for k in range(len(pca_result[j])):
# print(pca_result[j][k],end=" ")
# print()
# 計算正確預測個數
true_count = 0
for j in range(len(pca_result)):
true_count += pca_result[j][j]
# 錯誤預測個數
false_count = 200 - true_count
# 統計資料
print('PCA正確預測個數:{} 錯誤預測個數:{} 準確率:{}'.format(true_count,false_count,pca_accuracy))
# 列印混合矩陣
print('PCA混淆矩陣:')
print(pca_result)
print('PCA報告:')
print(pca_report)
# 將降為後的資料進行的逆轉換成原本資料
# data_inverse = myPCA.inverse_transform(pca_test_img)
# show_img(1,20,data_inverse)
# 因為LDA模型在SKlearn 維度只接受到0~min(n_features, n_classes-1)之間的維度,因為我們只有40個類別,因此最大維度只能到39
if(i<40):
myLDA = LDA(n_components=i).fit(pca_train_img, train_true)
lda_train_img = myLDA.transform(pca_train_img)
lda_test_img = myLDA.transform(pca_test_img)
lda_pred = lda_clf.fit(lda_train_img, train_true)
lda_test_pred = lda_pred.predict(lda_test_img)
lda_result = confusion_matrix(test_true, lda_test_pred)
# 準確率
lda_accuracy = str(accuracy_score(lda_test_pred, test_true) * 100) + "%"
lda_report = classification_report(test_true,lda_test_pred)
# 計算正確預測個數
true_count = 0
for j in range(len(lda_result)):
true_count += lda_result[j][j]
# 錯誤預測個數
false_count = 200 - true_count
# 統計資料
print('LDA正確預測個數:{} 錯誤預測個數:{} 準確率:{}'.format(true_count, false_count, lda_accuracy))
# 列印混合矩陣
print('LDA混淆矩陣:')
print(lda_result)
print('LDA報告:')
print(lda_report)
```
## 四、預測結果
第10維:
PCA正確預測個數:173 錯誤預測個數:27 準確率:86.5%
PCA混淆矩陣:
[[4 0 0 ... 0 0 0]
[0 5 0 ... 0 0 0]
[0 0 5 ... 0 0 0]
...
[0 0 0 ... 5 0 0]
[0 0 0 ... 0 5 0]
[0 0 0 ... 0 0 5]]
PCA報告:
precision recall f1-score support
1 1.00 0.80 0.89 5
2 0.62 1.00 0.77 5
3 1.00 1.00 1.00 5
4 0.83 1.00 0.91 5
5 1.00 0.80 0.89 5
6 1.00 1.00 1.00 5
7 1.00 1.00 1.00 5
8 1.00 1.00 1.00 5
9 1.00 1.00 1.00 5
10 1.00 0.80 0.89 5
11 0.71 1.00 0.83 5
12 1.00 1.00 1.00 5
13 1.00 1.00 1.00 5
14 1.00 0.20 0.33 5
15 0.83 1.00 0.91 5
16 0.83 1.00 0.91 5
17 0.00 0.00 0.00 5
18 0.83 1.00 0.91 5
19 1.00 0.80 0.89 5
20 1.00 1.00 1.00 5
21 1.00 1.00 1.00 5
22 1.00 1.00 1.00 5
23 1.00 1.00 1.00 5
24 0.83 1.00 0.91 5
25 0.71 1.00 0.83 5
26 1.00 1.00 1.00 5
27 1.00 0.20 0.33 5
28 0.50 0.60 0.55 5
29 1.00 1.00 1.00 5
30 1.00 1.00 1.00 5
31 1.00 1.00 1.00 5
32 1.00 0.40 0.57 5
33 1.00 1.00 1.00 5
34 1.00 1.00 1.00 5
35 1.00 0.40 0.57 5
36 0.38 0.60 0.46 5
37 0.83 1.00 0.91 5
38 0.83 1.00 0.91 5
39 1.00 1.00 1.00 5
40 1.00 1.00 1.00 5
accuracy 0.86 200
macro avg 0.89 0.86 0.85 200
weighted avg 0.89 0.86 0.85 200
------------------------------------------------------------
LDA正確預測個數:177 錯誤預測個數:23 準確率:88.5%
LDA混淆矩陣:
[[4 0 0 ... 0 0 0]
[0 5 0 ... 0 0 0]
[0 0 5 ... 0 0 0]
...
[0 0 0 ... 5 0 0]
[0 0 0 ... 0 5 0]
[0 0 0 ... 0 0 5]]
LDA報告:
precision recall f1-score support
1 1.00 0.80 0.89 5
2 1.00 1.00 1.00 5
3 1.00 1.00 1.00 5
4 1.00 1.00 1.00 5
5 1.00 1.00 1.00 5
6 1.00 1.00 1.00 5
7 0.83 1.00 0.91 5
8 0.83 1.00 0.91 5
9 1.00 1.00 1.00 5
10 1.00 0.60 0.75 5
11 0.83 1.00 0.91 5
12 1.00 1.00 1.00 5
13 1.00 1.00 1.00 5
14 1.00 0.20 0.33 5
15 0.83 1.00 0.91 5
16 0.83 1.00 0.91 5
17 0.00 0.00 0.00 5
18 1.00 1.00 1.00 5
19 1.00 1.00 1.00 5
20 1.00 0.80 0.89 5
21 1.00 1.00 1.00 5
22 1.00 1.00 1.00 5
23 1.00 1.00 1.00 5
24 1.00 1.00 1.00 5
25 0.83 1.00 0.91 5
26 1.00 0.80 0.89 5
27 1.00 0.40 0.57 5
28 0.43 0.60 0.50 5
29 0.83 1.00 0.91 5
30 1.00 1.00 1.00 5
31 1.00 1.00 1.00 5
32 1.00 0.60 0.75 5
33 1.00 1.00 1.00 5
34 1.00 1.00 1.00 5
35 1.00 0.80 0.89 5
36 0.50 0.80 0.62 5
37 0.83 1.00 0.91 5
38 0.83 1.00 0.91 5
39 1.00 1.00 1.00 5
40 1.00 1.00 1.00 5
accuracy 0.89 200
macro avg 0.91 0.89 0.88 200
weighted avg 0.91 0.89 0.88 200
================================================================
第20維:
PCA正確預測個數:173 錯誤預測個數:27 準確率:86.5%
PCA混淆矩陣:
[[5 0 0 ... 0 0 0]
[0 5 0 ... 0 0 0]
[0 0 4 ... 0 0 0]
...
[0 0 0 ... 5 0 0]
[0 0 0 ... 0 5 0]
[0 0 0 ... 0 0 5]]
PCA報告:
precision recall f1-score support
1 0.83 1.00 0.91 5
2 0.71 1.00 0.83 5
3 1.00 0.80 0.89 5
4 1.00 1.00 1.00 5
5 1.00 0.80 0.89 5
6 1.00 1.00 1.00 5
7 1.00 1.00 1.00 5
8 0.83 1.00 0.91 5
9 1.00 1.00 1.00 5
10 1.00 0.80 0.89 5
11 0.71 1.00 0.83 5
12 1.00 1.00 1.00 5
13 1.00 1.00 1.00 5
14 0.00 0.00 0.00 5
15 1.00 1.00 1.00 5
16 1.00 0.80 0.89 5
17 0.00 0.00 0.00 5
18 0.83 1.00 0.91 5
19 1.00 0.80 0.89 5
20 1.00 1.00 1.00 5
21 0.83 1.00 0.91 5
22 1.00 1.00 1.00 5
23 1.00 1.00 1.00 5
24 0.83 1.00 0.91 5
25 0.83 1.00 0.91 5
26 1.00 0.80 0.89 5
27 1.00 0.40 0.57 5
28 0.38 0.60 0.46 5
29 1.00 1.00 1.00 5
30 1.00 1.00 1.00 5
31 1.00 0.80 0.89 5
32 1.00 0.60 0.75 5
33 1.00 1.00 1.00 5
34 1.00 1.00 1.00 5
35 1.00 0.80 0.89 5
36 0.38 0.60 0.46 5
37 0.83 1.00 0.91 5
38 1.00 1.00 1.00 5
39 1.00 1.00 1.00 5
40 1.00 1.00 1.00 5
accuracy 0.86 200
macro avg 0.88 0.86 0.86 200
weighted avg 0.88 0.86 0.86 200
------------------------------------------------------------
LDA正確預測個數:173 錯誤預測個數:27 準確率:86.5%
LDA混淆矩陣:
[[5 0 0 ... 0 0 0]
[0 5 0 ... 0 0 0]
[0 0 5 ... 0 0 0]
...
[0 0 0 ... 5 0 0]
[0 0 0 ... 0 3 0]
[0 0 0 ... 0 0 5]]
LDA報告:
precision recall f1-score support
1 0.62 1.00 0.77 5
2 0.83 1.00 0.91 5
3 1.00 1.00 1.00 5
4 1.00 1.00 1.00 5
5 1.00 1.00 1.00 5
6 1.00 1.00 1.00 5
7 1.00 1.00 1.00 5
8 0.83 1.00 0.91 5
9 1.00 1.00 1.00 5
10 1.00 0.80 0.89 5
11 0.80 0.80 0.80 5
12 1.00 1.00 1.00 5
13 1.00 1.00 1.00 5
14 1.00 0.20 0.33 5
15 1.00 1.00 1.00 5
16 1.00 0.40 0.57 5
17 0.00 0.00 0.00 5
18 1.00 1.00 1.00 5
19 1.00 0.80 0.89 5
20 1.00 1.00 1.00 5
21 1.00 1.00 1.00 5
22 0.83 1.00 0.91 5
23 0.71 1.00 0.83 5
24 1.00 1.00 1.00 5
25 1.00 1.00 1.00 5
26 1.00 0.80 0.89 5
27 1.00 0.20 0.33 5
28 0.43 0.60 0.50 5
29 0.83 1.00 0.91 5
30 0.71 1.00 0.83 5
31 1.00 1.00 1.00 5
32 1.00 0.80 0.89 5
33 1.00 0.80 0.89 5
34 1.00 1.00 1.00 5
35 1.00 1.00 1.00 5
36 0.57 0.80 0.67 5
37 0.71 1.00 0.83 5
38 0.83 1.00 0.91 5
39 1.00 0.60 0.75 5
40 1.00 1.00 1.00 5
accuracy 0.86 200
macro avg 0.89 0.86 0.86 200
weighted avg 0.89 0.86 0.86 200
================================================================
第30維:
PCA正確預測個數:177 錯誤預測個數:23 準確率:88.5%
PCA混淆矩陣:
[[5 0 0 ... 0 0 0]
[0 5 0 ... 0 0 0]
[0 0 5 ... 0 0 0]
...
[0 0 0 ... 5 0 0]
[0 0 0 ... 0 5 0]
[0 0 0 ... 0 0 5]]
PCA報告:
precision recall f1-score support
1 1.00 1.00 1.00 5
2 0.83 1.00 0.91 5
3 1.00 1.00 1.00 5
4 1.00 1.00 1.00 5
5 1.00 0.80 0.89 5
6 1.00 1.00 1.00 5
7 1.00 1.00 1.00 5
8 0.83 1.00 0.91 5
9 1.00 1.00 1.00 5
10 1.00 0.80 0.89 5
11 0.71 1.00 0.83 5
12 1.00 1.00 1.00 5
13 1.00 1.00 1.00 5
14 1.00 0.20 0.33 5
15 0.83 1.00 0.91 5
16 1.00 1.00 1.00 5
17 0.00 0.00 0.00 5
18 0.83 1.00 0.91 5
19 1.00 0.80 0.89 5
20 1.00 1.00 1.00 5
21 0.83 1.00 0.91 5
22 1.00 1.00 1.00 5
23 1.00 1.00 1.00 5
24 0.83 1.00 0.91 5
25 1.00 1.00 1.00 5
26 1.00 0.80 0.89 5
27 1.00 0.40 0.57 5
28 0.43 0.60 0.50 5
29 1.00 1.00 1.00 5
30 1.00 1.00 1.00 5
31 1.00 0.80 0.89 5
32 1.00 0.60 0.75 5
33 1.00 1.00 1.00 5
34 1.00 1.00 1.00 5
35 1.00 1.00 1.00 5
36 0.38 0.60 0.46 5
37 0.83 1.00 0.91 5
38 1.00 1.00 1.00 5
39 1.00 1.00 1.00 5
40 1.00 1.00 1.00 5
accuracy 0.89 200
macro avg 0.91 0.89 0.88 200
weighted avg 0.91 0.89 0.88 200
------------------------------------------------------------
LDA正確預測個數:175 錯誤預測個數:25 準確率:87.5%
LDA混淆矩陣:
[[4 0 0 ... 0 0 0]
[0 5 0 ... 0 0 0]
[0 0 5 ... 0 0 0]
...
[0 0 0 ... 5 0 0]
[0 0 0 ... 0 4 0]
[0 0 0 ... 0 0 5]]
LDA報告:
precision recall f1-score support
1 0.80 0.80 0.80 5
2 0.83 1.00 0.91 5
3 1.00 1.00 1.00 5
4 1.00 1.00 1.00 5
5 1.00 1.00 1.00 5
6 1.00 1.00 1.00 5
7 1.00 0.80 0.89 5
8 1.00 1.00 1.00 5
9 1.00 1.00 1.00 5
10 1.00 1.00 1.00 5
11 1.00 0.80 0.89 5
12 1.00 1.00 1.00 5
13 1.00 1.00 1.00 5
14 1.00 0.20 0.33 5
15 0.83 1.00 0.91 5
16 1.00 0.80 0.89 5
17 0.00 0.00 0.00 5
18 1.00 1.00 1.00 5
19 1.00 0.80 0.89 5
20 1.00 0.80 0.89 5
21 0.83 1.00 0.91 5
22 0.83 1.00 0.91 5
23 0.83 1.00 0.91 5
24 1.00 1.00 1.00 5
25 1.00 1.00 1.00 5
26 1.00 1.00 1.00 5
27 1.00 0.20 0.33 5
28 0.57 0.80 0.67 5
29 0.83 1.00 0.91 5
30 0.71 1.00 0.83 5
31 1.00 0.80 0.89 5
32 1.00 0.80 0.89 5
33 1.00 0.80 0.89 5
34 1.00 1.00 1.00 5
35 1.00 1.00 1.00 5
36 0.50 0.80 0.62 5
37 0.83 1.00 0.91 5
38 0.83 1.00 0.91 5
39 1.00 0.80 0.89 5
40 1.00 1.00 1.00 5
accuracy 0.88 200
macro avg 0.91 0.88 0.87 200
weighted avg 0.91 0.88 0.87 200
================================================================
第40維:
PCA正確預測個數:178 錯誤預測個數:22 準確率:89.0%
PCA混淆矩陣:
[[5 0 0 ... 0 0 0]
[0 5 0 ... 0 0 0]
[0 0 5 ... 0 0 0]
...
[0 0 0 ... 5 0 0]
[0 0 0 ... 0 5 0]
[0 0 0 ... 0 0 5]]
PCA報告:
precision recall f1-score support
1 1.00 1.00 1.00 5
2 1.00 1.00 1.00 5
3 1.00 1.00 1.00 5
4 1.00 1.00 1.00 5
5 1.00 0.80 0.89 5
6 1.00 1.00 1.00 5
7 1.00 1.00 1.00 5
8 0.83 1.00 0.91 5
9 1.00 1.00 1.00 5
10 1.00 0.80 0.89 5
11 0.71 1.00 0.83 5
12 1.00 1.00 1.00 5
13 1.00 1.00 1.00 5
14 1.00 0.20 0.33 5
15 0.83 1.00 0.91 5
16 1.00 1.00 1.00 5
17 0.00 0.00 0.00 5
18 0.83 1.00 0.91 5
19 1.00 0.80 0.89 5
20 1.00 1.00 1.00 5
21 1.00 1.00 1.00 5
22 1.00 1.00 1.00 5
23 1.00 1.00 1.00 5
24 0.83 1.00 0.91 5
25 0.83 1.00 0.91 5
26 1.00 0.80 0.89 5
27 1.00 0.40 0.57 5
28 0.43 0.60 0.50 5
29 1.00 1.00 1.00 5
30 1.00 1.00 1.00 5
31 1.00 1.00 1.00 5
32 1.00 0.80 0.89 5
33 1.00 1.00 1.00 5
34 1.00 1.00 1.00 5
35 1.00 0.80 0.89 5
36 0.38 0.60 0.46 5
37 0.83 1.00 0.91 5
38 1.00 1.00 1.00 5
39 1.00 1.00 1.00 5
40 1.00 1.00 1.00 5
accuracy 0.89 200
macro avg 0.91 0.89 0.89 200
weighted avg 0.91 0.89 0.89 200
================================================================
第50維:
PCA正確預測個數:179 錯誤預測個數:21 準確率:89.5%
PCA混淆矩陣:
[[5 0 0 ... 0 0 0]
[0 5 0 ... 0 0 0]
[0 0 5 ... 0 0 0]
...
[0 0 0 ... 5 0 0]
[0 0 0 ... 0 5 0]
[0 0 0 ... 0 0 5]]
PCA報告:
precision recall f1-score support
1 1.00 1.00 1.00 5
2 1.00 1.00 1.00 5
3 1.00 1.00 1.00 5
4 1.00 1.00 1.00 5
5 1.00 0.80 0.89 5
6 1.00 1.00 1.00 5
7 1.00 1.00 1.00 5
8 0.83 1.00 0.91 5
9 1.00 1.00 1.00 5
10 1.00 0.80 0.89 5
11 0.71 1.00 0.83 5
12 1.00 1.00 1.00 5
13 1.00 1.00 1.00 5
14 1.00 0.20 0.33 5
15 0.83 1.00 0.91 5
16 1.00 1.00 1.00 5
17 0.00 0.00 0.00 5
18 0.83 1.00 0.91 5
19 1.00 0.80 0.89 5
20 1.00 1.00 1.00 5
21 1.00 1.00 1.00 5
22 1.00 1.00 1.00 5
23 1.00 1.00 1.00 5
24 0.83 1.00 0.91 5
25 1.00 1.00 1.00 5
26 1.00 0.80 0.89 5
27 1.00 0.40 0.57 5
28 0.43 0.60 0.50 5
29 1.00 1.00 1.00 5
30 1.00 1.00 1.00 5
31 1.00 1.00 1.00 5
32 1.00 0.80 0.89 5
33 1.00 1.00 1.00 5
34 1.00 1.00 1.00 5
35 1.00 1.00 1.00 5
36 0.38 0.60 0.46 5
37 0.83 1.00 0.91 5
38 1.00 1.00 1.00 5
39 1.00 1.00 1.00 5
40 1.00 1.00 1.00 5
accuracy 0.90 200
macro avg 0.92 0.89 0.89 200
weighted avg 0.92 0.90 0.89 200
## 五、 分析結果
分類評估(Classification Evaluation metrics)


#### 準確率(Accuracy):
Accuracy = (TP+TN)/(TP+FP+FN+TN)
意味著正確預測結果佔預測總數的比例
#### 精確度(Precision):
Precision = (TP)/(TP+FP)
精確度=正確預測的個數(TP)/被預測正確的個數(TP+FP)
意味著模型預測為正之中的結果有多少是預測正確的
#### 召回率(Recall):
Recall = (TP)/(TP+FN)
召回率=正確預測的個數(TP)/預測個數(TP+FN)
意味著正確預測為正在實際為正中的比例
#### F1 Score:

F-score的recall和precision之間存在著權衡的關係,可通過 β 調整更重視的部份。
但在這個實驗中我們沒有定義recall和precision的權重
因此 β 預設為 1
這邊的公式為:
F1 = 2*(精確度*召回率)/(精確度+召回率)
#### micro avg:
計算所有數據下的指標值
#### macro avg:
每個類別評估指標未加權的平均值
#### weighted avg:
加權平均,就是測試集中樣本量大的,我認爲它更重要,給他設置的權重大點。但因為在此實驗中每個樣本數量皆相同,因此可忽略不計
---
### PCA
10 dimension:
| | precision| recall| f1-score| support|
|-|-|-|-|-
| accuracy| | | 0.86| 200|
| macro avg| 0.89| 0.86| 0.85| 200|
20 dimension:
| | precision| recall| f1-score| support|
|------------|-----------|----------|----------|----------|
| accuracy| | | 0.86| 200|
| macro avg| 0.88| 0.86| 0.86| 200|
30 dimension:
| | precision| recall| f1-score| support|
|------------|-----------|----------|----------|----------|
| accuracy| | | 0.89| 200|
| macro avg| 0.91| 0.89| 0.88| 200|
40 dimension:
| | precision| recall| f1-score| support|
|------------|-----------|----------|----------|----------|
| accuracy| | | 0.89| 200|
| macro avg| 0.91| 0.89| 0.89| 200|
50 dimension:
| | precision| recall| f1-score| support|
|------------|-----------|----------|----------|----------|
| accuracy| | | 0.90| 200|
| macro avg| 0.92| 0.89| 0.89| 200|
---
### LDA
10 dimension:
| | precision| recall| f1-score| support|
|-|-|-|-|-|
| accuracy| | | 0.89| 200|
| macro avg| 0.91| 0.89| 0.88| 200|
20 dimension:
| | precision| recall| f1-score| support|
|-|-|-|-|-|
| accuracy| | | 0.86 | 200 |
| macro avg| 0.89 | 0.86 | 0.86 | 200 |
30 dimension:
| | precision| recall| f1-score| support|
|-|-|-|-|-|
| accuracy| | | 0.88| 200|
| macro avg| 0.91| 0.88| 0.87| 200|
## 六、 結論
從上面的結果可以看出PCA在越多維度時表現的最好
而LDA在10維度時分辨準確度最高(88.5%)
說明了灰度值依據不同的分群方式也能有不同的辨識結果