sklearn.decomposition.TruncatedSVD可以分块应用到矩阵上吗?

我在处理非常大的矩阵时使用了sklearn.decomposition.TruncatedSVD。当矩阵超过一定大小(例如35万行2.5万列)时,svd.fit(x)会耗尽RAM。

我将svd应用于特征矩阵,每行代表从单张图像中提取的一组特征。

为了解决内存问题,将矩阵分块应用svd(然后再拼接)是否安全?

结果会相同吗? 即:

from sklearn.decomposition import TruncatedSVDsvd = TruncatedSVD(n_components=128)part_1 = svd.fit_transform(features[0:100000, :])part_2 = svd.fit_transform(features[100000:, :])svd_features = np.concatenate((part_1, part_2), axis=0)

.. 等同于(?):

from sklearn.decomposition import TruncatedSVDsvd = TruncatedSVD(n_components=128)svd_features = svd.fit_transform(svd_features)

如果不是,那么对于非常大的矩阵进行降维有没有解决方案?


回答:

结果不会相同,

例如,考虑以下代码:

import numpy as npfeatures=np.array([[3, 2, 1, 3, 1],       [2, 0, 1, 2, 2],       [1, 3, 2, 1, 3],       [1, 1, 3, 2, 3],       [1, 1, 2, 1, 3]])from sklearn.decomposition import TruncatedSVDsvd = TruncatedSVD(n_components=2)svd = TruncatedSVD(n_components=2)part_1 = svd.fit_transform(features[0:2, :])part_2 = svd.fit_transform(features[2:, :])svd_features = np.concatenate((part_1, part_2), axis=0)svd_b = TruncatedSVD(n_components=2)svd_features_b = svd_b.fit_transform(features)print(svd_features)print(svd_features_b)

这会打印出

[[ 4.81379561 -0.90959982] [ 3.36212985  1.30233746] [ 4.70088886  1.37354278] [ 4.76960857 -1.06524658] [ 3.94551566 -0.34876626]][[ 4.17420185  2.47515867] [ 3.23525763  0.9479915 ] [ 4.53499272 -1.13912762] [ 4.69967028 -0.89231578] [ 3.81909069 -1.05765576]]

这些结果彼此不同。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注