recursion to iteration in python

Viewed 114

We are trying to make a cluster analysis for a big amount of data. We are kind of new to python and found out that an iterative function is way more efficient than an recursive one. Now we are trying to change that but it is way harder than we thought.

This code underneath is the heart of our clustering function. This takes over 90 percent of the time. Can you help us to change that into a recursive one?

Some extra information: The taunach function gets neighbours of our point which will later form the clusters. The problem is that we have many many points.

def taunach(tau,delta, i,s,nach,anz):
    dis=tabelle[s].dist
    #delta=tau
    x=data[i]
    y=Skalarprodukt(data[tabelle[s].index]-x)
    a=tau-abs(dis)
    #LA.norm(data[tabelle[s].index]-x)
    if y<a*abs(a):
        nach.update({item.index for item in tabelle[tabelle[s].inner:tabelle[s].outer-1]})
        anz = anzahl(delta, i, tabelle[s].inner, anz)
        if dis>-1:
            b=dis-tau
            if y>=b*abs(b):#*(1-0.001):
                nach,anz=taunach(tau,delta, i,tabelle[s].outer,nach,anz)
    else:
        if y<tau**2:
            nach.add(tabelle[s].index)
            if y < delta:
                anz += 1
        if tabelle[s].dist>-4:
            b = dis - tau
            if y>=b*abs(b):#*(1-0.001)):
                nach,anz=taunach(tau,delta, i,tabelle[s].outer,nach,anz)
            if tabelle[s].dist > -1:
                if y<=(dis+tau)**2:
                    nach,anz=taunach(tau,delta, i,tabelle[s].inner,nach,anz)
    return nach,anz
0 Answers
Related