Indexing and Slicing in NumPy

0
487

Introduction To Indexing and Slicing in NumPy

What is Indexing and Slicing in NumPy? Accessing the values of an array we generally used the index of an array. The process to access the value with the help of using an index is called slicing. The index of the array always starts with zero.

Suppose Of Index of 1 D array is

Value -> [ 5, 6, 7, 8 ]

Index -> [ 0, 1, 2, 3 ]

Access one-dimensional array value

Syntex to access 1D array value using index:-

Variable_name[ Start index : Ending index ]

Note:- Suppose you want to access value till 2 index that time pass 3 at the place of ending index

Example:- Create 1- D array and access value using there index

Code:- 

import numpy as np

Arr = np.array([ 5, 6, 7, 8 ])     # index is [0, 1, 2, 3]
print('Values Of Array :- ', Arr )

print("Access value from 0 index to 1 index :- ",Arr[0:2])

Output:- 
Values Of Array :-  [5 6 7 8]
Access value from 0 index to 1 index :-  [5 6]

Example:- Access value till the last index in array

import numpy as np

Arr = np.array([ 5, 6, 7, 8 ])     # index is [0, 1, 2, 3]
print('Values Of Array :- ', Arr )

print("Access value from 1 index to last index :- ",Arr[1:])       # Arr[Starting index : ]
# Here we do not mention ending index because to access value till last

Output:-
Values Of Array :-  [5 6 7 8]
Access value from 1 index to last index:-  [6 7 8]

Widget not in any sidebars

Access Two-dimensional array value 

Inndex of 2D array in row and columns also 

Example :- 

index    0  1  2 

    0      [ 1 2 3 ]

    1      [ 4 5 6 ]

    2      [ 7 8 9 ]

Note:- 0 1 2 is the index of rows and columns

Syntex to access 2D array value using index:-

Variable_name[ Rows index , Columns Index ]

Variable_name[ Start index row : Ending index row , Start index col : Ending index col ]

Example1:- Create 2 D array and access the first two rows of array using index

Code:-

import numpy as np

Arr = np.array([[1,2,3],[4,5,6],[7,8,9]])     # index is [0, 1, 2, 3]
print('Values Of Array :- \n', Arr )
print(Arr.ndim)
print("Access value of the first two rows :- \n",Arr[0:2])

Output:- 
Values Of Array :- 
 [[1 2 3]
 [4 5 6]
 [7 8 9]]
Access value of the first two rows :- 
 [[1 2 3]
 [4 5 6]]

Example 2:-
import numpy as np

Arr = np.array([[1,2,3],[4,5,6],[7,8,9]])     # index is [0, 1, 2, 3]
print('Values Of Array :- \n', Arr )

print("Access value of the first two Columns :- \n",Arr[:,0:2])


Output:-
Values Of Array :- 
 [[1 2 3]
 [4 5 6]
 [7 8 9]]
Access value of the first two Columns :- 
 [[1 2]
 [4 5]
 [7 8]]

Widget not in any sidebars

Example3:- Create array using a range function and access value

arr = np.arange(1,26).reshape(5,5)

print('Values Of Array :- \n', arr )

print("Access value of 1st row and 2nd columns index :- ",arr[0,2])
# arr[row index : columns index ]

Output:- 

Values Of Array :- 
 [[ 1  2  3  4  5]
 [ 6  7  8  9 10]
 [11 12 13 14 15]
 [16 17 18 19 20]
 [21 22 23 24 25]]
Access value of 1st row and 2nd columns index :-  3

Example4:- Access value of concatenation of the first two rows and first two columns

Code:-
arr = np.arange(1,26).reshape(5,5)


print('Values Of Array :- \n', arr )

print("Access value of concatenation of the first two rows and first two columns :- \n",arr[0:2,0:2])
# arr[row(Start : Stop) , columns(Start : Stop)]
Output:-
Values Of Array :- 
 [[ 1  2  3  4  5]
 [ 6  7  8  9 10]
 [11 12 13 14 15]
 [16 17 18 19 20]
 [21 22 23 24 25]]
Access value of concatenation of the first two rows and first two columns :- 
 [[1 2]
 [6 7]]

Example 4:-

arr = np.arange(1,26).reshape(5,5)


print('Values Of Array :- \n', arr )

print("Access value last two rows :- \n",arr[3:5])
# arr[row(Start : Stop)]

