2011-12-29 151 views
0

carrays我有這樣的代碼:問題與蟒蛇

import numpy as np 
import tables as tb 

ndim = 50000 
h5in = tb.openFile('data.h5','r') 
data = h5in.root.x 

h5out = tb.openFile('testout.h5', mode='w', title="argsort distances") 
root = h5out.root 
x = h5out.createCArray(root,'x',tb.Int16Atom(),shape=(ndim,ndim)) 

for i in xrange(ndim): 
    x[:,i] = np.argsort(dist[i,:]) 

它只是需要執行的永恆。它有什麼辦法來加速這一點?

注:它必須是X [:我]:

x[:,:] = np.argsort(dist, axis=1).T 

UPDATE:而不是X [我,:]

回答

1

替換爲for循環如果過大,然後嘗試找到切片大小的折中:

slice_size = 100 # or 1000 if it fits into your memory 
for i in xrange(0, ndim, slice_size): 
    x[:,i:i+slice_size] = np.argsort(dist[i:i+slice_size,:], axis=1) 
+0

它給出內存錯誤。這就是爲什麼我使用循環 – Academia 2011-12-29 20:24:10

+0

@ user1038382 - 也許你可以嘗試在切片中工作......如果使用'slice_size',請參閱我的更新回答 – eumiro 2011-12-30 07:57:55

+0

,然後調整np.argsort()的返回值。 – jfs 2011-12-30 08:30:58

0

您是否試圖加載一個文件,其中有行的形式的數據?

如果是,請嘗試np.loadtxt