I have an array A
with size 600x6
that each row is a vector and I want to calculate the distance of each row from all other rows of the array. calculating the distance ( BD distance) is easy and I can calculate all the distances and put them in a matrix D(600x600)
, but during my code, I have just the value of the row not the index of it and so I cannot use D to find the distance quickly. so I have to calculate the distance again. my question is it a way to assign a label or index to each row of A
during the code? for example, I have A1
and A2
so I very fast find out that I have to extract D1,2
for distance. I am not very familiar with python. Could you please tell me how can I do this without calculating the distance each time?
as you can see in the following code, the centroid during the next step of the code will change. so I have to calculate the BD distance again which is time-consuming. but if I could save the index of centroid
I could extract the distance from my distance matrix very fast.
def kmeans_BD(psnr_bitrate,K,centroid):
m=psnr_bitrate.shape[0]#number of samples
n=psnr_bitrate.shape[1]#number of bitrate
# creating an empty array
BD=np.zeros((m,K))
#weight of BD_rate
wr=0.5
#weight of BD_Q
wq=0.5
n_itr=10
# finding distance between for each centroid
for itr in range(n_itr):
for k in range(K):
for i in range(len(psnr_bitrate)):
BD_R=bd_rate(rate,centroid[k,:],rate,psnr_bitrate[i,:])
if BD_R==-2:
BD_R=np.inf
BD_Q=bd_PSNR(rate,centroid[k,:],rate,psnr_bitrate[i,:])
if BD_Q==-2:
BD_Q=np.inf
BD[i,k]=np.abs(wr*BD_R+wq*BD_Q)