Exemple #1
0
    def add(self, val: int) -> int:
        if len(self.stream) < self.k:
            heappush(self.stream, val)
        elif val > self.stream[0]:      # if the new value is greater than the smallest element in the min heap,
            # then it's one of the largest elements greater than kth. else it's even smaller, ignore it
            heappop(self.stream)
            heappush(self.stream, val)

        return self.stream[0]
Exemple #2
0
    def addNum(self, num: int) -> None:
        heappush(self.maxHeap, num)
        heapq._heapify_max(self.maxHeap)
        topOfMax = heappop(self.maxHeap)
        heappush(self.minHeap, topOfMax)
        heapq.heapify(self.minHeap)

        if len(self.maxHeap) < len(self.minHeap):
            topOfMin = heappop(self.minHeap)
            heappush(self.maxHeap, topOfMin)
Exemple #3
0
def construct(tree):
    while (len(tree) > 1):
        left = _heapq.heappop(tree)
        right = _heapq.heappop(tree)
        root = (left[0] + right[0], left[1] + right[1],
                Tree(left[0] + right[0], left[1] + right[1]))
        root[2].left = left[2]
        root[2].right = right[2]
        _heapq.heappush(tree, root)
    return tree
Exemple #4
0
 def insert_num(self, num):
     """这里面记住最小值在0位"""
     if not self.left or num <= -self.left[0]:
         _heapq.heappush(self.left, -num)
     else:
         _heapq.heappush(self.right, num)
     if len(self.left) > len(self.right) + 1:
         tmp = -_heapq.heappop(self.left)
         _heapq.heappush(self.right, tmp)
     elif len(self.right) > len(self.left) + 1:
         tmp = -_heapq.heappop(self.right)
         _heapq.heappush(self.left, tmp)
    def kthSmallest(self, root, k) -> int:
        heap = []
        self.inorder_traversal(
            root, heap
        )  # get the elements in a list so that heap properties can be applied
        heapq.heapify(heap)  # heapify to form a min heap (default)

        while k - 1:  # pop k-1 elements so that the top element is the kth smallest one
            heappop(heap)
            k -= 1

        return heap[0]
Exemple #6
0
    def _dictionary(self, dictfile, docfreq, k):
        """
            This function reads the file which contains the token list and
            their frequency in the entire training corpus. Then it selects the
            top k tokens and generates the dictionary.
            
            Parameters
            -----------
            dictfile: string
                path of file containing the tokens in training corpus and their 
                frequencies.
            docfreq: string
                path of the file containing the term-document frequencies. This is
                needed to the tf-idf calculations.
            k: int
                dictionary size
            
            Note
            -----
            this method creates an private variable self._vocab which 
            contains the frequent tokens and an unique id associated with them
            and self._idf which stores the term frequency of each token in self._vocab
        """
        
        temp_vocab = []
        

        data = pickle.load(open(dictfile, "r"))
        df = pickle.load(open(docfreq,"r"))
        for line in data.keys():
            # if the size of heap is smaller than k then add element
            if len(temp_vocab) <= k:
                heappush(temp_vocab, (int(data[line]), line))
            else:
            #if the size of heap is greater than k then replace smallest element
                heapreplace(temp_vocab, (int(data[line]), line))
                
        heappop(temp_vocab)  # remove the extra element
        
        self._vocab = dict()
        itr = 0
        self._idf = list()
        
        for term in list(temp_vocab):
            #convert heap into dictionary keyed on the token and value is token id
            self._vocab[term[1]] = itr
            #store the term-document frequency in case of tfidf calculations
            self._idf.append(df[term[1]])
            itr += 1
        self._idf = np.array(self._idf,dtype="float")
        self._idf = self._idf/1255353 #total number of documents is 1255353
        self._idf = np.log(self._idf)
def pop_task():
    while pq:
        priority, count, task = heappop(pq)
        if task is not REMOVED:
            del entry_finder[task]
            return task
    raise KeyError('pop form an empty prioriry queue')
Exemple #8
0
def djikstra(nodes,links,source,dest):
    """An implementation of Djikstra's Algorithm for our Node and Link classes"""
    route = []
    vertexes = []
    for v in nodes:
        v.set_dist(float("inf"))
        v.set_prev(None)
        heappush(vertexes, v)
    source.set_dist(0)
    heapify(vertexes)
    while vertexes:
        unsorted = False
        u = heappop(vertexes)
        if u == dest:
            break #because we found the destination no need to look further
        for v in u.get_links():
            if v.get_enabled():
                alt = u.get_dist() + 1
                target = v.get_target()
                if alt < target.get_dist():
                    target.set_dist(alt)
                    target.set_prev(u)
                    unsorted = True #just a variable that help check if changes were made to the objects inside the heap
            if unsorted: #because i updated the variables but the heap wasn't maintained, i just heapify it again
                heapify(vertexes) 
    #this is the part that saves the distance and route  
    if dest.get_dist() == float("inf"): #if there is no route then we just return None
        return None
    u = dest
    while u.get_prev() != None:
        v = u.get_prev()
        route.insert(0, v.get_specific_link(u)) 
        u = v
    return route
    def run(self):
        # First create the reporting times list
        reporting_times = numpy.arange(self.reportingInterval,
                                       self.timeHorizon,
                                       self.reportingInterval)

        prev_time = 0.0
        # Iterate over the queue. The (delayed) events will be added to the queue as they are trigerred.
        for next_time in reporting_times:
            self.log.Message(
                "Simulating from {0:.5f}s to {1:.5f}s...".format(
                    prev_time, next_time), 0)

            try:
                while True:
                    # Get the first item from the heap
                    (t_event, inlet_event_port,
                     target_neurone) = heappop(self.events_heap)

                    # If out of the interval put it back
                    if t_event > next_time:
                        heappush(self.events_heap,
                                 (t_event, inlet_event_port, target_neurone))
                        break

                    #print('{0} --> {1}s (trigger event on {2})'.format(target_neurone.Name, t_event, inlet_event_port.CanonicalName))

                    # Integrate until next event time and report the data if there is a discontinuity
                    target_neurone.simulation.IntegrateUntilTime(
                        t_event, pyActivity.eDoNotStopAtDiscontinuity, True)

                    # Trigger the event and reinitialize the system
                    inlet_event_port.ReceiveEvent(t_event)
                    target_neurone.simulation.Reinitialize()

                    self.spike_count += 1

            except IndexError:
                pass

            # Integrate each neurone until the *next_time* is reached and report the data
            for simulation in self.simulations:
                #print('{0}........ {1} {2} {3}'.format(simulation.m.Name,
                #                                       simulation.CurrentTime,
                #                                       '<' if simulation.CurrentTime < next_time else '=' ,
                #                                       next_time))
                if simulation.CurrentTime < next_time:
                    simulation.IntegrateUntilTime(
                        next_time, pyActivity.eDoNotStopAtDiscontinuity, True)
                simulation.ReportData(next_time)

            # Set the progress
            self.log.SetProgress(100.0 * next_time / self.timeHorizon)
            prev_time = next_time

        print('Simulation has ended successfuly.')
        print('Processing the results...')

        # Finally, process the results (generate 2D plots, raster plots, and other voodoo-mojo stuff)
        self.processResults()
