# Sample with replacement

random.sample() samples without replacement. I find this piece of code by Sean Ross which samples with replacement.

```# credit author(s) of random.py
import random
import itertools

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)]
```

# A good blog for matplotlib configuration

Publication-ready the first time: Beautiful, reproducible plots with Matplotlib

http://blog.olgabotvinnik.com/blog/2013/08/21/2013-08-21-prettyplotlib-painlessly-create-beautiful-matplotlib/

And recently I found this http://nipunbatra.github.io/2014/08/latexify/

# Specify how many ticks one needs

```from matplotlib.ticker import LogLocator
ax.yaxis.set_major_locator(LogLocator(numticks=6))
```

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.

# Update figure in a for loop

```line,=pylab.plot(x,y)
for i in range(10):
line.set_data(x,y)
pylab.draw()
pylab.pause(1)
```

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.

# Parallelize big for loops in python

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)
f((i,j))
```

After hours of search I arrived at the solution using “multiprocessing” module, as the following:

```pool=Pool()
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.

# References for python

Learning Python
by Mark Lutz

Python Cookbook
by Alex Martelli, Anna Ravenscroft, David Ascher

Python Essential Reference
by David M. Beazley

# Numerical vector to array of strings

Convert a numeric vector (must be a column vector) to a cell array of strings

```cellstr(num2str((0:12)'));
```