示例#1
0
def main():
    from random import seed
    from probability import genProbs
    #### Unit test for training funciton
    # Initiatializes seed for recurrent testing.
    for _ in range(10):
        dimen = (4, 3, 6, 5, 6)
        classValues = 5

        measures = MeasurementGenerator(dimen)
        classes = ClassAssign(dimen, classValues)

        conds = [list(genProbs(measures.range)) for _ in range(classValues)]
        egain = [[2, 0, 0, 0, 1], [3, 4, 0, 2, 2], [2, 2, 5, 1, 1],
                 [2, 2, 3, 4, 1], [0, 1, -3, 2, 3]]
        classifier = BayesClassifier(
            None, conds, eGain=egain
        )  # Worries that supplying similar priors is affecting our results. Even though vFold updates.
        y = train(classifier, 20, measures, classes, 6000, delta=.0005)
        z = [y[i] - y[i - 1] for i in range(1, len(y))]
        # Trying to figure out average negative error to see if this is floating point.
        print(y)
        print()
        print(z)
        q = [i for i in z if i < 0]
        q = sum(q) / max(len(q), 1)
        print(q)
        print()
    x = measures.genMeas(20)

    p = classes.assign(x)
    l = classifier.assign(x)
示例#2
0
    def __init__(self, dimen: Tuple[int]):
        self.dimen = dimen

        # Caclulates the size of space
        self.range = 1
        for N in self.dimen:
            self.range *= N

        # Generates probabilities for later measurements
        self.cmlProbs = genCmlProbs(genProbs(self.range))
示例#3
0
def genCCP(K: int, dimen: Tuple[int]) -> List[List[float]]:  #### LINEAR ARRAY?
    # Generates a randomly alotted class conditional probability array.
    # Array is of size K, each index represents a corresponding class conditional probability list
    # and each list is of size of measure space represented by dimen.

    # Generates M space length.
    N = 1
    for n in dimen:
        N *= n

    # Generates K normalized probability tables; one for each class.
    classProbs = [list(genProbs(N)) for _ in range(K)]

    return classProbs
示例#4
0
    def __init__(self, dimen: Tuple[int], K: int):
        ## Dimen is representation of measurement space M
        ## K is amount of classes to be assigned

        self.dimen = dimen
        self.probs = genProbs(K)  # For testing
        self.cmlProbs = genCmlProbs(self.probs)
        self.range = K

        # Amount of possible measures is cardinality of M as represented by dimen
        self.mSpaceSize = 1
        for N in dimen:
            self.mSpaceSize *= N

        self.tags = None
        self.genTags()
示例#5
0
def main():
    #### Unit tests

    ####### Tests for sampling function.
    # Tests for distributions to be within range
    for i in range(1, 1000):
        sampSize = randint(1, 1000)
        spaceRang = randint(3, 1000)
        a = genProbs(spaceRang)
        b = genCmlProbs(a)
        c = genSamples(sampSize, b)
        for j in c:
            assert j <= (spaceRang - 1) and j >= 0

    # Tests for suitable randomness so same samples aren't being generated.
    for _ in range(1000):

        sampSize = randint(2, 1000)
        spaceRang = randint(3, 1000)  # Provides sufficient space for variety

        a = genProbs(spaceRang)
        b = genCmlProbs(a)

        c = genSamples(sampSize, b)
        z = genSamples(sampSize, b)

        assert c != z, c

#####Tests genMeas method
# Checks for measurement space to be of proper length
    Z = randint(2, 1000)
    dimen = genSpace(6)
    size = randint(2, 1000)
    generator = MeasurementGenerator(dimen)
    x = generator.genMeas(size)
    assert len(x) == size

    # Checks for values to actually exist in measurement space
    for _ in range(1, 1000):
        maxValue = 1
        for i in dimen:
            maxValue *= i
        for j in x:
            assert j <= maxValue - 1, maxValue - j


###### Tests ClassAssign class
# Verifies no class is assigned outside class values
    for _ in range(1000):
        dimen = genSpace(5)
        maxValue = 1
        for i in dimen:
            maxValue *= i
        tagSize = randint(1, 100)
        tagAssign = ClassAssign(dimen, tagSize)

        for j in tagAssign.tags:
            assert j <= tagSize - 1, (j, tagSize - 1)

        # Verfies assignment funciton is of proper length.
        assert len(tagAssign.tags) == maxValue