Exemple #10
0
    def leastInterval(self, tasks, n):
        """
        :type tasks: List[str]
        :type n: int
        :rtype: int
        """
        heap, m = [], collections.defaultdict(int)
        for t in tasks:
            m[t] += 1

        res = 0
        for k, v in m.iteritems():
            heappush(heap, [-v, k])

        while heap:
            temp = []
            l = min(n + 1, len(heap))
            for i in xrange(l):
                item = heappop(heap)
                item[0] = item[0] + 1
                if item[0] <= 0:
                    temp.append(item)

            if not temp:
                break
            res += len(temp)
            if temp[0][0] != 0:
                res += max(0, n + 1 - len(temp))
            for t in temp:
                heappush(heap, t)

        return res
Exemple #11
0
    def find(self, origin, destination):
        frontier = []
        heappush(frontier, (0, origin))
        came_from = {origin: None}
        accumulated_cost = defaultdict(lambda: float('inf'))
        accumulated_cost[origin] = 0

        while frontier:
            current = heappop(frontier)[1]

            if current == destination:
                break

            for next, base_cost in self.grid.neighbors(*current):
                calculated = self.cost(current, next) * base_cost
                if not calculated:
                    continue
                new_cost = accumulated_cost[current] + calculated
                if new_cost < accumulated_cost[next]:
                    accumulated_cost[next] = new_cost
                    priority = new_cost + self.heuristic(next, destination)
                    heappush(frontier, (priority, next))
                    came_from[next] = current

        current = destination
        yield current
        while current != origin:
            current = came_from[current]
            yield current
Exemple #12
0
def solution(scoville, k):
    answer = 0

    h = []

    for i in scoville:
        _heapq.heappush(h, i)
    print(3**2)
    while h[0] < k:
        try:
            _heapq.heappush(h, _heapq.heappop(h) + (_heapq.heappop(h) * 2))

        except Exception:
            return -1
        answer += 1
    return answer
def uniform_cost_search(startState, goalState, image):
    frontier = []  # lowest cost comes out first
    startState.cost = 0.0
    startState.parent = None
    heappush(frontier, startState)
    beenthere = {startState : 0}  
       
    width, height = image.size
    pixels = image.load()
      
    it = 0  
    while len(frontier) > 0:
        s = heappop(frontier)    #Mystate
        if s == goalState:    #return state which reached goal
            return s
        actions = options(s, width, height)
        if ((it % 5000) < 1000):
            pixels[s.x, s.y] = (0, 255, 0)
        it+=1 
        for a in actions:
            child = transition(s, a, pixels)       # compute the next state
            acost = action_cost(s, a, pixels)      # compute the cost of the action
            
            c_cost = beenthere.get(child)
            if c_cost != None:               #if child is inside of beenthere
                if s.cost + acost < c_cost:
                    child.cost = s.cost + acost
                    child.parent = s;
                    beenthere[child] = child.cost
            else:
                child.cost = s.cost + acost;
                child.parent = s;
                heappush(frontier, child)  
                beenthere[child] = child.cost  
    raise Exception("No path to traverse")
Exemple #14
0
 def dequeue(self):
     """Remove and return the lowest priority Item"""
     while self.pq:
         item = heappop(self.pq)[1]
         if item in self.entry_finder:
             del self.entry_finder[item]
             return item
     return None
def fa_cardio_scatter_plot(path):
    data_set = 'cardio'
    x_train, y_train = load_data(path + 'data/' + data_set + '/train/')

    pca = TruncatedSVD(n_components=5)
    pca_x_train = pca.fit_transform(x_train)

    kmeans = KMeans(n_clusters=10, random_state=0).fit(pca_x_train)
    print(kmeans.labels_)
    p = kmeans.predict(pca_x_train)

    for i in range(15):
        print(str(i) + " " + str(len(p[p == i])))

    h = []
    for i in range(15):
        heappush(h, (-1 * len(p[p == i]), i))
        print(str(i) + " " + str(len(p[p == i])))

    index = heappop(h)[1]
    plt.scatter(pca_x_train[:, 0][p == index].ravel(),
                pca_x_train[:, 1][p == index].ravel(),
                alpha=.1,
                color='red')
    index = heappop(h)[1]
    plt.scatter(pca_x_train[:, 0][p == index].ravel(),
                pca_x_train[:, 1][p == index].ravel(),
                alpha=.1,
                color='orange')
    index = heappop(h)[1]
    plt.scatter(pca_x_train[:, 0][p == index].ravel(),
                pca_x_train[:, 1][p == index].ravel(),
                alpha=.1,
                color='blue')
    index = heappop(h)[1]
    plt.scatter(pca_x_train[:, 0][p == index].ravel(),
                pca_x_train[:, 1][p == index].ravel(),
                alpha=.1,
                color='red')

    plt.xlabel('SVD Component 1')
    plt.ylabel('SVD Component 2')
    plt.title('Cardiovascular Data Representation after SVD')
    # plt.show()
    plt.savefig("plots/svd_cardio.png")
 def check(self):
     if not self.call_times:
         return
     current_time = int(round(time.time() * 1000))
     while self.call_times and self.call_times[0] <= current_time:
         call_time = heappop(self.call_times)
         for func in self.requests[call_time]:
             func()
         self.requests.pop(call_time)
Exemple #17
0
    def _handle_timeouts(self, timeout):
        if timeout > 0.0:
            time.sleep(timeout)

        current_time = time.time()

        while self._timeouts and (self._timeouts[0][0] <= current_time):
            item = heapq.heappop(self._timeouts)[1]
            item._handle_timeout(self)
 def check(self):
     if not self.call_times:
         return
     current_time = int(round(time.time() * 1000))
     while self.call_times and self.call_times[0] <= current_time:
         call_time = heappop(self.call_times)
         for func in self.requests[call_time]:
             func()
         self.requests.pop(call_time)
Exemple #19
0
    def _handle_timeouts(self, timeout):
        if timeout > 0.0:
            time.sleep(timeout)

        current_time = time.time()

        while self._timeouts and (self._timeouts[0][0] <= current_time):
            item = heapq.heappop(self._timeouts)[1]
            item._handle_timeout(self)
def grp_cardio_scatter_plot(path):
    data_set = 'cardio'
    x_train, y_train = load_data(path + 'data/' + data_set + '/train/')

    grp = GaussianRandomProjection(n_components=3)
    grp_x_train = grp.fit_transform(x_train)

    kmeans = KMeans(n_clusters=10, random_state=0).fit(grp_x_train)
    print(kmeans.labels_)
    p = kmeans.predict(grp_x_train)

    p = kmeans.predict(grp_x_train)

    h = []
    for i in range(15):
        heappush(h, (-1 * len(p[p == i]), i))
        print(str(i) + " " + str(len(p[p == i])))

    index = heappop(h)[1]
    plt.scatter(grp_x_train[:, 0][p == index].ravel(),
                grp_x_train[:, 1][p == index].ravel(),
                alpha=.1,
                color='yellow')
    index = heappop(h)[1]
    plt.scatter(grp_x_train[:, 0][p == index].ravel(),
                grp_x_train[:, 1][p == index].ravel(),
                alpha=.1,
                color='orange')
    index = heappop(h)[1]
    plt.scatter(grp_x_train[:, 0][p == index].ravel(),
                grp_x_train[:, 1][p == index].ravel(),
                alpha=.1,
                color='blue')
    index = heappop(h)[1]
    plt.scatter(grp_x_train[:, 0][p == index].ravel(),
                grp_x_train[:, 1][p == index].ravel(),
                alpha=.1,
                color='red')
    plt.xlabel('GRP Component 1')
    plt.ylabel('GRP Component 2')
    plt.title('Cardiovascular Data Representation after GRP')
    plt.savefig("plots/grp_cardio.png")

    print()
Exemple #21
0
def heapsort(iterable):
    h = []
    result = []
    # 모든 원소를 차례대로 힙에 삽입
    for value in iterable:
        _heapq.heappush(h, value)
    # 힙에 삽입된 모든 원소를 차례대로 꺼내어 담기
    for i in range(len(h)):
        result.append(_heapq.heappop(h))
    return result
 def findKthLargest_1(self, nums, k):
     """
     :type nums: List[int]
     :type k: int
     :rtype: int
     """
     import _heapq
     if k > len(nums) + 1:
         return None
     if k == len(nums) + 1:
         return max(nums)
     heap_keep = []
     for i in range(len(nums)):
         _heapq.heappush(heap_keep, nums[i])
         if len(heap_keep) > k:
             _heapq.heappop(heap_keep)
     if len(heap_keep) < k:
         return -1
     return _heapq.heappop(heap_keep)
 def Idle(self):
     time_ms = time.monotonic() * 1000
     while self._tasks_pq:
         entry = _heapq.heappop(self._tasks_pq)
         if entry[0] > time_ms:
             # Entry delay condition not met. Put back on queue and wait until next refresh cycle.
             _heapq.heappush(self._tasks_pq, entry)
             return
         task = entry[1]
         task()
