FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Update weighted_sample_with_replacement() in utils.py by sofmonk · Pull Request #366 · aimacode/aima-python · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (4) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
2 changes: 1 addition & 1 deletion learning.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ def weighted_replicate(seq, weights, n):
wholes = [int(w * n) for w in weights]
fractions = [(w * n) % 1 for w in weights]
return (flatten([x] * nx for x, nx in zip(seq, wholes)) +
weighted_sample_with_replacement(seq, fractions, n - sum(wholes)))
weighted_sample_with_replacement(n - sum(wholes),seq, fractions, ))


def flatten(seqs): return sum(seqs, [])
Expand Down
2 changes: 1 addition & 1 deletion probability.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -651,5 +651,5 @@ def particle_filtering(e, N, HMM):
w[i] = float("{0:.4f}".format(w[i]))

# STEP 2
s = weighted_sample_with_replacement(s, w, N)
s = weighted_sample_with_replacement(N,s,w)
return s
2 changes: 1 addition & 1 deletion search.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ def genetic_algorithm(population, fitness_fn, ngen=1000, pmut=0.1):
new_population = []
for i in range(len(population)):
fitnesses = map(fitness_fn, population)
p1, p2 = weighted_sample_with_replacement(population, fitnesses, 2)
p1, p2 = weighted_sample_with_replacement(2,population, fitnesses)
child = p1.mate(p2)
if random.uniform(0, 1) < pmut:
child.mutate()
Expand Down
2 changes: 1 addition & 1 deletion utils.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def probability(p):
return p > random.uniform(0.0, 1.0)


def weighted_sample_with_replacement(seq, weights, n):
def weighted_sample_with_replacement(n,seq, weights):
"""Pick n samples from seq at random, with replacement, with the
probability of each element in proportion to its corresponding
weight."""
Expand Down

Back | FazBrowse Home | New Git URL