问题描述
我整理了一些我认为简单的脚本,用于计算矩阵(@R_379_4045@ion_Gains_Matrix)及其相应的权重矩阵(Weights_Matrix)中每个要素的Moran指数。我想使用pool.map来处理@R_379_4045@ion_Gains_Matrix中的每个功能。输出只是@R_379_4045@ion_Gains_Matrix中每个要素的Moran指数的向量。查看代码:
import multiprocessing
from functools import partial
def Feature_Moran_Index(@R_379_4045@ion_Gains_Matrix,Wij,N):
Feature = @R_379_4045@ion_Gains_Matrix
X_bar = np.mean(Feature)
if X_bar != 0:
Deviance = Feature - X_bar
Outer_Deviance = np.outer(Deviance,Deviance)
Deviance2 = Deviance * Deviance
Denom = np.sum(Deviance2)
Moran_Index_score = (N/Wij) * (np.sum((Weights_Matrix * Outer_Deviance))/Denom)
else:
Moran_Index_score = 0
return Moran_Index_score
def Parallel_Feature_Moran_Index(@R_379_4045@ion_Gains_Matrix,Use_Cores):
N = @R_379_4045@ion_Gains_Matrix.shape[0]
Wij = np.sum(Weights_Matrix)
pool = multiprocessing.Pool(processes = Use_Cores)
Result = pool.map(partial(Feature_Moran_Index,Wij=Wij,N=N),@R_379_4045@ion_Gains_Matrix)
pool.close()
pool.join()
Moran_Index_scores = np.asarray(Result)
np.save("Moran_Index_scores.npy",Moran_Index_scores)
return Result
if __name__ == '__main__':
Moran_Index_scores = Parallel_Feature_Moran_Index(@R_379_4045@ion_Gains_Matrix,Use_Cores=(multiprocessing.cpu_count()-2))
我会公开承认我对Python中的多处理的理解不是理想的,但是过去我已经成功使用了很多。所以我真的不明白为什么这个简单的脚本不起作用。帮助极大的赞赏。注意,我知道Weights_Matrix不在脚本中。它以全局方式加载,因此它非常大,因此不必序列化到每个内核。很抱歉,这会激怒任何人,我知道有些人对使用全局变量感到很不满。
编辑:我已经在本地Ubuntu Shell(Windows笔记本电脑)上的较小数据集上成功使用了此确切代码。该问题发生在Ubuntu虚拟机中的非常大的数据集上。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)