python带返回值的函数_带返回值函数的Python多处理

我试图对数组的每个元素进行主成分分析。数组的每个元素都是一个矩阵。我有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语句。它卡住了。我是否正确地进行了多重处理?

我希望我能得到一些帮助。

提前谢谢。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值