(365)
(8)
(130)
(155)
(50)
(22)
分类: 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}')
上一篇:
下一篇: