Поскольку у меня нет ваших данных, я должен показать вам, как вы можете использовать мои данные.
- Загрузка данных и отображение данных
from sklearn.datasets import fetch_olivetti_faces
from sklearn.model_selection import train_test_split
from matplotlib.pyplot import subplots
from matplotlib.pyplot import suptitle
from matplotlib.pyplot import savefig
from sklearn.decomposition import PCA
def display_set(n_row, n_col, x, y_, t, title="Id:{}",
fig_size=(6, 3), dpi_=300, f_name="default.png"):
fig, ax = subplots(n_row, n_col, figsize=fig_size, dpi=dpi_)
ax = ax.flatten()
for i in range(n_row * n_col):
ax[i].imshow(X=x[i], cmap='gray')
ax[i].set_xticks([])
ax[i].set_yticks([])
ax[i].set_title(title.format(y_[i]))
suptitle(t=t)
savefig(f_name)
olivetti = fetch_olivetti_faces()
X = olivetti.images # Train
y = olivetti.target # Labels
x_train, x_test, y_train, y_test = train_test_split(X, y,
test_size=0.3,
random_state=42)
train_name = "train_samples.png"
test_name = "test_samples.png"
display_set(n_row=2, n_col=10, x=x_train, y_=y_train,
t="Train-set samples", title="Id:{}", f_name=train_name)
display_set(n_row=2, n_col=10, x=x_test, y_=y_test,
t="Test-set samples", title="Id:{}", f_name=test_name)
Вывод образцов поездов:
The output of test samples:
Now lets create a pca
object
x_train = x_train.reshape((x_train.shape[0], X.shape[1] * X.shape[2]))
x_test = x_test.reshape((x_test.shape[0], X.shape[1] * X.shape[2]))
pca_train = PCA(n_components=100).fit(X=x_train)
pca_test = PCA(n_components=100).fit(X=x_test)
eig_num_tr = len(pca_train.components_)
eig_num_te = len(pca_test.components_)
# eigen training faces
eig_tr_faces = pca_train.components_.reshape((eig_num_tr, X.shape[1], X.shape[2]))
# eigen test faces
eig_te_faces = pca_test.components_.reshape((eig_num_te, X.shape[1], X.shape[2]))
title_tr = "PCA Applied Train-set samples"
title_te = "PCA Applied Test-set samples"
t_ = "Eig. Id:{}"
display_set(n_row=2, n_col=5, x=eig_tr_faces, y_=range(0, eig_num_tr-1),
t=title_tr, title=t_, fig_size=(6, 3.2))
display_set(n_row=2, n_col=5, x=eig_te_faces, y_=range(0, eig_num_te-1),
t=title_te, title=t_, fig_size=(6, 3.2))
The output of the training set:
The output of the test set:
введите описание изображения здесь