示例#1
0
 def push(self, item):
     if len(self.items) < self.n:
         self.items.append(item)
         if len(self.items) == self.n:
             _heapify_max(self.items)
     else:
         _heappushpop_max(self.items, item)
示例#2
0
    def addElement(self, num):
        if len(self.minHeap) == 0:
            heapq.heappush(self.minHeap, num)
        else:
            minHeapTop = heapq.nsmallest(1, self.minHeap)[0]
            if len(heapq.nlargest(1, self.maxHeap)) == 0:
                heapq.heappush(self.maxHeap, min(num, minHeapTop))
                heapq.heappushpop(self.minHeap, max(num, minHeapTop))
            else:
                maxHeapTop = heapq.nlargest(1, self.maxHeap)[0]

                if num > minHeapTop:
                    tmp = minHeapTop
                    minHeapTop = num
                    num = tmp
                elif num < maxHeapTop:
                    tmp = maxHeapTop
                    maxHeapTop = num
                    num = tmp

                heapq.heappushpop(self.minHeap, minHeapTop)
                heapq._heappushpop_max(self.maxHeap, maxHeapTop)

                if len(self.minHeap) - len(self.maxHeap) > 0:
                    heapq.heappush(self.maxHeap, num)
                else:
                    heapq.heappush(self.minHeap, num)
示例#3
0
    def addElement(self, num):
        if len(self.minHeap) == 0:
            heapq.heappush(self.minHeap, num)
        else:
            minHeapTop = heapq.nsmallest(1,self.minHeap)[0]
            if len(heapq.nlargest(1,self.maxHeap)) == 0:
                heapq.heappush(self.maxHeap, min(num,minHeapTop))
                heapq.heappushpop(self.minHeap, max(num,minHeapTop))
            else:
                maxHeapTop = heapq.nlargest(1,self.maxHeap)[0]

                if num > minHeapTop:
                    tmp = minHeapTop
                    minHeapTop = num
                    num = tmp
                elif num < maxHeapTop:
                    tmp = maxHeapTop
                    maxHeapTop = num
                    num = tmp
                
                heapq.heappushpop(self.minHeap, minHeapTop)
                heapq._heappushpop_max(self.maxHeap, maxHeapTop)
                
                if len(self.minHeap) - len(self.maxHeap) > 0:
                    heapq.heappush(self.maxHeap,num)
                else:
                    heapq.heappush(self.minHeap,num)
示例#4
0
文件: med.py 项目: ajiehust/rosalind
def med(n, lst, k):
    assert n == len(lst)
    assert n > k
    l = lst[0:k]
    heapq._heapify_max(l)
    for i in range(k, n):
        if lst[i] < l[0]:
            heapq._heappushpop_max(l, lst[i])
    return l[0]
def _kth_smallest(nums, k):
    heap = []
    for num in itertools.islice(nums, 0, k):
        heap.append(num)
        heapq._siftdown_max(heap, 0, len(heap) - 1)
    for num in itertools.islice(nums, k, len(nums)):
        if num <= heap[0]:
            heapq._heappushpop_max(heap, num)
    return heap[0]
示例#6
0
 def push(self, item):
     if self.order:
         if len(self._data) < self.n:
             self.maxpush((self.key(item), item))
         else:
             heapq._heappushpop_max(self._data, (self.key(item), item))
     else:
         if len(self._data) < self.n:
             heapq.heappush(self._data, (self.key(item), item))
         else:
             heapq.heappushpop(self._data, (self.key(item), item))
示例#7
0
  def CalMedian(self,array):
    l=len(array)
    hql=[]
    hqr=[]
    ll=0
    lr=0
    summedian=0
    for i in range(l):
      if ll==lr:
        if ll==0:
          self.heappush_max(hql,array[i])
        else:
          if array[i]<hqr[0]:
            self.heappush_max(hql,array[i])
          else:
            self.heappush_min(hqr,array[i])
            item=heapq.heappop(hqr)
            self.heappush_max(hql,item) 
        ll+=1
      else:
        if array[i]>hql[0]:
          self.heappush_min(hqr,array[i])
        else:
          temp=array[i]
          item=heapq._heappushpop_max(hql,temp)
          self.heappush_min(hqr,item)
        lr+=1
#      print hql[0]
      summedian=summedian + hql[0]
#    print summedian  
    return summedian%l  
def f_heap(arr):
    lowest = arr[:2]
    highest = arr[:3]
    _heapify_max(lowest)
    heapify(highest)

    for i in range(2, len(arr)):
        value = arr[i]
        if value < lowest[0]:
            _heappushpop_max(lowest, value)
        if i != 2 and value > highest[0]:
            heappushpop(highest, value)

    candidates = [[lowest[0], lowest[1], max(highest)],
                  [highest[0], highest[1], highest[2]]]

    return max(candidates, key=lambda x: reduce(operator.mul, x))
