我试图对数组的每个元素进行主成分分析。数组的每个元素都是一个矩阵。我有500个这样的阵列。每个数组包含大约50个矩阵。总的来说,这需要太多的时间。我想用多重处理来加速这个过程。在
我写了以下代码:from multiprocessing import Pool
def princomp(A):
A=A.T
# computing eigenvalues and eigenvectors of covariance matrix
M = (A-mean(A.T,axis=1)).T # subtract the mean (along columns)
[latent,coeff] = linalg.eig(cov(M)) # attention:not always sorted
score = dot(coeff.T,M) # projection of the data in the new space
top_eigen=[i[0] for i in sorted(enumerate(latent), key=lambda x:x[1])]
#output = random.rand(990,3)
score = score.real
output = []
for a in range(0, 128):
output += [score[top_eigen[a]]]
return numpy.array(output)
def func1(...)
...
...
for i in range(0, nframes): #nframes is around 50
fc_array[i] = normalize(fc_array[i], axis=1, norm='l1') #fc_array[i] is a 2D numpy array
pool = Pool()
#ans_array[i] = princomp(fc_array[i]) # normal principle component analysis
result = pool.apply_async(princomp, [fc_array[i]]) # principle component analysis using multiprocessing
print "Frame No.: ", i
ans_array[i] = result.get()
p.close()
p.join()
return ans_array
它卡在'func1'的for循环中。不执行print语句。它卡住了。我是否正确地进行了多重处理?
我希望我能得到一些帮助。
提前谢谢。在