0

I am trying to save the indexes of list at each iteration of a function in a loop. Actually, I called a function within a loop and saved the function output values in a list. At each iteration, I selected some values from this list based on threshold and deleted the selected values from the list and again ran the function in the order till the last number. The problem is that at each iteration this function is called and indexes start from zero.

I explain with example.

I have an adjacency matrix with a size of 11*11, which means 11 points in this matrix:

A= [[0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [1. 0. 1. 1. 1. 0. 0. 0. 0. 0. 0.]
 [0. 1. 0. 1. 1. 1. 0. 0. 0. 0. 0.]
 [0. 1. 1. 0. 1. 0. 0. 0. 0. 0. 0.]
 [0. 1. 1. 1. 0. 0. 1. 0. 0. 0. 0.]
 [0. 0. 1. 0. 0. 0. 0. 1. 0. 0. 0.]
 [0. 0. 0. 0. 1. 0. 0. 1. 0. 0. 0.]
 [0. 0. 0. 0. 0. 1. 1. 0. 1. 1. 1.]
 [0. 0. 0. 0. 0. 0. 0. 1. 0. 1. 1.]
 [0. 0. 0. 0. 0. 0. 0. 1. 1. 0. 1.]
 [0. 0. 0. 0. 0. 0. 0. 1. 1. 1. 0.]]

I calculate the similarity from this matrix A using given code:

from scipy.spatial.distance import pdist, squareform
D = squareform(pdist(A))
sigma2 = np.median(D)
#print(sigma2)
S = np.exp(-(D*D) / sigma2)

here is my RD function that takes S as input to calculate the probability of each node:

from numpy.core.fromnumeric import shape
import numpy as np
from numpy.linalg import norm

#define a RD function
def RD(A, x=None, epsilon=2.0e-4):
  """A is a similarity matrix and compute the dominant sets for the A with the
    replicator dynamics optimization approach. 
    Convergence is reached when x changes less than epsilon.
    """
  if x is None:
    x = np.ones(A.shape[0])/float (A.shape[0])
  
  distance = epsilon*2.0
  
  while distance > epsilon:
    x_old = x.copy()
    x = x* A.dot(x)
    x = x/ x.sum()
    distance = norm(x-x_old)
    #print(x.size, distance)
  
  return x

now I used all the above functions in this code and selects some points in each iteration based on the threshold and then delete the selected points from matrix S and again calculate the S matrix for the remaining points, this process ran at n time:

n = 3
k = 0
cluster_idx = []
cluster_ids = []
while k<n:
    x = RD(S, epsilon=2e-4)
    threshold = 1.0e-4 
    cluster_idx = np.where(x>=threshold)
    cluster_ids.append(cluster_idx)
    List_list = sum(cluster_ids, [])
    print("cluster ids", List_list)
    S = np.delete(S, cluster_idx, axis=0)
    S = np.delete(S, cluster_idx, axis=1)
    k +=1

the out of this code: List_list=[[0,1,2,3], [0,1,2], [0,1,2,3]]

the expected output should like that: List_list=[[0,1,2,3], [4,5,6], [7,8,9,10]]

In each iteration, the indexes start from zero. how can I save the indexes at each iteration with the original indexes?

Waqar Ali
  • 1
  • 1

0 Answers0