Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3657086
  • 博文数量: 365
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2522
  • 用 户 组: 普通用户
  • 注册时间: 2019-10-28 13:40
文章分类

全部博文(365)

文章存档

2023年(8)

2022年(130)

2021年(155)

2020年(50)

2019年(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}')

阅读(5587) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~