Slicing of Numpy Arrays | Data Science tutorial| codin india

 Hello Friends, in this tutorial we will learn about what is indexing and slicing. Let us see one by one.

What is indexing?

Indexing is a term of defining something orderly. It is used when we want something in a specific range. Suppose you have multiple copies of the same item and you have to remember or recognize every item then you will give every item a unique name or number. That exactly what indexing is. In Numpy Arrays indexing start from 0 like List in Python.

EX:-

myarr = np.array([101,102,103,104,105])

In the given example, the indexing start from zero. If I call the 3rd item from myarr array it will give me 104 from the array.

What is slicing of array?

Slicing of arrays indicates that calling the specific value or range of value from Array. Slicing of Array is almost the same as Slicing of string. We can fetch a specific value from the array, range of values from arrays, call values in reverse order and call value by jumping one or more element. You can slice accoring to your choice.

EX:-

myarr = np.array([101,102,103,104,105])

print(myarr[3])

From the given example, from myarr array I am fetching 3rd value and it will give 104.

CODE:-

Slicing of numpy arrays


Slice in Specific Range?

If you want to slice multiple items from the array just pass array_name[first_value, last_value].
It will give you the list of values from thatparticular items.
NOTE:- But point to be note here is that numpy array just ignore the last value and give output as n-1 where n is last value.

Example with Code:-

How to slice array by leaving values?

Yes, you heared it right. You can fetch elements of array by leaving one or more values. 

In my example I am leaving 2 values.

You can use it by just passing 3rd argument in slicing

Example:- myarr[1:8:2]

Note:- But point to be note here is that while jumping of Arrays it will accept last values also.

Example with Code:-

slice array

Negative Slicing:-

Yes, we can Slice array in negative order also. For this you have to pass the last value in first argument, starting value at 2nd argument and -1 at 3rd argument for getting arrays in reverse order.
NOTE:- In order to reversing of arrays point to be remember is that it will ignore last value.
Example with Code:-
negative slicing

Negative Slicing by leaving values:-

Yes, we can also do negative slicing by leaving values.

Instead of pass -1 in 3rd argument just pass -any_value

Example with code:-


Multi-Dimensional Array Slicing:-

We can also Slice Multi-Dimensional Arrays. But in This Case 0 means 1st row, 1 means 2nd row and so on.

Example with Code:-

multi-dimensional array slicing

In the above Code, I called arr[1] that means 2nd array. That is why it will give 6,7,8,9,10 from multi-dimensional array.

Slice multiple array from multi-dimensional array:-

Yes, we can call multiple arrays from multi-Dimensional array. In the following Example:- I will call all array after 1 array from multi-Dimensional array.

Example with Code:-


I hope you liked this tutorial, You can also watch our video tutorial from youtube

Thanks for watching :) Stay tuned

Comments