Exemple #24
0
def solution(v, k, l):
    # 방문 배열 만들어주고
    # visted = [0] * (v + 1)
    # 출발 노드 방문 처리

    # 최솟값 배열
    minArray = [int(1e9)] * (v + 1)
    minArray[k] = 0

    # 경로값 만들어 주고
    # route = [[int(1e9) for _ in range(v + 1)] for _ in range(v + 1)]
    graph = [[] for _ in range(v + 1)]

    # 경로 바꿔 주기 -> 양쪽 모든 간선을 연결 해줘야 한다
    for element in l:
        # route[element[0]][element[1]] = element[2]
        # route[element[1]][element[0]] = element[2]
        graph[element[0]].append((element[1], element[2]))
        # graph[element[1]].append((element[0], element[2])) // 단방향 그래프 ?

        q = []
        # 최소힙 사용 시간복잡도 줄이기
        # 힙푸시를 이용하기 0 = k로 가는 최소값
        _heapq.heappush(q, (0, k))

        # 큐가 있을때
    while q:
        # dist = 비용 , now = 현재 노드
        # 최단거리의 노드 끄내기, heapop 은 가장 작은 값이 출력된다. 비용이
        dist, now = _heapq.heappop(q)
        # 방문된 노드라 라면 무시하기-> 이미 처리가 끝났슴
        # if visted[now] == 1:
        #     continue
        # 방문처리
        # visted[now] = 1

        # 지금 볼려는 곳이, 들어온 경로 보다 작으면 굳이 비교 할 필요가 없음,
        if minArray[now] < dist:
            continue

        # 현재 노드의 비용들을 돌아보면서
        for cost in graph[now]:
            # 새로운 비용은 현재 거리랑, 최소 경로를 더해 주는데
            newCost = dist + cost[1]
            # newCost = route[now][cost] + dist
            # 현재 노드를 거쳐서, 다른 노드로 이동하는 거리가 더 짧은 경우
            if newCost < minArray[cost[0]]:
                minArray[cost[0]] = newCost
                _heapq.heappush(q, (newCost, cost[0]))

    # print(l)
    # print(route)
    # print(minArray)

    return minArray
Exemple #25
0
 def _duration_timeout(self):
     count = self._duration_timer.read()
     expire, task = heappop(self._expireq)
     task.stop()
     self._poller.unregister(task)
     task.close()
     if self._expireq:
         self._duration_timer.settime(self._expireq[0][0], 0.0, absolute=True)
     else:
         self._duration_timer.settime(0.0, 0.0)
         self._poller.unregister_fd(self._duration_timer.fileno())
def fastest_path_estimation(sol):
    """
    Returns the time spent on the fastest path between
    the current vertex c and the ending vertex pm
    """

    class Path:
        def __init__(self, places, graph):
            self.g = 0  # current cost
            self.graph = graph
            self.visited = [places[0]]  # list of already visited attractions
            self.not_visited = copy.deepcopy(places[1:])  # list of attractions not yet visited

        def __lt__(self, other):
            return self.g < other.g

        def add(self, idx):
            # add the cost
            self.g += self.graph[self.visited[-1], idx]
            # add the to the visited place and remove from the unvisited places
            self.visited.append(idx)
            self.not_visited.remove(idx)

    def add_to_heap_queue(path):
        # custom function to add to heap queue sorted by the solution's cost
        heappush(h_queue, path)

    if len(sol.not_visited) == 0:
        return 0
    elif len(sol.not_visited) == 1:
        return sol.graph[sol.visited[-1], sol.not_visited[0]]

    c = sol.visited[-1]
    pm = sol.not_visited[-1]
    # the heap queue of solution sorted by their cost - change all to tuples with g for dijkstra
    h_queue = []

    # the places to use for the graph
    sub_search_places = [c]
    sub_search_places.extend(sol.not_visited)

    # push the first "node" in the queue
    add_to_heap_queue(Path(sub_search_places, sol.graph))
    while True:
        # take the next solution with the shortest cost
        path = heappop(h_queue)
        # if it contains destination, stop and return that solution
        if pm in path.visited:
            return path.g
        # create a new solution for each neighbor of the current vertex and add it to heap queue
        for place in path.not_visited:
            new_path = copy.deepcopy(path)
            new_path.add(place)
            add_to_heap_queue(new_path)
Exemple #27
0
    def minMeetingRooms(self, intervals: list[list[int]]) -> int:
        if not intervals:
            return 0

        intervals.sort()

        # End time
        free_rooms = []
        heappush(free_rooms, intervals[0][1])

        for i in intervals[1:]:

            # Here is i[start] time is less than the earlier finish time
            # for those meetings in heap
            # insert this in to heap
            if free_rooms[0] <= i[0]:
                heappop(free_rooms)

            heappush(free_rooms, i[1])

        return len(free_rooms)
Exemple #28
0
    def start(self, script):
        self.inicio = Nodo(script)
        self.final = Nodo()
        lista_abierta = []
        lista_cerrada = {}
        
        heappush(lista_abierta, self.inicio)

        while(lista_abierta):

            nodo_actual = heappop(lista_abierta)
            while(nodo_actual.g > len(script)):
                nodo_actual = heappop(lista_abierta)

            if(nodo_actual == self.final):
                print "Solucion Encontrada"
                self.lista_cerrada = lista_cerrada
                print 'Estados recorridos: '+str(len(lista_cerrada))
                print 'Estados a visitar: '+str(len(lista_abierta))
                print 'g: '+str(nodo_actual.g)
                print 'h: '+str(nodo_actual.h)
                return self.devolverRuta(nodo_actual)
            
            lista_cerrada[nodo_actual] = 'eliminado'
            
            for y in nodo_actual.gethijos():
                if(not y in lista_cerrada):
                    
                    if(y.g <= len(script)):
                        a = self.seek(lista_abierta,y)
                        if (a == None):
                            heappush(lista_abierta,y)
                        elif(nodo_actual.g + 1 < y.g):
                            a.g = y.g
                            a.h = y.h
                            a.f = y.f
                            a.padre = y.padre
                            a.script = y.script
                            heapify(lista_abierta)
        return "Ha ocurrido un Error"
 def run(self):
     # First create the reporting times list
     reporting_times = numpy.arange(self.reportingInterval, self.timeHorizon, self.reportingInterval)
     
     prev_time = 0.0
     # Iterate over the queue. The (delayed) events will be added to the queue as they are trigerred.
     for next_time in reporting_times:
         self.log.Message("Simulating from {0:.5f}s to {1:.5f}s...".format(prev_time, next_time), 0)
         
         try:
             while True:
                 # Get the first item from the heap
                 (t_event, inlet_event_port, target_neurone) = heappop(self.events_heap)
                 
                 # If out of the interval put it back
                 if t_event > next_time:
                     heappush(self.events_heap, (t_event, inlet_event_port, target_neurone))
                     break
                 
                 #print('{0} --> {1}s (trigger event on {2})'.format(target_neurone.Name, t_event, inlet_event_port.CanonicalName))
                 
                 # Integrate until next event time and report the data if there is a discontinuity
                 target_neurone.simulation.IntegrateUntilTime(t_event, pyActivity.eDoNotStopAtDiscontinuity, True)
                 
                 # Trigger the event and reinitialize the system
                 inlet_event_port.ReceiveEvent(t_event)
                 target_neurone.simulation.Reinitialize()
                 
                 self.spike_count += 1
         
         except IndexError:
             pass
             
         # Integrate each neurone until the *next_time* is reached and report the data
         for simulation in self.simulations:
             #print('{0}........ {1} {2} {3}'.format(simulation.m.Name, 
             #                                       simulation.CurrentTime, 
             #                                       '<' if simulation.CurrentTime < next_time else '=' , 
             #                                       next_time))
             if simulation.CurrentTime < next_time:
                 simulation.IntegrateUntilTime(next_time, pyActivity.eDoNotStopAtDiscontinuity, True)
             simulation.ReportData(next_time)
         
         # Set the progress
         self.log.SetProgress(100.0 * next_time / self.timeHorizon)
         prev_time = next_time
                 
     print('Simulation has ended successfuly.')
     print('Processing the results...')
     
     # Finally, process the results (generate 2D plots, raster plots, and other voodoo-mojo stuff)
     self.processResults()