def kth_unique3(nums, k):
    # time O(n), space O(n), one pass with heap
    # http://stackoverflow.com/questions/5380568/algorithm-to-find-k-smallest-numbers-in-array-of-n-items
    # http://stackoverflow.com/questions/2501457/what-do-i-use-for-a-max-heap-implementation-in-python
    lookup = {}
    for i in range(len(nums)):
        if nums[i] not in lookup:
            lookup[nums[i]] = [1, i]
        else:
            lookup[nums[i]][0] += 1

    import heapq
    unique_indexes = [float('inf') for _ in range(k)]
    for count, location in lookup.values():
        if count == 1:
            if location < unique_indexes[0]:
                heapq._heappushpop_max(unique_indexes, location)
    unique_indexes.sort()
    return unique_indexes[-1]
示例#10
0
def get_median():
    minheap, maxheap = [], []
    heapq.heapify(minheap)
    heapq._heapify_max(maxheap)
    median = 0
    while True:
        val = random.random()
        if minheap==None and maxheap==None:
            heapq.heappush(minheap, val)
            median = val
        elif minheap and maxheap==None:
            heapq._heappushpop_max(maxheap, val)
        elif minheap==None and maxheap:
            heapq.heappush(minheap, maxheap[0])
            heapq.heappop(maxheap)
        else:
            if val<minheap[0]:
                heapq.heappush(minheap, val)
            else:
                heapq._heappushpop_max(maxheap, val)
            if len(minheap)>len(maxheap)+1:
                heapq.heappush(minheap, heapq.heappop(maxheap))
            elif len(minheap)+1<len(maxheap):
                heapq._heappushpop_max(maxheap, heapq.heappop(minheap))
        if minheap or maxheap:
            if len(minheap)==len(maxheap):
                median = (minheap[0]+maxheap[0])/2
            elif len(minheap)>len(maxheap):
                median = minheap[0]
            else:
                median = maxheap[0]
        print median
def approx_median_heap(arr, sigma):
    min_heap = [arr.pop(0)]
    min_l = 1
    max_heap = [arr.pop(0)]
    max_l = 1
    med = None

    while len(arr) > 0:
        # randomly samply item from arr
        elem = arr.pop(random.randint(0, len(arr) - 1))

        # append to shorter heap
        if len(min_heap) > len(max_heap):
            max_heap.insert(0, elem)
            heapq._siftup_max(max_heap, 0)
            max_l += 1
        else:
            heapq.heappush(min_heap, elem)
            min_l += 1
        # check if heap elems need to be swapped
        while min_heap[0] < max_heap[0]:
            temp1 = max_heap[0]
            temp2 = min_heap[0]
            heapq._heappushpop_max(max_heap, temp2)
            heapq.heappushpop(min_heap, temp1)

        # calculate median
        if min_l > max_l:
            med = min_l[0]
        elif max_l > min_l:
            med = max_l[0]
        else:
            med = (min_l[0] + max_l[0]) / 2

        # check if certainly within sigma
        radius = len(arr)  # unchecked vars
        # how do i check when heaps aren't fully sorted????
    return (min_heap, max_heap)
 def pushpopleft(self, val):
     # self._offset += 1
     return heapq._heappushpop_max(self._left, val)
示例#13
0
 def collect(self):
     if self.order:
         return [heapq._heappushpop_max(self._data, None)[1] for i in range(len(self._data))]
     else:
         return [heappop(self._data)[1] for i in range(len(self._data))]
 def pushpopleft(self, val):
     # self._offset += 1
     return heapq._heappushpop_max(self._left, val)
import heapq

A = [54, 3, 534, 543, 33]

heapq._heapify_max(A)

heapq._heappushpop_max(A, 9999)

print A
示例#16
0
                current_samples.append(
                    Sample(image_name, current_score, crop_top, crop_left, crop_top + win_height, crop_left + win_width)
                )
        else:
            current_samples = [Sample(image_name)]

        if classification[0] == category.name:
            scores[index] = 1
            category.correct_samples += current_samples
        else:
            for sample in current_samples:
                if len(category.incorrect_samples) < (num_images):
                    category.incorrect_samples.append(sample)
                    heapq._heapify_max(category.incorrect_samples)
                else:
                    heapq._heappushpop_max(category.incorrect_samples, sample)

        cv2.imwrite(output_folder + classification[0] + "_" + image_name, classification[1])
        print "{} [{} out of {}]".format(classification[0], index + 1, num_images)

    print "Accuracy = %.2f%%" % (100.0 * np.sum(scores) / num_images)

########################################################################################
# Re train on incorrect results from negative incorrect samples
female_category = next(category for category in categories if category.name == "female")
images_folder = "data/segmented_image/colour/"

# TODO - REMOVE A SAMPLE FROM THE SAME IMAGE YOU ARE REPLACING WITH - IF POSSIBLE?
output_folder = "data/cropped_wing/female/"
number_to_delete = len(female_category.incorrect_samples)
for old_image_name in os.listdir(output_folder):
import heapq

A = [54,3,534,543,33]


heapq._heapify_max(A)

heapq._heappushpop_max(A,9999)

print A