# PCA

PCA (Principal Component Analysis) is an unsupervised algorithm that can be viewed as a dimensionality reduction algorithm. It can be seen as a method of combining information into a small number of variables so as not to lose information on many variables as much as possible.

import numpy as np
import matplotlib.pyplot as plt
import japanize_matplotlib
from sklearn.datasets import make_blobs


## Dataset for experiments

X, y = make_blobs(n_samples=600, n_features=3, random_state=117117)

fig = plt.figure(figsize=(8, 8))
ax.scatter(X[:, 0], X[:, 1], X[:, 2], marker="o", c=y)
ax.set_xlabel("$x_1$")
ax.set_ylabel("$x_2$")
ax.set_zlabel("$x_3$")


## Dimension reduction to two dimensions with PCA

from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler

pca = PCA(n_components=2, whiten=True)
X_pca = pca.fit_transform(StandardScaler().fit_transform(X))

fig = plt.figure(figsize=(8, 8))
plt.scatter(X_pca[:, 0], X_pca[:, 1], c=y)


## See the effect of normalization

### Number of clusters 3, overlapping clusters

X, y = make_blobs(
n_samples=200, n_features=3, random_state=11711, centers=3, cluster_std=2.0
)
X[:, 1] = X[:, 1] * 1000
X[:, 2] = X[:, 2] * 0.01
X_ss = StandardScaler().fit_transform(X)

# Scatter plots of data before dimensionality reduction
fig = plt.figure(figsize=(8, 8))
ax.scatter(X[:, 0], X[:, 1], X[:, 2], marker="o", c=y)
ax.set_xlabel("$x_1$")
ax.set_ylabel("$x_2$")
ax.set_zlabel("$x_3$")
plt.title("Scatter plots of data before PCA")
plt.show()

# PCA without normalization
pca = PCA(n_components=2).fit(X)
X_pca = pca.transform(X)

# PCA with normalization
pca_ss = PCA(n_components=2).fit(X_ss)
X_pca_ss = pca_ss.transform(X_ss)

fig = plt.figure(figsize=(10, 5))
plt.subplot(121)
plt.title("without normalization")
plt.scatter(X_pca[:, 0], X_pca[:, 1], c=y, marker="x", alpha=0.6)
plt.subplot(122)
plt.title("with normalization")
plt.scatter(X_pca_ss[:, 0], X_pca_ss[:, 1], c=y, marker="x", alpha=0.6)