[英]How to reconstruct image from first component in Python after PCA?
[英]How to reconstruct data from 10 components in Python using 2 matrixes after PCA?
请帮忙,。 我在这里看到了一些答案,但他们没有帮助我。 我需要重建初始数据。 有 2 个矩阵并使用前十个主成分。 第一个矩阵 (Z) (X_reduced_417) - 应用 sklearn.decomposition.PCA 的结果。 第二个矩阵 (X_loadings_417) (F) 是权重矩阵? 答案是初始数据 = Z*F+mean_matrix。 如何使用sklearn找到Z?
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import sklearn.datasets, sklearn.decomposition
df_loadings = pd.read_csv('X_loadings_417.csv', header=None)
df_reduced = pd.read_csv('X_reduced_417.csv', header=None) ```
import pandas as pd
import numpy as np
# Load the df_loadings and df_reduced matrices from the CSV files
df_loadings = pd.read_csv("X_loadings_417.csv", header=None)
df_reduced = pd.read_csv("X_reduced_417.csv", header=None)
# Convert the DataFrames to numpy arrays
F = df_loadings.values
Z = df_reduced.values
# The mean of the original data is needed to reconstruct the data
mean_matrix = np.mean(X, axis=0)
# Reconstruct the original data using the first ten principal components
X_reconstructed = Z[:,:10].dot(F[:10,:]) + mean_matrix
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.