Apply PCA to reduce dimensionality and visualize transformed data.
import numpy as np
from sklearn.decomposition import PCA
X = np.array([[2.5, 2.4],
[0.5, 0.7],
[2.2, 2.9],
[1.9, 2.2],
[3.1, 3.0],
[2.3, 2.7]])
pca = PCA(n_components=1, random_state=0)
X_reduced = pca.fit_transform(X)
print("Original Shape:", X.shape)
print("Reduced Shape:", X_reduced.shape)
print("Reduced Data:\n", X_reduced)
print("Explained Variance Ratio:", pca.explained_variance_ratio_)
Original Shape: (6, 2)
Reduced Shape: (6, 1)
Reduced Data:
[[ 0.66]
[-2.12]
[ 0.57]
[-0.33]
[ 1.53]
[-0.31]]
Explained Variance Ratio: [0.96]