Skip to content

Commit

Permalink
Merged the correction to knn.
Browse files Browse the repository at this point in the history
--HG--
branch : dev
rename : examples/ga_knn.py => examples/ga/ga_knn.py
rename : examples/knn.py => examples/ga/knn.py
  • Loading branch information
fmder committed Jan 14, 2013
2 parents 2e33d4f + aad565b commit 8970008
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
6 changes: 3 additions & 3 deletions examples/ga/ga_knn.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

def evalClassifier(individual):
labels = classifier.predict(trainset[N_TRAIN:], individual)
return sum(1 for x, y in zip(labels, trainlabels) if x == y) / float(len(trainlabels)), \
return sum(x == y for x, y in zip(labels, trainlabels[N_TRAIN:])) / float(len(trainlabels[N_TRAIN:])), \
sum(individual) / float(classifier.ndim)

creator.create("FitnessMulti", base.Fitness, weights=(1.0, -1.0))
Expand All @@ -68,15 +68,15 @@ def main():
# random.seed(64)
MU, LAMBDA = 100, 200
pop = toolbox.population(n=MU)
hof = tools.HallOfFame(1)
hof = tools.ParetoFront()
stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("avg", tools.mean)
stats.register("std", tools.std)
stats.register("min", min)
stats.register("max", max)

algorithms.eaMuPlusLambda(pop, toolbox, mu=MU, lambda_=LAMBDA,
cxpb=0.5, mutpb=0.2, ngen=40,
cxpb=0.7, mutpb=0.3, ngen=40,
stats=stats, halloffame=hof)

return pop, stats, hof
Expand Down
2 changes: 1 addition & 1 deletion examples/ga/knn.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def predict(self, data, features=None):
def classification_rate(features):
"""Returns the classification rate of the default KNN."""
labels = _knn.predict(trainset[N_TRAIN:], features)
return sum(1 for x, y in zip(labels, trainlabels) if x == y)/float(len(trainlabels))
return sum(x == y for x, y in zip(labels, trainlabels[N_TRAIN:]))/float(len(trainlabels[N_TRAIN:]))

if __name__ == "__main__":
trainset = [[1, 0], [1, 1], [1, 2]]
Expand Down

0 comments on commit 8970008

Please sign in to comment.