【问题标题】:MemoryError while converting sparse matrix to dense matrix? (numpy, scikit)将稀疏矩阵转换为密集矩阵时出现内存错误? (numpy,scikit)
【发布时间】:2013-10-17 08:34:50
【问题描述】:
lr = lm.LogisticRegression(penalty='l2', dual=True, tol=0.0001, 
                             C=1, fit_intercept=True, intercept_scaling=1.0, 
                             class_weight=None, random_state=None)

rd = AdaBoostClassifier( base_estimator=lr, 
                                           learning_rate=1, 
                                           n_estimators=20, 
                                           algorithm="SAMME")
##here, i am deleting unnecesseary objects
##print X.shape
##(7395, 412605)
print "20 Fold CV Score: ", np.mean(cross_validation.cross_val_score(rd, X, y, cv=20, scoring='roc_auc'))

当我运行这个我得到这个错误:

TypeError:传递了稀疏矩阵,但需要密集数据。采用 X.toarray() 转换为密集的 numpy 数组。

然后,我像这样更改了我的代码:

print "20 Fold CV Score: ", np.mean(cross_validation.cross_val_score(rd, X.toarray(), y, cv=20, scoring='roc_auc'))

现在,我有以下例外:

  File "/usr/lib/python2.7/dist-packages/scipy/sparse/compressed.py", line 559, in toarray
    return self.tocoo(copy=False).toarray(order=order, out=out)
  File "/usr/lib/python2.7/dist-packages/scipy/sparse/coo.py", line 235, in toarray
    B = self._process_toarray_args(order, out)
  File "/usr/lib/python2.7/dist-packages/scipy/sparse/base.py", line 628, in _process_toarray_args
    return np.zeros(self.shape, dtype=self.dtype, order=order)
MemoryError

有解决问题的建议吗?

【问题讨论】:

    标签: python numpy scikit-learn


    【解决方案1】:

    MemoryError 表示您的系统上没有足够的可用 RAM 来分配矩阵。为什么?好吧,7395 x 412605 矩阵有 3,051,213,975 个元素。如果它们是默认的 float64(通常是 C 中的 double)数据类型,则为 22.7GB。如果您转换为较低精度的 float32s(通常是 C 中的 float),则为 11.4GB;也许这在你的机器上是可以处理的。不过,它仍然会很慢。

    似乎AdaBoostClassifier 不支持稀疏输入(如您所见in the code here)。我不知道算法是否需要密集表示,或者只是实现假设了这一点。

    【讨论】:

    • 这是实现。对决策树的稀疏矩阵支持,以及所有花哨的集成估计器,已经在待办事项列表中出现了很长时间。
    猜你喜欢
    • 2021-11-25
    • 1970-01-01
    • 2014-12-21
    • 1970-01-01
    • 2023-04-10
    • 2017-07-02
    • 2013-03-07
    • 1970-01-01
    • 2012-02-20
    相关资源
    最近更新 更多