random.sample() samples without replacement. I find this piece of code by Sean Ross which samples with replacement.
# credit author(s) of random.py
def sample_wr(population, k):
"Chooses k random elements (with replacement) from a population"
n = len(population)
_random, _int = random.random, int # speed hack
return [population[_int(_random() * n)] for i in itertools.repeat(None, k)]
from matplotlib.ticker import LogLocator
This is for a loglog plot. There is another function for regular plot called MaxNLocator.
Why not using set_ticks? The situation is that I do not know where the ticks are and python does a good job in determining the location of ticks.
for i in range(10):
The trick is to use pylab.pause() instead of time.sleep. So the figure will update in each iteration instead of showing all plots when the loop finishes. This works for me, but no everyone, as far as I can tell from googling.
Suppose I have a big for loop to run (big in the sense of many iterations):
for i in range(10000)
for j in range(10000)
After hours of search I arrived at the solution using “multiprocessing” module, as the following:
x=pool.imap(f,((i,j) for i in xrange(10000) for j in xrange(10000)]))
Remark: pool.map would generate a list of arguments first and then feed the list to the function. Hence if I have a big for loop, it spends a lot of time generating the list of arguments using only 1 cpu. In contrast, imap would generate the arguments on the fly, therefore parallelizing the for loop as I wish.
by Mark Lutz
by Alex Martelli, Anna Ravenscroft, David Ascher
Python Essential Reference
by David M. Beazley
Convert a numeric vector (must be a column vector) to a cell array of strings