Esempio n. 1
0
    def test_remove_removesElemAndMaintainsHeapProperty(self):
        minHeap = MinHeap()
        arr = [3, 9, 12, 13, 1]
        _ = minHeap.convertArrayToHeap(arr)
        minHeap.remove(13)
        expectedHeap = [1, 3, 12, 9]

        self.assertListEqual(expectedHeap, minHeap.heap)
Esempio n. 2
0
def nearestNeighbor(trainingSet, testRow, classCol, k):
    minHeap = MinHeap(len(trainingSet.index))
    for row in trainingSet.iterrows():
        dropRow = row[1].drop(classCol)
        dist = findDistance(dropRow, testRow, classCol)
        minHeap.insert((row, dist))
    minHeap.minHeap()

    # Determine class by k neighbor voting
    votes = {}
    for i in range(k):
        minVal = minHeap.remove()
        if minVal[0][1][classCol] in votes:
            votes[minVal[0][1][classCol]] += 1
        else:
            votes[minVal[0][1][classCol]] = 1

    maxVotes = 0
    predictedClass = ""
    for key in votes:
        if votes[key] > maxVotes:
            maxVotes = votes[key]
            predictedClass = key

    return 0 if (predictedClass == testRow[1][classCol]) else 1
Esempio n. 3
0
# Add, edit, or remove tests in this file.
# Treat it as your playground!

from minHeap import MinHeap
import unittest

test1 = MinHeap([2, 3, 1])

test2 = MinHeap([1, 2, 3, 4, 5, 6, 7, 8, 9])

test3 = MinHeap([48, 12, 24, 7, 8, -5, 24, 391, 24, 56, 2, 6, 8, 41])
test3.insert(76)
test3.remove()
test3.remove()
test3.insert(87)

test3a = MinHeap([[1, 48], [1, 12], [1, 24], [1, 7], [1, 8], [1, -5], [1, 24],
                  [1, 39], [1, 24], [1, 56], [1, 2], [1, 6], [1, 8], [1, 41]])
test3a.insert([1, 76])
test3a.remove()
test3a.remove()
test3a.insert([1, 9])
test3a.insert([1, 10])
test3a.insert([1, 87])
test3a.insert([1, -87])
test3a.insert([1, 49])

test4 = MinHeap(
    [-4, 5, 10, 8, -10, -6, -4, -2, -5, 3, 5, -4, -5, -1, 1, 6, -7, -6, -7, 8])

test5 = MinHeap(