예제 #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 test(classifier: BayesClassifier,
         measures: Tuple[int],
         tags: Tuple[int],
         V=0) -> float:
    ## Training protocol for testing measurements. Shuffles measures and tags while retaining
    # pairings and then classifies according to Bayes classifier. Returns expected gain
    # of testing. If V is provided, performs V-fold testing on data of V partitions

    # Shuffles tags and measures
    K = classifier.range
    e = classifier.eGain
    measures, tags = shuffle(measures, tags)

    # Sees if we are v-folding or just testing over a random measure.
    if V:
        normMatrix = vFold(measures, tags, V, classifier)
    else:
        results = classifier.assign(measures)
        matrix = genConMatrix(tags, results, K)
        normMatrix = normConMatrix(matrix)
    return calcExpGain(normMatrix, e)
예제 #3
0
def biasCCP(classifier: BayesClassifier, delta: float) -> List[List[float]]:
    # Destrucctively Updates CCP values in classifier. Classes are generated from classifier
    # for all measurements d in M space. Each d|c is increased by delta towards classifier's selection
    # and is normalized over class space. Returns conditional matrix. Not used in function but preserved
    # for legacy update.

    # Classifies for all d in measurement space M
    M = classifier.spaceSize
    K = classifier.range
    bayesValues = classifier.assign(range(M))
    conds = classifier.cond  # Note, this alters the original classifier. Had to be done for speed and memory efficiency.

    # Updates conditionals
    for measure in range(M):
        tag = bayesValues[measure]
        conds[tag][measure] += delta

    # Normalizes conditionals
    for val in range(K):
        sums = sum(conds[val])
        conds[val] = [prb / sums for prb in conds[val]]

    return conds
예제 #4
0
def vFold(meas: Tuple[int], tags: Tuple[int], V: int,
          classifier: BayesClassifier) -> List[List[float]]:
    # Performs a round of V-fold validation tests on measurements 'meas' and respective real classes 'tags'
    # Performs V test using classifier. Returns a normalized confusion matrix of tests.
    results = []

    measFold = partition(meas, V)
    tagsFold = partition(tags, V)

    for v in range(V):
        # Creates folds
        # Assigns testing and training
        trainTags = [tag for i in range(V) if i != v for tag in tagsFold[i]]
        testMeas = measFold[v]

        # Updates with new probability values
        trainProb = calcClassProb(trainTags, classifier.range)
        classifier.priorUpdate(trainProb)

        results.append(classifier.assign(testMeas))  # Unfolds tuple
    results = tuple(i for tpl in results for i in tpl)
    matrix = genConMatrix(tags, results, classifier.range)
    return results  #normConMatrix(matrix)