Exemple #30
0
 def _duration_timeout(self):
     count = self._duration_timer.read()
     expire, task = heappop(self._expireq)
     task.stop()
     self._poller.unregister(task)
     task.close()
     if self._expireq:
         self._duration_timer.settime(self._expireq[0][0],
                                      0.0,
                                      absolute=True)
     else:
         self._duration_timer.settime(0.0, 0.0)
         self._poller.unregister_fd(self._duration_timer.fileno())
Exemple #31
0
def grp_loan_distribution_plot(path):

    data_set = 'loan'
    x_train, y_train = load_data(path + 'data/' + data_set + '/train/')

    ica = GaussianRandomProjection(n_components=4)
    ica.fit_transform(x_train)

    ica_x_train = ica.transform(x_train)

    kmeans = KMeans(n_clusters=3, random_state=0).fit(x_train)
    print(kmeans.labels_)

    p = kmeans.predict(x_train)

    h = []
    for i in range(15):
        heappush(h, (-1 * len(p[p == i]), i))
        print(str(i) + " " + str(len(p[p == i])))

    index_1 = heappop(h)[1]
    index_2 = heappop(h)[1]
    index_3 = heappop(h)[1]

    dist_1 = ica_x_train[:, 0][p == index_1].ravel()
    dist_2 = ica_x_train[:, 0][p == index_2].ravel()
    dist_3 = ica_x_train[:, 0][p == index_3].ravel()

    sns.distplot(dist_1, hist=False, rug=True, color='red')
    sns.distplot(dist_2, hist=False, rug=True, color='blue')
    sns.distplot(dist_3, hist=False, rug=True, color='orange')

    plt.title('Financial Loan GRP Distribution')
    plt.savefig('plots/loan_grp_distribution.png')

    print("")
    print(kurtosis(dist_1))
    print(kurtosis(dist_2))
    print(kurtosis(dist_3))
 def scheduleCourse2(self, courses):
     """
     :type courses: List[List[int]]
     :rtype: int
     """
     heap, curTime, res = [], 0, 0
     courses.sort(key=lambda c: c[1])
     for k, v in enumerate(courses):
         curTime += v[0]
         heappush(heap, -v[0])
         if curTime > v[1] and heap:  # bugfixed need to check if heap is empty or not
             item = heappop(heap)
             curTime += item
     return len(heap)
    def leastInterval(self, tasks, n):
        """
        :type tasks: List[str]
        :type n: int
        :rtype: int
        """
        heap, m = [], collections.defaultdict(int)
        for t in tasks:
            # got the frequency of the element, we could use `Counter` instead, more concise
            m[t] += 1

        res = 0
        for k, v in m.iteritems():
            heappush(heap, [-v, k])

        while heap:
            temp = []
            # Example 1:
            # Input: tasks = ["A","A","A","B","B","B"], n = 2
            # Output: 8
            # Explanation: A -> B -> idle -> A -> B -> idle -> A -> B.
            # for this test case, n+1 = 3, len(heap) = 2, interval might > or < the task #
            l = min(n + 1, len(heap))
            for i in xrange(l):
                # greedy, we firstly pick the most frequent element
                item = heappop(heap)
                # after choose one task, reduce the freq by 1, remember the freq is < 0
                item[0] = item[0] + 1
                if item[0] <= 0:
                    temp.append(item)

            # if all tasks are already finshed( heap is empty)
            if not temp:
                break
            # how many tasks been put in temp, let's say [[A,0], [B,0]]
            res += len(temp)
            # for [[A,0], [B,0]], means we have already finised all tasks
            # also, it is possible have [[A,2], [B,0]..] if A task more than B
            # but no matter what, the front one would always be the most frequent task, if it becomes 0
            # means no more task any more
            # but if we have [[A,1], [B,1]], n = 2, means we need an idle there, A, B, idle, so we need
            # n+1 - len(temp)
            if temp[0][0] != 0:
                res += max(0, n + 1 - len(temp))
            # we need to push it back ,even for [A,0], [B,0] , then at next for loop, it would become +1
            # and it would go to `if not temp: break`.
            for t in temp:
                heappush(heap, t)

        return res
