Thursday 20 July 2017

Data Science & Machine Learning - 3.4 NumPy Array Indexing

Hi friends,

In the previous post under Data Science & Machine Learning, we discussed various ways to generate NumPy Arrays of random numbers. In this post, we will learn about Array Indexing of NumPy Arrays. This is quite similar to accessing general lists or strings in Python. 

Note: All the commands discussed below are run in the Jupyter Notebook environment. See this post on Jupyter Notebook to know about it in detail.


NumPy Array Indexing

So, let's begin to see how we can do indexing of NumPy arrays.

First, let's declare a NumPy array of integers from 1 to 9 using the NumPy's arange() function.


As I mentioned earlier, array indexing of NumPy arrays is just like indexing lists in Python. Just like Python lists, the index starts from zero and goes up to 1 less than the size of the array here too. So, to access the fifth element of the above NumPy array, we execute the following command:


We can see that starting the index from zero (0), the fifth element is at index 4 and the element is 4.

We can also use the slice notation with NumPy Arrays just like we do with Python lists and strings. To access the first four elements of the NumPy Array, execute the following command:



We can also access elements which are only at the middle, say, from elements 3rd to 5th. To do so, use the following command:


If we don't specify the starting index, then it starts with the first element by default.



Note: The end index is exclusive i.e. it does not include the 7th element in the sliced result.

Similarly, if we don't specify the end element, then it goes up to the last element from the starting index.



The advantage to NumPy Arrays over Python lists is that it allows us to update multiple elements of the NumPy array all at once. For example, if we want to update the first three elements of the above NumPy Array to say 100, we can do so using the following command:


However, doing the same with Python lists generates the following error:


This is just a small example which shows the advantage of using NumPy Arrays over Python lists.

Let's now see Indexing of NumPy Matrices (2-D arrays). So, let's first declare a 2-D NumPy array.


This is also similar to the indexing of Python 2-D lists. For example, to access the element present in the first row and first column, run the following command:


We can also just index an entire row of the matrix. For example, to get the second row of the matrix, run the following command:


To access the element 5, which is present in the second row and second column, we run the following command:


Note that we mentioned mat[1][1] and not mat[2][2] since the index starts with 0.

You can use the slice notation for the matrices as well to grab a bunch of elements from the matrix. For example, to access both the second and third columns completely, we can do something like below:


Note that we have put 1:3 and not 1:2 for the rows since we know that the end index is exclusive. So, putting 1:2 will only give the second row or the row at index 1.


Slice notation may appear tricky at first use so I recommend to think of different elements of the above matrix and try to write slice notation to access them. With practice, you'll start to find it easier soon enough.

Now, we will learn to access the elements of the array based on condition which is also called Conditional Selection i.e. select only those elements of the array for which the condition(s) is met.

Let's see an example. We'll use the same 1-D array we declared earlier for understanding conditional selection:


Now, suppose we want to select only those elements of the array which are less than 6. So, how do we do it? That's where Conditional Selection comes into the picture. 

Executing the following statement returns a boolean array equal to the size of original array with values as True/False based the condition:


We see from the above image that the first six elements are True since the value of those elements is less than 6. 

Now, we can use the result of this boolean array to filter elements of the array satisfying the condition that the array element is less than 6.


Similarly, we can also obtain the elements of the array which are even using the following commands:


We can also add a scalar to the elements satisfying the condition. For example, if want to add 5 to all the even integers of the above array, we run the following instructions:


Notice that the even values have got 5 added to them now.

We now end this post here on NumPy Array Indexing. In the next post, we will discuss a list of important methods supported by NumPy Arrays.
Share:

0 comments:

Post a Comment

Contact Me

Name

Email *

Message *

Popular Posts

Blog Archive