NumPy Array Slicing (Python Tutorial)

NumPy Array Slicing (Python Tutorial)

This article explains the NumPy array Slicing. Learn to slice an array of any dimension into another array having items of a given range with examples.

NumPy Array Slicing

The content present in the NumPy arrays can be made accessible, and also we can make changes thorough indexing as we got to know in the previous module. Another way of data manipulation in arrays in NumPy is though slicing through the arrays. We can also try changing the position of the elements in the array with the help of their index number. Slicing is the extension of python’s basic concept of changing position in the arrays of N-d dimensions.

Slicing, in very simple words, means changing the position of elements from one given index to another given index.

We give the value of slice in this form [Start: End], and there is also another way in which we also define step attribute which could be written in this form [ Start:End:Step]

Some steps we need to keep in mind while giving these parameters:

  • If we do not give any value for start, then it is considered 0 by default.
  • If we do not give any value for the end, then it would consider the length of the array in the given dimension.
  • Also, if we do not give value for the step, then it is considered 1 by default.

Examples

Let us look through an example to understand it better:

#here we are importing the numpy and also making alias as np
import numpy as np 
#we created an array with some even numbers
array1=np.array([2, 4,6,8,10,12,14,16,18])
# now we would slice through the array by giving start and end
print(array1[2:5])

Output:

[6 8 10]

The output we get is slicing the elements with the index starting from 2  and going up to element with index 5, which is the end.

Let us take another example where we will not give the starting point for slicing:

#here we are importing the numpy and also making alias as np 
import numpy as np 
#we created an array with some even numbers 
array1=np.array([2, 4,6,8,10,12,14,16,18]) 
# now we would slice through the array by giving only the end 
print(array1[:5])

Output:

[ 2  4  6  8 10]

Here it is slicing elements from the beginning because it has taken start 0 by default until index 5.

Let us take another example where will not give value for the end:

#here we are importing the numpy and also making alias as np 
import numpy as np #we created an array with some even numbers 
array1=np.array([2, 4,6,8,10,12,14,16,18]) 
# now we would slice through the array by giving only the start
print(array1[2:])

Output:

[ 6  8 10 12 14 16 18]

Here it is slicing from the element with index 2 and goes up till the end because we specified no ending point. It considered the length of the array by default.

Negative Slicing

As we have gone through the concept of negative indexing in the very same manner, we have negative slicing. In this, we will be using the minus operator to refer to the index as a negative number.

let’s go through an example for negative slicing:

#here we are importing the numpy and also making alias as np 
import numpy as np 
#we created an array with some even numbers 
array1=np.array([2, 4,6,8,10,12,14,16,18]) 
# now we would slice through the array by giving negative indexes 
print(array1[-5:-2])

Output:

[10 12 14]

As we gave our index in negative, so it starts slicing from the index with -5 from behind and goes up till the index -2. We can also provide step value if need be.

Using STEP

In this example, we will be specifying the Step value to slice through the array:

#here we are importing the numpy and also making alias as np 
import numpy as np #we created an array with some even numbers 
array1=np.array([2, 4,6,8,10,12,14,16,18]) 
# now we would slice through the array by giving start, end and also step
print(array1[2:5:2])

Output:

[ 6 10]

Here it is slicing up the element of the array starting from the element with index 2 and going up till the element with index 5. Also, it is using the step of two, so we see it skips index and prints the next index element.

To return every element of the array, we can use only step value. Let us go through examples:

#here we are importing the numpy and also making alias as np 
import numpy as np 
#we created an array with some even numbers 
array1=np.array([2, 4,6,8,10,12,14,16,18]) 
# now we would slice through the array by only step value 
print(array1[::2])

Output:

[ 2  6 10 14 18]

Here we are specifying nor the starting index neither ending index of the array. We gave only the step value so it would provide us with all the elements by skipping the step values.

Slicing through 2-D Arrays

As we have studied about the two-dimensional array so to slice through these arrays will also specify the array we want to slice the elements from.

Let us go through an example of this:

#here we are importing the numpy and also making alias as np 
import numpy as np 
#we created a 2-D array with some even numbers 
array1=np.array([[2, 4,6,8,10],[12,14,16,18,20]]) 
# now we would slice through the array by giving all values
print(array1[0,1:4])

Output:

[4 6 8]

Here we are trying to slice through the 2-d array. So we first select one of the two arrays we want to slice through. Then we give details about the start and end value for the slicing of the array.

Now if we want to have elements from both arrays for a particular index, we would give start and end along with mentioning no of arrays.

Let us go through the example:

#here we are importing the numpy and also making alias as np 
import numpy as np 
#we created a 2-D array with some even numbers 
array1=np.array([[2, 4,6,8,10],[12,14,16,18,20]]) 
# now we would slice through the both array by giving all
print(array1[0:3,2])

Output:

[ 6 16]

Here it is returning us elements from both the arrays starting from index 0 going up to index 3.

If we want to slice from both the arrays of this 2-array.

Let us go through an example of this scenario:

#here we are importing the numpy and also making alias as np 
import numpy as np 
#we created a 2-D array with some even numbers 
array1=np.array([[2, 4,6,8,10],[12,14,16,18,20]]) 
# now we would slice through the both array by giving all 
print(array1[0:3,1:4])

Output:

[[ 4  6  8]
 [14 16 18]]

So if we try to get elements from both the arrays, then it would return a 2-D array with all the sliced elements.

Slicing through 3-D arrays

Here we would select through the array and then slice through them.

Let us go through an example:

#here we are importing the numpy and also making alias as np 
import numpy as np 
#we created a 2-D array with some even numbers 
array1=np.array([[[2, 4,6,8,10],[12,14,16,18,20]],[[2, 4,6,8,10],[12,14,16,18,20]]]) 
# now we would slice through the both array by giving all 
print(array1[0:3,1:4])

Output:

[[[12 14 16 18 20]]
 [[12 14 16 18 20]]]

Here we are slicing through a 3-D array, and we are selecting through the array and then slicing through the array and getting the desired result.

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *