1

I think I understand the basics of numpy array views (it doesn't copy data and we're basically referring to the same data buffer as the array but with different offsets, etc.). I am however confused about the syntax.

What I do understand is that if I were to run this:

import numpy as np
a = np.random.random_sample((100,100))
b = a[:10, :10]

Then b would be the first 10 rows and the first 10 columns of a.

What I don't understand is what a[::10] does. Could someone please explain that?

Also - are there any other similar things I should know about when dealing with numpy arrays?

I've looked at array views on http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.view.html but it doesn't really help me.

1 Answer 1

2

The ::10 syntax means sample the entire range returning every ten elements. SO if you wanted to return 1/100th of the data (ie every 10th row, every 10th column), you'd do:

b = a[::10, ::10]

a[::10] is just shorthand for a[::10, ::] if I recall.

Sign up to request clarification or add additional context in comments.

5 Comments

isn't that 1/100th of the data?
okay... so what does a[:2:10, :2:10] do? it returns something when I try it out on IDLE?
What is the shape of the resulting array? I don't have access to python right now to test it. And is it any different than a[2:10, 2:10]
I figured it out. Turns out the syntax is array[start:end:steps, start:end:steps,..] for as many dimensions as I have. So basically in the example I gave with the shape being (100,100), a[::10, ::10] == a[:100:10, :100:10]
Interesting. Admittedly, I usually get tripped up with indexing in numpy and only choose to remember a subset of use cases. I've never run into any trouble with using the :: syntax exclusively.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.