Exemple #34
0
def ica_loan_scatter_plot(path):

    data_set = 'loan'
    x_train, y_train = load_data(path + 'data/' + data_set + '/train/')

    ica = FastICA(n_components=2)
    ica.fit_transform(x_train)

    ica_x_train = ica.transform(x_train)

    kmeans = KMeans(n_clusters=10, random_state=0).fit(ica_x_train)
    print(kmeans.labels_)

    p = kmeans.predict(ica_x_train)

    for i in range(15):
        print(str(i) + " " + str(len(p[p==i])))

    h = []
    for i in range(15):
        heappush(h, (-1 * len(p[p==i]), i))
        print(str(i) + " " + str(len(p[p==i])))

    index = heappop(h)[1]
    plt.scatter(ica_x_train[:, 0][p==index].ravel(), ica_x_train[:,1][p==index].ravel(), alpha=.1, color='red')
    index = heappop(h)[1]
    plt.scatter(ica_x_train[:, 0][p==index].ravel(), ica_x_train[:,1][p==index].ravel(), alpha=.1, color='orange')
    index = heappop(h)[1]
    plt.scatter(ica_x_train[:, 0][p==index].ravel(), ica_x_train[:,1][p==index].ravel(), alpha=.1, color='blue')

    plt.xlabel('ICA Component 1')
    plt.ylabel('ICA Component 2')
    plt.title('Financial Loan Data Representation after ICA')
    # plt.show()
    plt.savefig("plots/ica_loan.png")

    print(x_train.shape)
Exemple #35
0
def watershed(image, markers, connectivity=8, mask=None):
    """Return a matrix labeled using the watershed algorithm
    
    image - a two-dimensional matrix where the lowest value points are
            labeled first.
    markers - a two-dimensional matrix marking the basins with the values
              to be assigned in the label matrix. Zero means not a marker.
    connectivity - either 4 for four-connected or 8 (default) for eight-
                   connected
    mask    - don't label points in the mask
    """
    if connectivity not in (4, 8):
        raise ValueError(
            "Connectivity was %d: it should be either four or eight" %
            (connectivity))

    image = numpy.array(image)
    markers = numpy.array(markers)
    labels = markers.copy()
    max_x = markers.shape[0]
    max_y = markers.shape[1]
    if connectivity == 4:
        connect_increments = ((1, 0), (0, 1), (-1, 0), (0, -1))
    else:
        connect_increments = ((1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0),
                              (-1, -1), (0, -1), (1, -1))
    pq, age = __heapify_markers(markers, image)
    #
    # The second step pops a value off of the queue, then labels and pushes
    # the neighbors
    #
    while len(pq):
        pix_value, pix_age, ignore, pix_x, pix_y = heappop(pq)
        pix_label = labels[pix_x, pix_y]
        for xi, yi in connect_increments:
            x = pix_x + xi
            y = pix_y + yi
            if x < 0 or y < 0 or x >= max_x or y >= max_y:
                continue
            if labels[x, y]:
                continue
            if mask != None and not mask[x, y]:
                continue
            # label the pixel
            labels[x, y] = pix_label
            # put the pixel onto the queue
            heappush(pq, (image[x, y], age, 0, x, y))
            age += 1
    return labels
Exemple #36
0
    def kSmallestSumPairs(
            self, arr1, arr2,
            k):  # Return up to `k` smallest sum of pairs of `arr1` and `arr2`
        minHeap = []
        for c in range(min(len(arr1), k)):
            heappush(minHeap, (arr1[c] + arr2[0], c, 0))

        ans = []
        while k > 0 and minHeap:
            _sum, c1, c2 = heappop(minHeap)
            ans.append(_sum)
            if c2 + 1 < len(arr2):
                heappush(minHeap, (arr1[c1] + arr2[c2 + 1], c1, c2 + 1))
            k -= 1
        return ans
Exemple #37
0
 def getPotentialFriends(self,userid,count):
     '''
     used a min heap to get the top 'count' potential friends of a user.
     space complexity of min heap is O(count)
     insert into heap q and delete takes log(count) time
     printing the list in reverse order to get users with maximum count: complexity O(n)
     Overall runtime complexity is O(n)
     '''
     if userid not in self.friendmap:
         print "User does not exist"
         return  None
     minh = []
     potentialfriends = [] 
     for key,value in self.friendmap.iteritems():
         if  userid != key:
             if not self.areFriend(userid, key):
                 c = self.getMutualFriendCount(userid,key)
                 if len(minh) >= count:
                     heappop(minh)
                 heappush(minh,(c,key))
     
     for friend in minh[::-1]:
         potentialfriends.append(friend[1])   
     return potentialfriends         
    def dijkstra_distance_between_experiences(self, id1, id2):
        exp_heap = Q.PriorityQueue()
        for id in range(len(self.experiences)):
            self.experiences[
                id].time_from_current_s = self.DBL_MAX  #DBL_MAX ???
            exp_heap.put(self.experiences[id])
        self.experiences[id1].time_from_current_s = 0
        self.goal_path_final_exp_id = self.current_exp_id

        while not exp_heap.empty():
            self.make_heap(Experience(exp_heap), len(exp_heap))
            exp = Experience(exp_heap[0])
            if (exp.time_from_current_s == self.DBL_MAX):
                return self.DBL_MAX
            heappop(exp_heap)

            for id in range(len(exp.links_from)):
                link = self.links[exp.links_from[id]]
                link_time_s = exp.time_from_current_s + link.delta_time_s
                if link_time_s < self.experiences[
                        link.exp_from_id].time_from_current_s:
                    self.experiences[
                        link.exp_from_id].time_from_current_s = link_time_s
                    self.experiences[link.exp_from_id].goal_to_current = exp.id
            for id in range(len(exp.links_from)):
                link = self.links[exp.links_from[id]]
                link_time_s = exp.time_from_current_s + link.delta_time_s
                if link_time_s < self.experiences[
                        link.exp_to_id].time_from_current_s:
                    self.experiences[
                        link.exp_to_id].time_from_current_s = link_time_s
                    self.experiences[link.exp_to_id].goal_to_current = exp_id

            if exp.id == id2:
                return exp.time_from_current_s
        return self.DBL_MAX
