机器学习-无监督学习(人脸识别,使用NMF进行特征提取)
一、非监督学习
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))