rbf-svm在lfw上demo

from matplotlib import pyplot as plt
from sklearn.datasets import fetch_lfw_people
from sklearn.svm import SVC
from sklearn.decomposition import PCA
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
import time

faces=fetch_lfw_people(min_faces_per_person=60) #一个人人脸图像数少于60就不作为样本

print(faces.target_names)

print(faces.images.shape)

fig,ax=plt.subplots(3,5)
for i, axi in enumerate(ax.flat):
axi.imshow(faces.images[i],cmap=“bone”)
axi.set(xticks=[],yticks=[],xlabel=faces.target_names[faces.target[i]])

#每个图62*47,需要降维,降到150维

pca=PCA(n_components=150,whiten=True,random_state=42) #random_state就是随机种子,控制每次随机结果一样
svc=SVC(kernel=“rbf”,class_weight=“balanced”)
model=make_pipeline(pca,svc)

#在sklearn中构造训练集和验证集
Xtrain,Xtest,ytrain,ytest=train_test_split(faces.data,faces.target,random_state=40)

#用GridSearchCV来寻找参数,c,gamma,给出几个值,遍历
param_grid={“svc__C”:[2,3,4],
“svc__gamma”:[0.0015,0.0017,0.0019]

}
grid=GridSearchCV(model,param_grid)
%time grid.fit(Xtrain,ytrain)
print(grid.best_params_)

model=grid.best_estimator_
yfit=model.predict(Xtest)
yfit.shape

#画出结果
fig, ax=plt.subplots(4,6)
for i, axi in enumerate(ax.flat):
axi.imshow(Xtest[i].reshape(62,47),cmap=“bone”)
axi.set(xticks=[],yticks=[])
axi.set_ylabel(faces.target_names[yfit[i]].split()[-1],
color=“black” if yfit[i]==ytest[i] else “red”
)
fig.suptitle(“Predicted names;Incorrect labels in red”,size=18)

rbf-svm在lfw上demo_第1张图片

你可能感兴趣的:(初学idea,everyday)