python svm pca实践二
继上一片的内容,这片来·讲一下sklearn来进行简单的人脸识别,这里用的方法是pca和svm
先导入必要的包和数据集
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
from sklearn.decomposition import PCA
from sklearn.svm import SVC
from sklearn import datasets
lfw_people = datasets.fetch_lfw_people(min_faces_per_person=70, \
resize=0.4)
sklearn的人脸数据集包含5千多个不同人的人脸,但有些人的人脸只包含一张,
n_samples, h, w = lfw_people.images.shape
print('height and width of images:', h, w)
# The images in X have been collapsed into a 1D array
# just like for the handwritten digits
X = lfw_people.data
# X.shape[0] tells you the number of images (faces);
# this is the same as n_samples ahove
# X.shape[1] gives the number of pixels for each image
# or, "features"
print('X.shape', X.shape)
n_features = X.shape[1]
# the label/target to predict is the id of the person -- y is an integer
y = lfw_people.target
# target_names are actually names
target_names = lfw_people.target_names
print('target_names.shape', target_names.shape)
print('target_names', target_names)
# n_classes gives the number of people
# Different from the number of faces (n_samples)!!
n_classes = target_names.shape[0]
print("Total dataset size:")
print("n_samples (number of faces): {0}".format(n_samples))
# n_features = 1850, which is 50x37, the dimension of the images.
print("n_features (number of pixels): {0}".format(n_features))
print("n_classes (number of people): {0}".format(n_classes))
通过打印可以看到数据集人脸的尺寸为50x37,为7类共1288张人脸
pca = PCA(n_components=4,whiten = True)
X_proj = pca.fit_transform(X[:500])
print("eigen vector",pca.components_)
print("...")
print('eigen value', pca.explained_variance_[:2])
print(np.var(X_proj[:,0]))
print(np.var(X_proj[:,1]))
取500组数据将其降维为4个维度,并进行归一化处理
explained_variance_,它代表降维后的各主成分的方差值。方差值越大,则说明越是重要的主成分
from sklearn import svm
def plot_faces(n_features):
# nside = 1
X = lfw_people.data
# fig, axes = plt.subplots(nside, nside, figsize=(8, 8))
plt.imshow(X[5].reshape(50,37))
plot_faces(n_features= 16)
plt.show()
试着打一下其中的一幅图片
Xtrain = lfw_people.data[:1000]
Xtest = lfw_people.data[1000:,]
ytrain = lfw_people.target[:1000]
ytest = lfw_people.target[1000:,]
# Xtest = X[select_idx].reshape(1, -1)
# test_img = X[select_idx]
# ytest = y[select_idx]
#
n_comp = 50
pca = PCA(n_comp, whiten = True)
pca.fit(Xtrain)
# pca.fit(Xtest)
Xtrain_proj = pca.transform(Xtrain)
# projecting test data onto pca axes
Xtest_proj = pca.transform(Xtest)
print(Xtrain_proj.shape)
print(Xtest_proj.shape)
# ************************************* The SVM Section ********************************
# instantiating an SVM classifier
clf = svm.SVC(gamma=0.001, C=100.)
# apply SVM to training data and draw boundaries.
clf.fit(Xtrain_proj, ytrain)
# Use SVM-determined boundaries to make
# a prediction for the test data point.
ypred = clf.predict(Xtest_proj)
correct = np.sum(ytest == ypred)
print(correct/288*100)
接下来之前载入的数据用pca和svm进行训练识别,在1288个数据中取前1000组为训练集,后288个为测试集,pca将维为50维,并用训练集训练的模型对测试集进行预测,最后的测试精度为:81.25%,相对于现状流行的深度学习来说精度还是差了一点。

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
Go基础(复杂类型):指针
Go语言指针 Go 具有指针。 指针保存了变量的内存地址。 类型 *T 是指向类型 T 的值的指针。其零值是 nil。 var p *int & 符号会生成一个指向其作用对象的指针。 i := 42 p = &i * 符号表示指针指向的底层的值。 fmt.Println(*p) // 通过指针 p 读取 i *p = 21 // 通过指针 p 设置 i 这也就是通常所说的“间接引用”或“非直接引用”。 与 C 不同,Go 没有指针运算。 例如以下的例子: package main import "fmt" func main() { //定义一个变量i等于42 i := 42 //方式一 //&i指针,生成一个指针,指向作用对象i p := &i //*T符号表示指针指向的底层的值 fmt.Println(*p) //通过这个指针去操作i,去改变i的值 *p = 21 fmt.Println(i) //定义一个变量j,值为2701 j := 2701 //方式二 //&i指针,生成一个指针,指向作用对象i //*p(除后的值) = *p(2701...
-
下一篇
leetcode算法题解(Java版)-15-动态规划(斐波那契)
一、二叉树遍历 题目描述 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. 思路 两种思路,都是递归。第一种是递归的判断每个节点的左右子树的深度是否只相差一以内。第二种做了剪枝处理,当判断到一个子树已经不满足时就返回结果。 代码 //思路一 /** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public boolean isBalan...
相关文章
文章评论
共有0条评论来说两句吧...