Answer project 5 questions 1 and 2.

This commit is contained in:
2022-01-03 10:50:33 -05:00
parent d50686d124
commit d6e89e8c00
2 changed files with 13 additions and 8 deletions

View File

@@ -13,4 +13,4 @@
def q2(): def q2():
"*** YOUR CODE HERE ***" return 'a'

View File

@@ -54,8 +54,14 @@ class PerceptronClassifier:
for iteration in range(self.max_iterations): for iteration in range(self.max_iterations):
print "Starting iteration ", iteration, "..." print "Starting iteration ", iteration, "..."
for i in range(len(trainingData)): for i in range(len(trainingData)):
"*** YOUR CODE HERE ***" datum, expectedLabel = trainingData[i], trainingLabels[i]
util.raiseNotDefined() vectors = util.Counter()
for l in self.legalLabels:
vectors[l] = self.weights[l] * datum
highestScoreLabel = vectors.argMax()
if highestScoreLabel != expectedLabel:
self.weights[expectedLabel] = self.weights[expectedLabel] + datum
self.weights[highestScoreLabel] = self.weights[highestScoreLabel] - datum
def classify(self, data ): def classify(self, data ):
""" """
@@ -77,9 +83,8 @@ class PerceptronClassifier:
""" """
Returns a list of the 100 features with the greatest weight for some label Returns a list of the 100 features with the greatest weight for some label
""" """
featuresWeights = [] featureWeightTuples = self.weights[label].items()
weightFeatureTuples = [(w, f) for f, w in featureWeightTuples]
"*** YOUR CODE HERE ***" weightFeatureTuples.sort(reverse=True)
util.raiseNotDefined() featuresWeights = [feature for _, feature in weightFeatureTuples[:100]]
return featuresWeights return featuresWeights