Пример #1
0
Файл: kd.py Проект: niminjie/KNN
	def search_knn(self,P,x,k,maxiter=200):
		def pf_compare(a,b):
			return self.dist(x,a.x)<self.dist(x,b.x)
		def ans_compare(a,b):
			return self.dist(x,a.x)>self.dist(x,b.x)
		pf_seq=Heap(compare=pf_compare)
		pf_seq.insert(P)    #prior sequence
		ans=Heap(k,compare=ans_compare)  #ans sequence
		while pf_seq.counter>0:
			t=pf_seq.heap[1]
			pf_seq.delete(1)
			flag=True
			if ans.counter==k:
				now=t.F
				#print ans.heap[1].x,'========'
				if now != None:
					q=x.copy()
					q[now.l]=now.x[now.l]
					length=self.dist(q,x)
					if length>self.dist(ans.heap[1].x,x):
						flag=False
					else:
						flag=True
				else:
					flag=True
			if flag:
				tp,pf_seq,ans=self.to_leaf(t,x,pf_seq,ans)
			#print "============="
			#ans.insert(tp)
		return ans
Пример #2
0
    def search_knn(self, P, x, k, maxiter=200):
        def pf_compare(a, b):
            return self.dist(x, a.x) < self.dist(x, b.x)

        def ans_compare(a, b):
            return self.dist(x, a.x) > self.dist(x, b.x)

        pf_seq = Heap(compare=pf_compare)
        pf_seq.insert(P)  #prior sequence
        ans = Heap(k, compare=ans_compare)  #ans sequence
        while pf_seq.counter > 0:
            t = pf_seq.heap[1]
            pf_seq.delete(1)
            flag = True
            if ans.counter == k:
                now = t.F
                #print ans.heap[1].x,'========'
                if now != None:
                    q = x.copy()
                    q[now.l] = now.x[now.l]
                    length = self.dist(q, x)
                    if length > self.dist(ans.heap[1].x, x):
                        flag = False
                    else:
                        flag = True
                else:
                    flag = True
            if flag:
                tp, pf_seq, ans = self.to_leaf(t, x, pf_seq, ans)
            #print "============="
            #ans.insert(tp)
        return ans
Пример #3
0
from dml.tool import Heap
def cmp(aa,bb):
	return aa<bb
a=Heap()
a.insert(36)
a.insert(45)

a.insert(72)
a.insert(30)
a.insert(18)
a.insert(53)
a.insert(35)
a.insert(48)
a.insert(93)
a.insert(15)

print a.heap
while a.counter>0:
	print a.heap[1]
	#print a.heap
	a.delete(1)