Exemple #39
0
def watershed(image, markers, connectivity=8, mask=None):
    """Return a matrix labeled using the watershed algorithm
    
    image - a two-dimensional matrix where the lowest value points are
            labeled first.
    markers - a two-dimensional matrix marking the basins with the values
              to be assigned in the label matrix. Zero means not a marker.
    connectivity - either 4 for four-connected or 8 (default) for eight-
                   connected
    mask    - don't label points in the mask
    """
    if connectivity not in (4,8):
        raise ValueError("Connectivity was %d: it should be either four or eight"%(connectivity))
    
    image = numpy.array(image)
    markers = numpy.array(markers)
    labels = markers.copy()
    max_x  = markers.shape[0]
    max_y  = markers.shape[1]
    if connectivity == 4:
        connect_increments = ((1,0),(0,1),(-1,0),(0,-1))
    else:
        connect_increments = ((1,0),(1,1),(0,1),(-1,1),
                              (-1,0),(-1,-1),(0,-1),(1,-1))
    pq,age = __heapify_markers(markers,image)
    #
    # The second step pops a value off of the queue, then labels and pushes
    # the neighbors
    #
    while len(pq):
        pix_value, pix_age, ignore,pix_x,pix_y = heappop(pq)
        pix_label = labels[pix_x,pix_y]
        for xi,yi in connect_increments:
            x = pix_x+xi
            y = pix_y+yi
            if x < 0 or y < 0 or x >= max_x or y >= max_y:
                continue
            if labels[x,y]:
                continue
            if mask != None and not mask[x,y]:
                continue
            # label the pixel
            labels[x,y] = pix_label
            # put the pixel onto the queue
            heappush(pq, (image[x,y],age,0,x,y))
            age += 1
    return labels
 def start(self):
     if self._verbose:
         print'------------------------------- starting event handler -------------------------------'
     while len(self._queue) > 0 and self._finished_jobs < self.all_jobs:
         event = heappop(self._queue)
         if self._verbose:
             print SimulationTime.Instance().get_time(), '->', event.time, event
         forward_time = event.time - SimulationTime.Instance().get_time()
         SimulationTime.Instance().forward(forward_time)
         for core in self._cores:
             core.forward(forward_time)
         event.handle()
         self.check_candidates()
         if (not Logger.Instance().active) and self._finished_jobs >= self.warm_up:
             Logger.Instance().activate()
         if self._verbose:
             self.print_info()
             print ''
def greedyPath(points, start):
    gPath = []  # greedy path
    dist = []   # distance heap
    curr = start
    gPath.append(curr)  # start path with start point
    
    while points:   # while there are points left
        for i, point in enumerate(points):  # get distances from current point to all other points
            heappush(dist, (distance(curr, point), i, point))   # 3-tuple [distance, point index, point coordinates]
            
        nearest = heappop(dist)    # get nearest point
        dist = []   # clear heap
        del points[nearest[1]]    # remove point from points
        curr = nearest[2]  # update current point
        gPath.append(curr)   # append point to greedyPath
    
    gPath.append(start) # return to start
    
    return gPath
def find_shortest_path(G, v):
    dist_so_far = {v: [0,[v]]}
    final_dist = {}
    heap=[]
    heappush(heap,(0,v))
    while len(final_dist) < len(G) and len(heap) :
        value,w = heappop(heap)
        if w not in final_dist:
            final_dist[w] = dist_so_far[w]
            del dist_so_far[w]
            for x in G[w]:
                if x not in final_dist:
                    path_len = G[w][x]
                    if x not in dist_so_far:
                        dist_so_far[x] = []
                        dist_so_far[x].append(final_dist[w][0] + path_len)
                        dist_so_far[x].append(final_dist[w][1] + [x])
                        heappush(heap,(final_dist[w][0] + path_len, x))
                    elif final_dist[w][0] + path_len < dist_so_far[x][0]:
                        dist_so_far[x][0] = final_dist[w][0] + path_len
                        dist_so_far[x][1] = final_dist[w][1] + [x]
                        heappush(heap,(final_dist[w][0] + path_len, x))
    return final_dist
