svm多分类__人脸识别

# -*-encoding: utf-8 -*-
"""
@version: 3.6
@time: 2018/4/16 22:45
@author: SunnyYang
"""
from __future__  import print_function

from time import time   #计算每个步骤花费多长时间
from matplotlib import pyplot as plt
import logging
from sklearn.model_selection import train_test_split
from sklearn.datasets import fetch_lfw_people
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.decomposition import RandomizedPCA
from sklearn.decomposition import PCA
from sklearn.svm import SVC
#打印输出日志信息
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO,filename='svmFaceRecongnize.log') #打印时间,日志级别以及日志信息

#数据采集
lfw_people = fetch_lfw_people(min_faces_per_person=70,resize=0.2)   #fetch_lfw_people()用来下载名人数据
#数据预处理
n_samples, h, w = lfw_people.images.shape   #数据集共有多少个样本,以及每一张图片的high 和width
print('共有 %s 个样本,height=%s,width=%s'%(n_samples,h,w))   #注意占位符的使用是用%()的形式,而不是每一个都用占位符
X = lfw_people.data  #data属性是取出所有特征值,每一行是一个样本,每一列是一个特征
# e = X[0:5,:] #切片操作取出矩阵的0到5行,和所有列
# print(e)
n_features = X.shape[1]  # 450个特征值,1表示的是列 0表示的是行数
n_sampless = X.shape[0]  #1288行 实例的个数
print('特征数量%s,样本数量%s'%(n_features,n_sampless))  #450,1288
y = lfw_people.target #每一个实例的类别标记
target_names = lfw_people.target_names  #提取不同人的身份,就是真实的名字是什么
n_class = target_names.shape[0] #总共有多少个人=多少类=多少行进行人脸识别
print(len(y))
print('总共有多少个人进行训练',n_class)
print('用于训练的人名称如下:',target_names)

#拆分成训练集和测试集
x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.25) #x_train,x_test是一个特征矩阵,y_train,y_test是对应的标记向量
#数据降维及模型训练
n_components = 150 #组成元素的数量,也就是降维后的维度
print('从%s个样本中抽取%s个样本:' %(x_train.shape[0],n_components))
t0 = time() #定义一个起始时间
pca = RandomizedPCA(n_components=n_components,whiten=True).fit(x_train)  #采用经典的pca主成分降维方法
# pca = PCA(svd_solver='ranomized',n_components=n_components,whiten=True).fit(x_train)  #
print('训练pca模型的时间%0.3fs:'%(time()-t0))
eigenfaces = pca.components_.reshape((n_components,h,w)) #从一张人脸上提取一些特征 ,对于人脸的一张照片上提取的特征值名为eigenfaces
x_train_pca = pca.transform(x_train) #将训练集中的所有特征值通过pca转换成低纬特征量
x_test_pca = pca.transform(x_test) #同理将测试集也降维

#核函数的构造,c惩罚因子也有人说是权重,gamma是特征值的对应的比例也就是有多少个特征点被使用 ,参数中共有6*6=36中不同的组合
param_grid = {'C':[1e3,5e3,1e4,5e4,1e5,5e5],
              'gamma':[0.0001,0.0005,0.01,0.05,0.1,0.5]
              }
clf = GridSearchCV( SVC(kernel='rbf',class_weight='balanced'),param_grid) #将参数的各种组合均放到svc中进行计算,最后看哪一组参数的效果好
clf = clf.fit(x_train_pca,y_train) #建模,找到使得边际最大的超平面
print(clf.best_estimator_) #打印输出最好的模型所对应的参数
#预测
y_pred = clf.predict(x_test_pca)
print('测试集的预测结果查看准确率',classification_report(y_test,y_pred,target_names=target_names)) #预测的准确率是多少
print(confusion_matrix(y_test,y_pred,labels=range(n_class))) #预测矩阵,对角线上的值越多,则说明预测的准确率越高,行和列分别是真实的和预测的标记,如果两者相等则值加一,所以对角线上的值是最有效的
#可视化预测结果和特征矩阵
def plot_galley(images,titles,h,w,n_row=3,n_col=4):
    plt.figure(figsize=(1.8 * n_col,2.5 * n_row))
    plt.subplots_adjust(bottom=0,left=.01,right=.99,top=.90,hspace=.35)
    for i in range(n_row * n_col):
        plt.subplot(n_row,n_col,i+1) #subplot()三个参数,分别是几行几列子图的位置在第几个
        plt.imshow(images[i].reshape((h,w)),cmap=plt.cm.gray) #imshow()展示的是一个矩阵images重新按照h,w来重构,reshape是np中的一个数组重构的方法,cmap=colormap
        plt.title(titles[i],size=12)
        plt.xticks(())  #x轴刻度
        plt.yticks(())  #y轴刻度

def title(y_pred,y_test,target_names,i):
    pre_name = target_names[y_pred[i]].rsplit(' ',1)[-1]   #预测的名字
    true_name = target_names[y_test[i]].rsplit(' ',1)[-1]   #真实的名字
    return 'predict %s\ntrue    %s'%(pre_name,true_name)    #

prediction_titles = [title(y_pred,y_test,target_names,i) for i in range(y_pred.shape[0])] #预测的人名字
plot_galley(x_test,prediction_titles,h,w)   #
eigenface_titles = ['eigenface %d '% i for i in range(eigenfaces.shape[0])]     #
plot_galley(eigenfaces,eigenface_titles,h,w)  #提取的特征向量组成的脸是什么样子的
plt.show()



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值