Creating numpy array with different steps
I would like to create an array like the following:
# 0 | 4 | 8 | 16 | 32
In which each element, except the first, is a double previous one. I can create this less through iteration, etc.
However, since Python provides many one-line functions, I was wondering if I have a way to do this.
+3
source to share
3 answers
You can use numpy.logspace
to get ranges at log spacing. Use a keyword argument base=N
to set the exponent base:
In [27]: np.logspace(0, 10, 11, base=2).astype(int)
Out[27]: array([ 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024])
I like this method because the name of the function "logspace" makes it clear that I am going to use a range with a log interval (as opposed to linear).
+1
source to share