Answer project 5 questions 1 and 2.
This commit is contained in:
@@ -13,4 +13,4 @@
|
|||||||
|
|
||||||
|
|
||||||
def q2():
|
def q2():
|
||||||
"*** YOUR CODE HERE ***"
|
return 'a'
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user