機器學習-無監督學習(人臉識別,使用NMF進行特徵提取)

weixin_34208283發表於2018-05-08

一、非監督學習
1、概念
輸入的基礎樣本資料,經過演算法能等到輸出結果無法預測或者多種結果
2、分類
資料集變換
概念
和原有的資料集比較,有變化
應用
降維:資料集的特徵變少
聚類
概念
將資料集分不同按照組分類
二、降維,特徵提取和流形學習
1、演算法
主成分分析(PCA)-降維
非負矩陣分解 -特徵提取
T_SNE-二維散點圖的視覺化

臉部識別示例:

import matplotlib.pyplot as plt
import numpy as np
from sklearn.datasets import fetch_lfw_people
from sklearn.decomposition import NMF
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier

people = fetch_lfw_people(min_faces_per_person=20, resize=0.7)
mask = np.zeros(people.target.shape, dtype=np.bool)
image_shape = people.images[0].shape

for target in np.unique(people.target):
mask[np.where(people.target == target)[0][:50]] = 1

X_people = people.data[mask]
y_people = people.target[mask]
X_people = X_people/255

X_train, X_test, y_train, y_test = train_test_split(X_people, y_people, stratify=y_people, random_state=0)
knn = KNeighborsClassifier(n_neighbors=1)
knn.fit(X_train, y_train)

print("Test score of 1-nn:{:2f}".format(knn.score(X_test, y_test)))

nmf = NMF(n_components=15, random_state=0)
nmf.fit(X_train)
X_train_nmf=nmf.transform(X_train)
X_test_nmf=nmf.transform(X_test)

fix, axes = plt.subplots(3, 5, figsize=(15, 12), subplot_kw={'xticks': (), 'yticks': ()})

for i, (component, ax) in enumerate(zip(nmf.components_, axes.ravel())):
ax.imshow(component.reshape(image_shape), cmap='viridis')
ax.set_title("{}.component".format(i))

相關文章