Dimensionality Reduction With PCA


# Load libraries
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn import datasets

Load Data

# Load the data
digits = datasets.load_digits()

Standardize Feature Values

# Standardize the feature matrix
X = StandardScaler().fit_transform(digits.data)

Conduct Principal Component Analysis

# Create a PCA that will retain 99% of the variance
pca = PCA(n_components=0.99, whiten=True)

# Conduct PCA
X_pca = pca.fit_transform(X)

View Results

# Show results
print('Original number of features:', X.shape[1])
print('Reduced number of features:', X_pca.shape[1])
Original number of features: 64
Reduced number of features: 54