Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy array slicing using colons

I am trying to learn numpy array slicing.

But this is a syntax i cannot seem to understand.

What does

a[:1] do.

I ran it in python.

a = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16])
a = a.reshape(2,2,2,2)
a[:1]

Output:

array([[[ 5,  6],
        [ 7,  8]],

       [[13, 14],
        [15, 16]]])

Can someone explain to me the slicing and how it works. The documentation doesn't seem to answer this question.

Another question would be would there be a way to generate the a array using something like

np.array(1:16) or something like in python where

x = [x for x in range(16)]
like image 798
aceminer Avatar asked Feb 04 '16 15:02

aceminer


3 Answers

The commas in slicing are to separate the various dimensions you may have. In your first example you are reshaping the data to have 4 dimensions each of length 2. This may be a little difficult to visualize so if you start with a 2D structure it might make more sense:

>>> a = np.arange(16).reshape((4, 4))
>>> a
    array([[ 0,  1,  2,  3],
           [ 4,  5,  6,  7],
           [ 8,  9, 10, 11],
           [12, 13, 14, 15]])
>>> a[0]  # access the first "row" of data
    array([0, 1, 2, 3])
>>> a[0, 2]  # access the 3rd column (index 2) in the first row of the data
    2

If you want to access multiple values using slicing you can use the colon to express a range:

>>> a[:, 1]  # get the entire 2nd (index 1) column
    array([[1, 5, 9, 13]])
>>> a[1:3, -1]  # get the second and third elements from the last column
    array([ 7, 11])
>>> a[1:3, 1:3]  # get the data in the second and third rows and columns
    array([[ 5,  6],
           [ 9, 10]])

You can do steps too:

>>> a[::2, ::2]  # get every other element (column-wise and row-wise)
    array([[ 0,  2],
          [ 8, 10]])

Hope that helps. Once that makes more sense you can look in to stuff like adding dimensions by using None or np.newaxis or using the ... ellipsis:

>>> a[:, None].shape
    (4, 1, 4)

You can find more here: http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html

like image 124
djhoese Avatar answered Nov 10 '22 20:11

djhoese


It might pay to explore the shape and individual entries as we go along.

Let's start with

>>> a = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16])
>>> a.shape
(16, )

This is a one-dimensional array of length 16.

Now let's try

>>> a = a.reshape(2,2,2,2)
>>> a.shape
(2, 2, 2, 2)

It's a multi-dimensional array with 4 dimensions.

Let's see the 0, 1 element:

>>> a[0, 1]
array([[5, 6],
   [7, 8]])

Since there are two dimensions left, it's a matrix of two dimensions.


Now a[:, 1] says: take a[i, 1 for all possible values of i:

>>> a[:, 1]
array([[[ 5,  6],
    [ 7,  8]],

   [[13, 14],
    [15, 16]]])

It gives you an array where the first item is a[0, 1], and the second item is a[1, 1].

like image 24
Ami Tavory Avatar answered Nov 10 '22 21:11

Ami Tavory


To answer the second part of your question (generating arrays of sequential values) you can use np.arange(start, stop, step) or np.linspace(start, stop, num_elements). Both of these return a numpy array with the corresponding range of values.

like image 26
jonchar Avatar answered Nov 10 '22 19:11

jonchar