【问题标题】:How do I store a fitted PCA so that I may transpose unseen testing dataset? I do not wish to keep the large training dataset on my CPU如何存储拟合的 PCA 以便我可以转置看不见的测试数据集?我不希望将大型训练数据集保留在我的 CPU 上
【发布时间】:2021-12-06 19:56:19
【问题描述】:

我有一个非常大的训练数据集。我的训练数据集包含 1050 个手势,每个手势包含 12,000 个数据点。向我们的机器学习模型提供如此多的数据点将导致性能非常缓慢和准确性低下。因此,我使用 PCA 从高维空间中去除不相关的特征,并将最重要的特征投影到低维子空间中,从而提高分类精度并减少计算时间。使用 PCA,我们将每个手势的 12,000 个数据点减少到 15 台 PC,而不会影响从数据中提取的信息。

将来,我想将我的机器学习模型存储到 Arduino 上。 Arduino 是一个小芯片,大约有 256KB 的存储空间。我用来拟合 PCA 的训练数据集的存储空间为 225MB,因此不可能。

有没有办法执行 PCA 并将其拟合到我的训练数据集,以便我将来可以在 Arduino 上转置我看不见的测试数据集,而不必将训练数据集存储到我的 Arduino 中进行拟合?

这是适合我的训练数据集的代码

from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA

transposed_normDF.columns = transposed_normDF.columns.map(str)
features = [str(i) for i in range(0,11999)]
x = transposed_normDF.loc[:, features].values
y = df.loc[:,['label']].values

pca = PCA(n_components=0.99)
principalComponents = pca.fit_transform(x)

pc = pca.explained_variance_ratio_.cumsum()
x1 = StandardScaler().fit_transform(principalComponents)
full_newdf = pd.DataFrame(data = x1
             , columns = [f'pc_stdscaled_{i}' for i in range(len(pc))])
full_finalDf = pd.concat([full_newdf, df[['label']]], axis = 1)
print(full_finalDf)
print(full_newdf.shape)

这是我转置看不见的数据的代码

pca = PCA(n_components=0.99)

newdata_transformed = pca.transform(in_data)
pc = pca.explained_variance_ratio_.cumsum()
x1 = StandardScaler().fit(principalComponents)
X1 = x1.transform(newdata_transformed)
newdf = pd.DataFrame(data = X1
             , columns = [f'pc_stdscaled_{i}' for i in range(len(pc))])
newdf.head()

【问题讨论】:

    标签: python machine-learning pca data-preprocessing


    【解决方案1】:

    是的,可以将 PCA 安装到训练集上,然后在另一个程序上重复使用。 您可以使用pickle 保存模型并加载它。 这是一个代码sn-p:

    from sklearn.decomposition import PCA
    import pickle as pk
    from sklearn.datasets import make_blobs
    
    X, y = make_blobs(n_samples=10, centers=3, n_features=20, random_state=0)
    pca = PCA(n_components=2)
    result = pca.fit_transform(X) # Assume X is having more than 2 dimensions    
    input = X[0]
    result = pca.transform([input])
    print(result) # output: [[ 25.27946068  -2.74478573]]
    pk.dump(pca, open("pca.pkl","wb"))
    

    保存拟合好的 PCA 后,您可以在另一个程序中重新加载并转换新的输入样本,而无需加载训练数据,如下所示:

    # later reload the pickle file, no training data needed
    pca_reloaded = pk.load(open("pca.pkl",'rb')) 
    result_new = pca_reloaded.transform([input]) # X_new is a new data sample
    print(result_new) # output: [[ 25.27946068  -2.74478573]]
    

    当您比较resultresult_new 时,您会发现它们是相等的。

    来源:https://datascience.stackexchange.com/questions/55066/how-to-export-pca-to-use-in-another-program

    【讨论】:

    • 我看到变量 pca 被pickle保存在这里。但这是否也会保存初始 X 的结果(第 4 行)(在我的情况下,pca 被应用于训练数据集)?因此训练数据集没有上传
    • 我希望能够以不再需要将我的 PCA 拟合到我的 Arduino 上的训练数据集的方式执行 PCA。我可以导入现有的 PCA 拟合来转置我的新数据(因此没有第 4 行 X 是我的训练数据集)
    • @Quine,很抱歉造成混淆,我编辑了答案。现在可以将 PCA 拟合到您的训练数据 X 然后保存。然后加载保存的 pickle 文件并将 PCA 应用于新的数据样本 X_new
    • 所以我还需要在 Arduino 脚本中执行 pca.fit_transform(X)。或者我现在可以通过pickle加载PCA并立即转置新数据(X_new)。因此不需要变量 X(也就是训练数据)
    • @Quine,没错。在 Arduino 上加载拟合模型后,无需训练数据。为了确保这一点,您可以对其进行测试并比较 PC 的
    猜你喜欢
    • 2019-07-24
    • 2021-10-30
    • 1970-01-01
    • 2017-02-22
    • 2016-10-16
    • 2020-10-21
    • 1970-01-01
    相关资源
    最近更新 更多