Sanuuu
Sanuuu

Reputation: 243

Is there any effective difference between those two functions?

I was trying to understand what is the effective difference between those two pieces of code. They are both written for an assignment I got at school, but only the first one works as it should. I've been unable to understand what goes wrong in the second one so I'd be fantastically grateful if someone could shine some light on this problem.

First code:

def classify(self, obj):
    if sum([c[0].classify(obj)*c[1] for c in self.classifiers]) >0:
        return 1
    else: return -1

def update_weights(self, best_error, best_classifier):
    w=self.data_weights
    for index in range(len(self.data_weights)):
        if self.standard.classify(self.data[index])==best_classifier.classify(self.data[index]):
            s=-1
        else: s=1
        self.data_weights[index] = self.data_weights[index]*math.exp(s*error_to_alpha(best_error))

Second code:

def classify(self, obj):
    score = 0
    for c, alpha in self.classifiers:
        score += alpha * c.classify(obj)
    if score > 0:
        return 1
    else:
        return -1

def update_weights(self, best_error, best_classifier):
    alpha = error_to_alpha(best_error)
    for d, w in zip(self.data, self.data_weights):
        if self.standard.classify(d) == best_classifier.classify(d):
            w *= w * math.exp(alpha)
        else:
            w *= w * math.exp(-1.0*alpha)

Upvotes: 0

Views: 82

Answers (1)

andrew cooke
andrew cooke

Reputation: 46862

the second doesn't modify the weights.

in the first you explicitly modify the weights array with the line

self.data_weights[index] = ...

but in the second you are only modifying w:

w *= ...

(and you have an extra factor of w). in the second case, w is a variable that is initialised from data_weights, but it is a new variable. it is not the same thing as the array entry, and changing its value does not change the array itself.

so when you later go to look at data_weights in the second case, it will not have been updated.

Upvotes: 2

Related Questions