【发布时间】:2014-07-15 18:13:37
【问题描述】:
我的代码如下所示:
from sklearn.datasets import load_svmlight_files
import numpy as np
perm1 =np.random.permutation(25000)
perm2 = np.random.permutation(25000)
X_tr, y_tr, X_te, y_te = load_svmlight_files(("dir/file.feat", "dir/file.feat"))
#randomly shuffle data
X_train = X_tr[perm1,:].toarray()[:,0:2000]
y_train = y_tr[perm1]>5 #turn into binary problem
到这里为止,代码都可以正常工作,但是当我尝试将另一个对象转换为数组时,我的程序会返回内存错误。
代码:
X_test = X_te[perm2,:].toarray()[:,0:2000]
错误:
---------------------------------------------------------------------------
MemoryError Traceback (most recent call last)
<ipython-input-7-31f5e4f6b00c> in <module>()
----> 1 X_test = X_test.toarray()
C:\Users\Asq\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\sparse\compressed.pyc in toarray(self, order, out)
788 def toarray(self, order=None, out=None):
789 """See the docstring for `spmatrix.toarray`."""
--> 790 return self.tocoo(copy=False).toarray(order=order, out=out)
791
792 ##############################################################
C:\Users\Asq\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\sparse\coo.pyc in toarray(self, order, out)
237 def toarray(self, order=None, out=None):
238 """See the docstring for `spmatrix.toarray`."""
--> 239 B = self._process_toarray_args(order, out)
240 fortran = int(B.flags.f_contiguous)
241 if not fortran and not B.flags.c_contiguous:
C:\Users\Asq\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\sparse\base.pyc in _process_toarray_args(self, order, out)
697 return out
698 else:
--> 699 return np.zeros(self.shape, dtype=self.dtype, order=order)
700
701
MemoryError:
我是python新手,不知道是否需要手动修复内存错误。
我的代码的其他部分返回相同的错误(例如使用 knn 或 ann 进行训练)。
我该如何解决这个问题?
【问题讨论】:
-
您可能耗尽了系统的可用内存。购买更多或分配更多(交换/分页)。
-
我使用 windows 并且交换内存现在扩展到 4gb。我的内存是 8GB。而 python 现在使用了我 2.5 gb 的内存(只是代码,直到这里运行)。
-
如果您可以通过将这些变量设置为具有相同形状和矩阵类型的随机变量来替换代码中加载 svm 数据的行,这将很有帮助,这样人们就可以尝试通过以下方式重现问题复制和粘贴。如果您无法做到这一点,请至少提供数组的形状。
标签: python python-2.7 numpy scikit-learn canopy