I am wondering if there is a better way to iterate through numpy arrays? I have timed my nested iterations and it takes roughly about 40-50 seconds per loop, and i am wondering if there is a faster way to do it? I know that looping through numpy arrays is not ideal, however I'm out of ideas. I looked through many questions on Stack Overflow but all of them ends up confusing me even more.
I have tried converting the numpy array to a list using the tolist() function, however the run time is equally slower, if not worse.
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
My euc_distance function passes in an array form (In my case, 5 dimensional) inputs, to output a 1 dimensional value. My data2.values is my way of access the numpy array through the pandas framework which is a [500 000, 5] dataframe.
(Note that the NormAttListTest is a list that has the categorical data of "Attack" and "Normal" tagged to each individual testing data).