Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 其它 > 资料存档
资料存档 资料存档
回复
 
主题工具 显示模式
旧 2019-12-10, 20:48   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
帖子 Numpy / Python与Matlab相比表现出色

新手程序员在这里。我正在编写一个程序,用于分析点(单元)的相对空间位置。程序从第1列的x坐标,第2列的y坐标和第3列的单元格类型的数组中获取边界和单元格类型。然后,检查每个单元格的单元格类型和距边界的适当距离。如果通过,它将计算与阵列中每个其他单元的距离,如果该距离在指定的分析范围内,则会将其以该距离添加到输出阵列。

我的细胞标记程序在wxpython中,因此我也希望在python中开发该程序,并最终将其粘贴到GUI中。不幸的是,现在python需要大约20秒才能在我的计算机上运行核心循环,而MATLAB可以每秒完成15个循环。由于我计划在大约30个案例上进行1000次循环(使用随机比较条件)乘以几种探索性分析类型,因此这并不是一个小小的差异。

我尝试运行探查器,并且数组调用的时间是该时间的1/4,几乎所有其余时间都是未指定的循环时间。

这是主循环的python代码:

for basecell in range (0, cellnumber-1): if firstcelltype == np.array((cellrecord[basecell,2])): xloc=np.array((cellrecord[basecell,0])) yloc=np.array((cellrecord[basecell,1])) xedgedist=(xbound-xloc) yedgedist=(ybound-yloc) if xloc>excludedist and xedgedist>excludedist and yloc>excludedist and yedgedist>excludedist: for comparecell in range (0, cellnumber-1): if secondcelltype==np.array((cellrecord[comparecell,2])): xcomploc=np.array((cellrecord[comparecell,0])) ycomploc=np.array((cellrecord[comparecell,1])) dist=math.sqrt((xcomploc-xloc)**2+(ycomploc-yloc)**2) dist=round(dist) if dist>=1 and distexcludedist) && (yloc>excludedist) && (xedgedist>excludedist) && (yedgedist>excludedist); for comparecell = 1:cellnumber; if secondcelltype==cellrecord(comparecell,3); xcomploc=cellrecord(comparecell,1); ycomploc=cellrecord(comparecell,2); dist=sqrt((xcomploc-xloc)^2+(ycomploc-yloc)^2); if (dist>=1) && (dist>> timeit.Timer('x = 111.1').timeit() 0.045882196294822819 >>> t=timeit.Timer('x = np.array(111.1)','import numpy as np').timeit() 0.55774970267830071 这些调用之间的差异是一个数量级。

第二:以下代码:

arraytarget=round(dist*analysisdist/intervalnumber) addone=np.array((spatialraw[arraytarget-1])) addone=addone+1 targetcell=arraytarget-1 np.put(spatialraw,[targetcell,targetcell],addone) 可以替换为

arraytarget=round(dist*analysisdist/intervalnumber)-1 spatialraw[arraytarget] += 1 第三:你可以摆脱开方的菲利普提到的平方analysisdist提前。但是,由于您使用analysisdist来获取arraytarget ,因此您可能需要创建一个单独的变量analysisdist2 ,该变量是analysisdist2的平方,并将其用于比较。

第四:您每次到达该点时都在寻找与secondcelltype匹配的单元,而不是一次查找并secondcelltype使用该列表。您可以定义一个数组:

comparecells = np.where(cellrecord[:,2]==secondcelltype)[0] 然后更换

for comparecell in range (0, cellnumber-1): if secondcelltype==np.array((cellrecord[comparecell,2])): 与

for comparecell in comparecells: 第五:使用psyco 。它是一个JIT编译器。如果您使用的是最新版本,则Matlab具有内置的JIT编译器。这应该可以加快代码的速度。

第六:如果经过上述所有步骤后代码仍然不够快,则应尝试对代码进行矢量化处理。应该不会太困难。基本上,在numpy数组中可以包含的内容越多越好。这是我尝试向量化的方法:

basecells = np.where(cellrecord[:,2]==firstcelltype)[0] xlocs = cellrecord[basecells, 0] ylocs = cellrecord[basecells, 1] xedgedists = xbound - xloc yedgedists = ybound - yloc whichcells = np.where((xlocs>excludedist) & (xedgedists>excludedist) & (ylocs>excludedist) & (yedgedists>excludedist))[0] selectedcells = basecells[whichcells] comparecells = np.where(cellrecord[:,2]==secondcelltype)[0] xcomplocs = cellrecords[comparecells,0] ycomplocs = cellrecords[comparecells,1] analysisdist2 = analysisdist**2 for basecell in selectedcells: dists = np.round((xcomplocs-xlocs[basecell])**2 + (ycomplocs-ylocs[basecell])**2) whichcells = np.where((dists >= 1) & (dists
poster 当前离线   回复时引用此帖
回复

主题工具
显示模式

发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛禁用 表情符号
论坛启用 [IMG] 代码
论坛启用 HTML 代码



所有时间均为北京时间。现在的时间是 23:46


Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.