Exemple #43
0
@author: Rahul
'''
import heapq
from _heapq import heappush, heappop
if __name__ == '__main__':
    x = [1,0,3,5,2,0,1]
    
    minh = []
    maxh = []
    maxe = 0
    equal = 0
    for e in x:
        
        if equal == 0:
            heappush(minh,e)
            mine = heappop(minh)
            heappush(minh,mine)
            print mine 
            equal = 1
        else:
            heappush(minh,e)
            temp = heappop(minh)
            heappush(maxh,-temp)
            mine = heappop(minh)
            heappush(minh,mine)
            maxe = -1 * heappop(maxh)
            heappush(maxh,-maxe)
            median = float(maxe + mine) / 2
            equal = 0
            print median
    # data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0]
    # for item in data:
    #     heappush(heap, item)
    # sort = []
    # while heap:
    #     sort.append(heappop(heap))
    # print sort

    # import doctest
    # doctest.testmod()

    # test = [10]
    # if test < data:
    #     print "sdf"
    # test = [0.1, 0.3, 0.5, 0.7, 0.9, 0.2, 0.4, 0.6, 0.8, 0.0]
    test = [ (0.1, [1, 5, 6]), (0.6, [9, 5, 6]) ]
    # tt = [[0.1, [4, 3,10,100]], [0.3, [5,5,1,23,23]], [0.5,3], [0.7, [1,5]], [0.7, [2,5]], [0.9, 1], [0.2, 4], [0.4, 10], [0.6,9], [0.8, 1], [0.0, 124]]
    heapify(test)
    print test
    # heapify(tt)
    heappush(test, (0.0, [1, 3]))
    print test
    dist, data = heappop(test)
    print dist, data
    # print tt
    

    # print pow(float(3), 2)


Exemple #45
0
    return map(itemgetter(2), result)                       # undecorate

_nlargest = nlargest
def nlargest(n, iterable, key=None):
    """Find the n largest elements in a dataset.

    Equivalent to:  sorted(iterable, key=key, reverse=True)[:n]
    """
    if key is None:
        it = izip(iterable, imap(neg, count()))             # decorate
        result = _nlargest(n, it)
        return map(itemgetter(0), result)                   # undecorate
    in1, in2 = tee(iterable)
    it = izip(imap(key, in1), imap(neg, count()), in2)      # decorate
    result = _nlargest(n, it)
    return map(itemgetter(2), result)                       # undecorate

if __name__ == "__main__":
    # Simple sanity test
    heap = []
    data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0]
    for item in data:
        heappush(heap, item)
    sort = []
    while heap:
        sort.append(heappop(heap))
    print sort

    import doctest
    doctest.testmod()
Exemple #46
0
def pop_node(data_structure, heap=False):
    return heappop(data_structure) if heap else data_structure.pop(0)
Exemple #47
0
print a

#双端队列
s = "yeah but no but yeah but no but yeah"
words = s.split()
wordlocations = defaultdict(list)
for n, w in enumerate(words) : 
    wordlocations[w].append(n)
print  wordlocations  

#堆
heap = range(1, 100)
shuffle(heap)
heapq.heapify(heap) 
print heap
print heappop(heap)
heappush(heap, 101)
heapreplace(heap, 102)
print heap

#itertools
for i in chain(range(1, 10), range(10, 20)) : 
    print i
    
#3个一组排列组合
for list in combinations(range(1,20), 3) : 
    print list

#从某个数开始,连续生产整数    
for i in count(100) : 
    if i == 200 :
Exemple #48
0
def _slow_watershed(image, markers, connectivity=8, mask=None):
    """Return a matrix labeled using the watershed algorithm

    Use the `watershed` function for a faster execution.
    This pure Python function is solely for pedagogical purposes.

    Parameters
    ----------
    image: 2-d ndarray of integers
        a two-dimensional matrix where the lowest value points are
        labeled first.
    markers: 2-d ndarray of integers
        a two-dimensional matrix marking the basins with the values
        to be assigned in the label matrix. Zero means not a marker.
    connectivity: {4, 8}, optional
        either 4 for four-connected or 8 (default) for eight-connected
    mask: 2-d ndarray of bools, optional
        don't label points in the mask

    Returns
    -------
    out: ndarray
        A labeled matrix of the same type and shape as markers


    Notes
    -----
    This function implements a watershed algorithm [1]_that apportions pixels
    into marked basins. The algorithm uses a priority queue to hold the pixels
    with the metric for the priority queue being pixel value, then the time of
    entry into the queue - this settles ties in favor of the closest marker.

    Some ideas taken from
    Soille, "Automated Basin Delineation from Digital Elevation Models Using
    Mathematical Morphology", Signal Processing 20 (1990) 171-182

    The most important insight in the paper is that entry time onto the queue
    solves two problems: a pixel should be assigned to the neighbor with the
    largest gradient or, if there is no gradient, pixels on a plateau should
    be split between markers on opposite sides.

    This implementation converts all arguments to specific, lowest common
    denominator types, then passes these to a C algorithm.

    Markers can be determined manually, or automatically using for example
    the local minima of the gradient of the image, or the local maxima of the
    distance function to the background for separating overlapping objects.
    """
    if connectivity not in (4, 8):
        raise ValueError("Connectivity was %d: it should be either \
        four or eight" % (connectivity))

    image = np.array(image)
    markers = np.array(markers)
    labels = markers.copy()
    max_x = markers.shape[0]
    max_y = markers.shape[1]
    if connectivity == 4:
        connect_increments = ((1, 0), (0, 1), (-1, 0), (0, -1))
    else:
        connect_increments = ((1, 0), (1, 1), (0, 1), (-1, 1),
                              (-1, 0), (-1, -1), (0, -1), (1, -1))
    pq, age = __heapify_markers(markers, image)
    pq = pq.tolist()
    #
    # The second step pops a value off of the queue, then labels and pushes
    # the neighbors
    #
    while len(pq):
        pix_value, pix_age, ignore, pix_x, pix_y = heappop(pq)
        pix_label = labels[pix_x, pix_y]
        for xi, yi in connect_increments:
            x = pix_x + xi
            y = pix_y + yi
            if x < 0 or y < 0 or x >= max_x or y >= max_y:
                continue
            if labels[x, y]:
                continue
            if mask is not None and not mask[x, y]:
                continue
            # label the pixel
            labels[x, y] = pix_label
            # put the pixel onto the queue
            heappush(pq, [image[x, y], age, 0, x, y])
            age += 1
    return labels
Exemple #49
0
 def pop(self):
     # Always return the least scored element
     return heappop(self.A)[1]
Exemple #50
0
 def pop(self):
     if self.size == 0:
         return None
     neg, pos = heappop(self.heap)
     self.size -= 1
     return pos