0
2

Scikit-Learnには有名人の顔データを集めたデータがあります。今回はそれを使ってLightGBMで顔認識をして精度を見てみようと思います。

基本的なプログラム

from sklearn.datasets import fetch_lfw_people
from lightgbm import LGBMClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, confusion_matrix

faces = fetch_lfw_people(min_faces_per_person=60)
x = faces.data
y = faces.target

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3)

model = LGBMClassifier()
model.fit(x_train, y_train)
y_pred = model.predict(x_test)

精度指標とラベル名をデータフレーム化する関数

targetが数値である以上誰と誰を間違えたか、誰が精度高いかが分かりませんのでそこを分かるようにします。

import pandas as pd
def classification_report_dataframe(y, y_pred, label=None):
    rep = classification_report(y, y_pred, output_dict=True)
    dst = []
    index = []
    for col in rep:
        if col == "accuracy":
            index.append(col)
            dst.append(["", "", rep["accuracy"], rep["weighted avg"]["support"]])
        elif col != "accuracy":
            dst.append([rep[col]["precision"], rep[col]["recall"], rep[col]["f1-score"],rep[col]["support"]])
            if label == None:
                index.append(col)
            elif label != None:
                try:
                    index.append(label[col])
                except:
                    index.append(col)
        
    df_rep = pd.DataFrame(dst)
    df_rep.index = index
    df_rep.columns = ["precision", "recall", "f1-score", "support"]
    return df_rep, index

精度を見る

y_list = list(set(y))
lists = {}
for i in y_list:
    lists[str(i)] = faces.target_names[i]
df_rep, index = classification_report_dataframe(y_test, y_pred, label=lists)
df_rep

image.png

Ariel SharonとGerhard Schroederの精度が芳しくない事が分かります。

混合行列で間違え方を見る

df_cm = pd.DataFrame(confusion_matrix(y_test, y_pred))
df_cm.index = index[0:7]
df_cm.columns = index[0:7]
df_cm

image.png

この結果から精度の悪かったAriel SharonはColln PowellとGeorge W Bushに間違いやすく、Gerhard SchroederはGeorge W Bushと間違いやすいそうです。

まとめ

こういうのはCNNでやりましょ

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2