我想知道是否有更好的方法来迭代numpy数组? 我已经对我的嵌套迭代进行了计时,每次循环大约需要40-50秒,我想知道有没有更快的方法来完成它?我知道在numpy数组中循环并不理想,但是我已经束手无策了!我看了很多关于stackoverflow的问题,但是所有的问题都让我更加困惑。在
我尝试过使用tolist()
函数将numpy数组转换为一个列表,但是运行时同样慢,如果不是更糟的话。在
def euc_distance(array1, array2):
return np.power(np.sum((array1 - array2)**2) , 0.5)
for i in range(N):
for j,n in enumerate(data2.values):
distance = euc_distance(n, D[i])
if distance < Dradius[i] and NormAttListTest[j] == "Attack":
TP += 1
我的euc_距离函数以数组形式(在我的例子中是5维)传递输入,以输出一维值。我的data2.values
是通过pandas框架访问numpy数组的方法,pandas框架是一个[500000,5]数据帧。
(请注意,NormAttListTest是一个列表,它将“攻击”和“正常”分类数据标记到每个单独的测试数据上)
你的问题是你用错了
numpy
,因为numpy
都是关于像MATLAB
这样的矢量化计算。考虑对代码的以下修改。我用简单的numpy代码替换了numpy数组上的循环,它有效地利用了2d数组的矢量化。结果代码运行速度快了100倍。在输出:
^{pr2}$相关问题 更多 >
编程相关推荐