Output:-
Values Of Array :- 
 [[ 1  2  3  4  5]
 [ 6  7  8  9 10]
 [11 12 13 14 15]
 [16 17 18 19 20]
 [21 22 23 24 25]]
Access value last two rows :- 
 [[16 17 18 19 20]
 [21 22 23 24 25]]

Example 5:- This selects rows 1: (1 to the end of the bottom of the array) and columns 2:4 (columns 2 and 3), as shown here:

arr4 = np.arange(1,26).reshape(5,5)
print(arr4)
print('\n Access value \n',arr4[1:,2:4])

Output:-
[[ 1  2  3  4  5]
 [ 6  7  8  9 10]
 [11 12 13 14 15]
 [16 17 18 19 20]
 [21 22 23 24 25]]

 Access value 
 [[ 8  9]
 [13 14]
 [18 19]
 [23 24]]

Advance Slicing 

Suppose you want to access the value of index using per step that time in syntex some modification

Syntex:- [ Start : Stop : Step ]

Example1:-

arr = np.arange(1,26).reshape(5,5)

print('Values Of Array :- \n', arr )

print("Access value of rows by step 2 :- \n",arr[0:5:2])
# arr[row(Start : Stop : Step)]


Output:-
Values Of Array :- 
 [[ 1  2  3  4  5]
 [ 6  7  8  9 10]
 [11 12 13 14 15]
 [16 17 18 19 20]
 [21 22 23 24 25]]
Access value of rows by step 2 :- 
 [[ 1  2  3  4  5]
 [11 12 13 14 15]
 [21 22 23 24 25]]

Example2:-

arr = np.arange(1,26).reshape(5,5)

print('Values Of Array :- \n', arr )

print("Access value of Columns by step 2 :- \n",arr[:,0:5:2])
# arr[rows ,columns(Start : Stop : Step)]
Output:-
Values Of Array :- 
 [[ 1  2  3  4  5]
 [ 6  7  8  9 10]
 [11 12 13 14 15]
 [16 17 18 19 20]
 [21 22 23 24 25]]
Access value of Columns by step 2 :- 
 [[ 1  3  5]
 [ 6  8 10]
 [11 13 15]
 [16 18 20]
 [21 23 25]]

Accessing values of 3D array

Example1:-

import numpy as np

a3 = np.array([[[10, 11, 12], [13, 14, 15], [16, 17, 18]],
               [[20, 21, 22], [23, 24, 25], [26, 27, 28]],
               [[30, 31, 32], [33, 34, 35], [36, 37, 38]]])
print(a3)

print('This selects matrix index 2 (the final matrix), row 0, column 1 :- \n',a3[2, 0, 1])
Output:-



Example 2:-In this case we are taking row 1, column 2 from each matrix:

import numpy as np

a3 = np.array([[[10, 11, 12], [13, 14, 15], [16, 17, 18]],
               [[20, 21, 22], [23, 24, 25], [26, 27, 28]],
               [[30, 31, 32], [33, 34, 35], [36, 37, 38]]])
print(a3)

print(' Taking row 1, column 2 from each matrix :- \n',a3[:, 1, 2])

Output:-
[[[10 11 12]
  [13 14 15]
  [16 17 18]]

 [[20 21 22]
  [23 24 25]
  [26 27 28]]

 [[30 31 32]
  [33 34 35]
  [36 37 38]]]
 Taking row 1, column 2 from each matrix :- 
 [15 25 35]

Example3:-Taking 1st row of each matrix


import numpy as np

a3 = np.array([[[10, 11, 12], [13, 14, 15], [16, 17, 18]],
               [[20, 21, 22], [23, 24, 25], [26, 27, 28]],
               [[30, 31, 32], [33, 34, 35], [36, 37, 38]]])
print(a3)

print(' Taking row 1 each matrix :- \n',a3[:, 1])
Output:-
[[[10 11 12]
  [13 14 15]
  [16 17 18]]

 [[20 21 22]
  [23 24 25]
  [26 27 28]]

 [[30 31 32]
  [33 34 35]
  [36 37 38]]]
 Taking row 1 each matrix :- 
 [[13 14 15]
 [23 24 25]
 [33 34 35]]


Conclusion

In this blog, you will get knowledge about the Array in NumPy library how to create an array, and accessing the value using the index of the array

LEAVE A REPLY

Please enter your comment!
Please enter your name here