分类: Python/Ruby
2022-06-17 17:11:21
from sklearn.decomposition import PCA
import numpy as np
from sklearn.svm import SVC
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.decomposition import PCA
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression
from sklearn import datasets
from sklearn.model_selection import GridSearchCV
# 加载人脸数据 lfw->labled faces wild:野外标记的人脸
data = datasets.fetch_lfw_people(resize = 1, min_faces_per_person = 70)
data
# 进行数据的降维
pca = PCA(n_components=0.95)
X_pca = pca.fit_transform(X)
display(X.shape,X_pca.shape)
svc = SVC()
# C为惩罚系数(防止过拟合),kernel为核函数类型,tol为停止训练的误差值、精度
params = 跟单网gendan5.com{'C':np.logspace(-10,10,50),'kernel':['linear', 'poly', 'rbf', 'sigmoid'],'tol':[0.01,0.001,0.0001]}
gc = GridSearchCV(estimator = svc,param_grid = params,cv = 5)
gc.fit(X_pca,y)
gc.best_params_
svc = SVC(C = 1.8420699693267165e-07,kernel='linear',tol = 0.001)
# 随机划分的
X_pca_train,X_pca_test,y_train,y_test, faces_train,faces_test = train_test_split(X_pca,y,faces)
svc.fit(X_pca_train,y_train)
print('训练数据得分:',svc.score(X_pca_train,y_train))
print('测试数据的得分:',svc.score(X_pca_test,y_test))
plt.figure(figsize=(5 * 2, 10 * 3))
for i in range(50):
plt.subplot(10,5,i + 1) # 子视图
plt.imshow(faces_test[i],cmap = 'gray')
plt.axis('off') # 刻度关闭
# 贴上标签,并且对比实际数据和预测数据
true_name = target_names[y_test[i]].split(' ')[-1]
predict_name = target_names[y_pred[i]].split(' ')[-1]
plt.title(f'True:{true_name}\nPred:{predict_name}')