From a73b2b35ac53ca37085f8cb67b01caacc8be1263 Mon Sep 17 00:00:00 2001 From: Felix Martin Date: Mon, 3 Jan 2022 12:46:37 -0500 Subject: [PATCH] Answer project 5 question 3 MIRA. --- p5_classification/mira.py | 43 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/p5_classification/mira.py b/p5_classification/mira.py index 5add6ef..e1d7813 100644 --- a/p5_classification/mira.py +++ b/p5_classification/mira.py @@ -60,8 +60,47 @@ class MiraClassifier: datum is a counter from features to values for those features representing a vector of values. """ - "*** YOUR CODE HERE ***" - util.raiseNotDefined() + + def scaleCounter(counter, scalar): + counter = counter.copy() + scalar = float(scalar) + for key in counter: + counter[key] *= scalar + return counter + + def updateWeights(weights, expectedLabel, guessedLabel, datum, c): + weightExpected = weights[expectedLabel] + weightGuessed = weights[guessedLabel] + tau = ((weightExpected - weightGuessed) * datum + 1.0) / ((datum * datum) * 2.0) + tau = min(c, tau) + weights[expectedLabel] = weights[expectedLabel] + scaleCounter(datum, tau) + weights[guessedLabel] = weights[guessedLabel] - scaleCounter(datum, tau) + + def evaluateWeights(weights): + correct = 0 + for datum, expectedLabel in zip(validationData, validationLabels): + guessedLabel = guessLabel(weights, datum) + if guessedLabel != expectedLabel: + correct += 1 + return correct / float(len(validationData)) + + def guessLabel(weights, datum): + vectors = util.Counter() + for l in self.legalLabels: + vectors[l] = weights[l] * datum + return vectors.argMax() + + allWeights = [] + for c in Cgrid: + weights = self.weights.copy() + for iteration in range(self.max_iterations): + for datum, expectedLabel in zip(trainingData, trainingLabels): + guessedLabel = guessLabel(weights, datum) + if guessedLabel != expectedLabel: + updateWeights(weights, expectedLabel, guessedLabel, datum, c) + accuracy = evaluateWeights(weights) + allWeights.append((accuracy, weights)) + self.weights = max(allWeights)[1] def classify(self, data ): """