def test_pca(self): globalPCA = model_order_reduction.PCA(self.X) globalPCA.eigens = self.nPCtest globalPCA.plot_explained_variance = False PCs, eigenvalues = globalPCA.fit() explained = globalPCA.get_explained() self.assertEqual(PCs.shape[1],self.nPCtest) self.assertEqual(len(eigenvalues),self.nPCtest) self.assertIsInstance(explained, float)
text_file.close() # Retrieve the PVs name, and the corresponding numbers in the original matrix X PV = model_order_reduction.variables_selection(X_preprocessed, settings) PVs, numbers = PV.fit() # Save the selected variables in a .txt, they can be useful later on np.savetxt("Selected_variables.txt", numbers) # Cut the dimensionality of the original matrix, considering only # the selected 'm' variables X_preprocessed_cut = X_preprocessed[:, numbers] #perform the dimensionality reduction via Principal Component Analysis, # and return the eigenvectors of the reduced manifold (PCs). model = model_order_reduction.PCA(X_preprocessed, settings) PCs, ____ = model.fit() #compute the projection of the original points on the reduced #PCA manifold, obtaining the scores matrix Z Z = model.get_scores() # Compute the matrix B for the MGPCA reconstruction Bi, resid, rank, s = np.linalg.lstsq(X_preprocessed_cut, Z) # Now we can reconstruct the original matrix from the reduced matrix X_preprocessed_cut X_recovered = X_preprocessed_cut @ Bi @ PCs.T # Uncenter and unscale the reconstructed matrix X_ = unscale(X_recovered, sigma) X_back = uncenter(X_, mu)
"scale": True, "scaling_method": "auto", #set the final dimensionality "number_of_eigenvectors": 7, #enable to plot the cumulative explained variance "enable_plot_variance": True, #set the number of the variable whose reconstruction must be plotted "variable_to_plot": 0, } X = readCSV(file_options["path_to_file"], file_options["input_file_name"]) model = model_order_reduction.PCA(X, settings) #perform the dimensionality reduction via Principal Component Analysis, #and return the eigenvectors of the reduced manifold PCs, ____ = model.fit() #plot the original PC model.plot_PCs() #apply the varimax rotation algorithm from the utilities module rotated = varimax_rotation(X, PCs, normalize=False) #plot the rotated PC fig = plt.figure() axes = fig.add_axes([0.15, 0.15, 0.7, 0.7], frameon=True) x = np.linspace(1, X.shape[1], X.shape[1]) axes.bar(x, rotated[:, 0])