我試圖從1e5字符串中抽取1e7項目,但得到一個內存錯誤。從1e4琴絃中抽取1e6項目很好。我在一臺配備4GB內存的64位機器上,並不認爲我應該在1e7時達到任何內存限制。有任何想法嗎?如何解決mtrand.RandomState.choice中的內存錯誤?
$ python3
Python 3.3.3 (default, Nov 27 2013, 17:12:35)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> K = 100
正常工作與1E6:
>>> N = int(1e6)
>>> np.random.choice(["id%010d"%x for x in range(N//K)], N)
array(['id0000005473', 'id0000005694', 'id0000004115', ..., 'id0000006958',
'id0000009972', 'id0000003009'],
dtype='<U12')
錯誤與N = 1E7:
>>> N = int(1e7)
>>> np.random.choice(["id%010d"%x for x in range(N//K)], N)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "mtrand.pyx", line 1092, in mtrand.RandomState.choice (numpy/random/mtrand/mtrand.c:8229)
MemoryError
>>>
我發現這個問題,但它似乎是關於趕上這樣的錯誤,而不是解決它。
Python not catching MemoryError
我很樂意與任何一個解決方案仍然使用random.choice
或不同的方法來做到這一點。謝謝。
您正在允許重新採樣,所以我可以在需要的時間間隔內使用隨機數查看工作。 – doctorlove 2014-09-02 15:56:14
@doctorlove Thx。我已經嘗試過'['a','b','c'] [np.random.choice(2,1)]',並且適用於其中之一。但'['a','b','c'] [np.random.choice(2,5)]'給出了一個TypeError。如何通過隨機數字選擇字符串?我也嘗試過'.tolist()',但TypeError仍然存在。 – 2014-09-02 16:05:22
things = ['a','b','c']; [np.random.choice(2,5)中的x的東西[x]] – doctorlove 2014-09-02 16:25:11