StackOverflow

### Answer rating: 189

I am a beginner with numpy, and I am trying to extract some data from a long numpy array. What I need to do is start from a defined position in my array, and then subsample every nth data point from that position, until the end of my array.

basically if I had

```
a = [1,2,3,4,1,2,3,4,1,2,3,4....]
```

I want to subsample this to start at `a[1]`

and then sample every fourth point from there, to produce something like

```
b = [2,2,2.....]
```

You can use numpy"s slicing, simply `start:stop:step`

.

```
>>> xs
array([1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4])
>>> xs[1::4]
array([2, 2, 2])
```

This creates a *view* of the the original data, so it"s *constant time*. It"ll also reflect changes to the original array and keep the whole original array in memory:

```
>>> a
array([1, 2, 3, 4, 5])
>>> b = a[::2] # O(1), constant time
>>> b[:] = 0 # modifying the view changes original array
>>> a # original array is modified
array([0, 2, 0, 4, 0])
```

so if either of the above things are a problem, you can make a copy explicitly:

```
>>> a
array([1, 2, 3, 4, 5])
>>> b = a[::2].copy() # explicit copy, O(n)
>>> b[:] = 0 # modifying the copy
>>> a # original is intact
array([1, 2, 3, 4, 5])
```

This isn"t constant time, but the result isn"t tied to the original array. The copy also contiguous in memory, which can make some operations on it faster.

Computer Age Statistical Inference: Algorithms, Evidence, and Data Science (Institute of Mathematical Statistics Monographs, Series Number 6). The twenty-first century has seen a breathtaking expan...

28/08/2021

Managing and analyzing data have always offered the greatest benefits and the greatest challenges for organizations of all sizes and across all industries. Businesses have long struggled with finding ...

10/07/2020

Since you are interested in learning more about designing data visualizations (by virtue of the fact that you’re reading this book), then chances are good that you have been the reader of other peop...

10/07/2020

If you’ve picked up this book, you’re probably aware of the extraordinary progress that deep learning has represented for the field of artificial intelligence in the recent past. In a mere five ye...

23/09/2020

X
# Submit new EBook