我想对比PCA降维前后模型拟合时间和得分,结果不进行降维运算后分类的结果只有一个,不知道是哪里出了问题,还是确实只能得到一个结果。(想象中应该是将test集图片分类成各个人的,但是结果出来以后都是小布什)
precision recall f1-score support
Ariel Sharon 0.00 0.00 0.00 13
Colin Powell 0.00 0.00 0.00 60
Donald Rumsfeld 0.00 0.00 0.00 27
George W Bush 0.45 1.00 0.62 146
Gerhard Schroeder 0.00 0.00 0.00 25
Hugo Chavez 0.00 0.00 0.00 15
Tony Blair 0.00 0.00 0.00 36
accuracy 0.45 322
macro avg 0.06 0.14 0.09 322
weighted avg 0.21 0.45 0.28 322
[[ 0 0 0 13 0 0 0]
[ 0 0 0 60 0 0 0]
[ 0 0 0 27 0 0 0]
[ 0 0 0 146 0 0 0]
[ 0 0 0 25 0 0 0]
[ 0 0 0 15 0 0 0]
[ 0 0 0 36 0 0 0]]
from time import time
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.datasets import fetch_lfw_people
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.decomposition import PCA
from sklearn.svm import SVC
lfw_people = fetch_lfw_people(min_faces_per_person=70, resize=0.4)
n_samples, h, w = lfw_people.images.shape
X = lfw_people.data
n_features = X.shape[1]
y = lfw_people.target
target_names = lfw_people.target_names
n_classes = target_names.shape[0]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)
t0 = time()
param_grid = {'C': [1e3, 5e3, 1e4, 5e4, 1e5],'gamma': [0.0001, 0.0005, 0.001, 0.005, 0.01, 0.1], }
clf = GridSearchCV(SVC(kernel='rbf', class_weight='balanced'), param_grid)
clf.fit(X_train, y_train)
print("done in %0.3fs" % (time() - t0))
y_pred=clf.predict(X_test)
print(classification_report(y_test, y_pred, target_names=target_names))
print(confusion_matrix(y_test, y_pred, labels=range(n_classes)))
print(classification_report(y_test, y_pred, target_names=target_names))
print(confusion_matrix(y_test, y_pred, labels=range(n_classes)))