# 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

There may be one line, but this is more explicit:

``````x = np.multiply.accumulate( np.ones( 10 )*2)
x = 0
```

```

OR

``````x  = 2**np.arange(1,10)
x = 0
```

```
+6

source

``````import numpy as np
```

```

In this case, you can use a list comprehension to evaluate your strength function ( `2^n`

), then generate `numpy.array`

.

``````>>> np.array( + [2**i for i in range(2, 10)])
array([  0,   4,   8,  16,  32,  64, 128, 256, 512])
```

```
+1

source

You can use `numpy.logspace`

to get ranges at log spacing. Use a keyword argument `base=N`

to set the exponent base:

``````In : np.logspace(0, 10, 11, base=2).astype(int)
Out: 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

All Articles