PCA dimensionality reduction and reconstruction two-dimensional dataBeijing Institute of Technology | Ming-Jian Li
The following Python code is a companion code for the course on Artificial Intelligence and Simulation Science. It functions to reduce the dimensionality of the 1000 × 1000 surface data (with noise) in the accompanying script from 1000 features to only 20 principal components via PCA (Principal Component Analysis), and then reconstruct the original-sized surface for visualization.
x
1import numpy as np2import matplotlib.pyplot as plt3from sklearn.decomposition import PCA4from mpl_toolkits.mplot3d import Axes3D5
6n = 10007x = np.linspace(-3, 3, n)8y = np.linspace(-3, 3, n)9X, Y = np.meshgrid(x, y)10
11Z_base = 0.05 * np.sin(2 * X + 3 * Y) + 0.03 * np.cos(4 * X - 2 * Y)12Z_main = 1.2 * np.exp(-(X**2 + Y**2) / 0.6)13
14np.random.seed(10)15for _ in range(6):16 xm = np.random.uniform(-2, 2)17 ym = np.random.uniform(-2, 2)18 hm = np.random.uniform(-0.2, 0.7)19 wm = np.random.uniform(0.2, 0.8)20 Z_base += hm * np.exp(-((X - xm)**2 + (Y - ym)**2) / wm)21
22Z_clean = Z_main + Z_base23
24sig_power = np.mean(Z_clean**2)25noise_power = sig_power / (10**(20 / 10))26noise = np.random.normal(0, np.sqrt(noise_power), Z_clean.shape)27Z_orig = Z_clean + noise28
29data = Z_orig30k = 2031pca = PCA(n_components=k)32Z_low = pca.fit_transform(data)33Z_recon = pca.inverse_transform(Z_low)34
35def plot_surface(Z, title, cmap='viridis'):36 fig = plt.figure(figsize=(6, 5))37 ax = fig.add_subplot(111, projection='3d')38 x = np.arange(Z.shape[1])39 y = np.arange(Z.shape[0])40 X, Y = np.meshgrid(x, y)41 ax.plot_surface(X, Y, Z, cmap=cmap, rstride=10, cstride=10, alpha=1)42 ax.set_title(title, fontsize=12)43 plt.tight_layout()44 return fig45
46fig1 = plot_surface(Z_orig, '1. Original data (1000×1000)')47fig2 = plot_surface(Z_low[:20, :20], '2. Latent variables', cmap='plasma')48fig3 = plot_surface(Z_recon, '3. Reconstructed data (1000×1000)')49
50plt.show()The result is as follows.
