Array indexing

The indexing of arrays is done using an array as an index. With an index array, a copy of the original array is returned. numpy arrays can be indexed using any other sequence or by using any other array, excluding tuples. The last element in the array can be indexed by -1 and the second last element can be indexed by -2, and so on.

So, to perform indexing operations on the  array, first we create a new numpy array and for that we are going to use the range() function to create the array, as shown here:

student@ubuntu:~$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> arr = np.arange(0,16)
>>> arr
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
>>>

In the preceding example, we created the array arr with the range 16; that is, 0-15.

Now, we are going to perform a different indexing operation on array arr. First, let's get the value in the array at a particular index:

>>> arr[7]
7
>>>

In the preceding example, we accessed the array by its index value and after passing the index number to the array arr, the array returned the value 7, which is the particular indexed number that we pass.

After getting the value at a particular index, we are going to get values in a range. Let's look at the following example:

>>> arr[2:10]
array([2, 3, 4, 5, 6, 7, 8, 9])
>>> arr[2:10:2]
array([2, 4, 6, 8])
>>>

In the preceding example, first we accessed the array and got values in a range of (2-10). As a result, it shows the output as array([2, 3, 4, 5, 6, 7, 8, 9]). In the second term, arr[2:10:2], it actually states that access array in the range of 2-10 in the interval of two step. The syntax of this kind of indexing is arr[_start_value_:_stop_value_:_steps_]. So, as the output of second term, we get array([2, 4, 6, 8]).

We can also get values in the array from the index until the end, as show in the following example:

>>> arr[5:]
array([ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
>>>

As we seen in the preceding example, we accessed the values in the array from the 5th index value until the end. As a result, we got the output as array([ 5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15]).

Now we are going to look at slicing of the numpy array. In slicing, we actually take some part of our original array and store it in a specified array name. Let's look at an example:

>>> arr_slice = arr[0:8]
>>> arr_slice
array([0, 1, 2, 3, 4, 5, 6, 7])
>>>

In the preceding example, we take slice of the original array. As a result, we got a slice of the array with values 0,1,2,…..,7. We can also give updated values to the slice of the array. Let's look at an an example:

>>> arr_slice[:] = 29
>>> arr_slice
array([29, 29, 29, 29, 29, 29, 29, 29])
>>>

In the the preceding example, we set all values in the array slice to 29,. But the important thing while assigning values to the array slice is that the value assigned to the slice will also get assigned to the original set of the array.

Let's see the result after giving values to the slice of the array and the effect on our original array:

>>> arr
array([29, 29, 29, 29, 29, 29, 29, 29, 8, 9, 10, 11, 12, 13, 14, 15])
>>>

Now, we are going to look at another operation; that is, copying the array. The difference between slicing and copying of arrays is that when we do the slicing of the array, the changes made are going to be applied on the original array. When we get a copy of the array, it gives an explicit copy of the original array. Therefore, the changes applied onto the copy of the array do not affect the original array. So let's look at an example of copying an array:

>>> cpying_arr = arr.copy()
>>> cpying_arr
array([29, 29, 29, 29, 29, 29, 29, 29, 8, 9, 10, 11, 12, 13, 14, 15])
>>>

In the preceding example, we just take a copy of the original array. For that, we use the array_name.copy() function and the output is the copy the original array.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset