Working with Numpy Arrays: Indexing
You have to be familiar with indexing if you want to work with Numpy arrays and matrices. You will use them when you would like to work with a subset of the array.
About 2d numpy array:
These dimentionals arrays are also known as matrices which can be shown as collection of rows and columns. In this article, we are going to show 2D array in Numpy in Python. NumPy is a very important library in python. We used NumPy for adding support for large multidimensional arrays & matrices .
Importing numpy module in your project:
import numpy as np
Indexing a Two-dimensional Array
import numpy as np array1 = np.arange(12).reshape(4,3) print(array1)
Output:
RESTART: C:/Users/HP/Desktop/article2.py [[ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11]]
Here we use two indices,one we use for rows and one for column.Both the column and the row indices start from 0.
So in above all example we are going to use same matrices.
Select a single element from 2D Numpy Array by index
We can use [][] tese brackets to select an element from Numpy Array .
array1[row_index][column_index]
So if i want to access the element ‘10,’ we use the index ‘3’ for the row and index ‘1’ for the column.
import numpy as np array1 = np.arange(12).reshape(4,3) array1= array1[3][1] print(array1)
Output:
RESTART: C:/Users/HP/Desktop/article2.py 10
Select Rows by Index from a 2D Numpy Array
We can use [] this bracket to select a single or multiple row. For single row use-
array1[row_index]
Let’s take an example,
import numpy as np array1 = np.arange(12).reshape(4,3) row = array1[1] print(row)
Output:
RESTART: C:/Users/HP/Desktop/article2.py [3 4 5]
For multiple rows use,
array1[start_index: end_index , :]
Let’s take an example,
import numpy as np array1 = np.arange(12).reshape(4,3) rows = array1[1:3, :] print(rows)
Output:
RESTART: C:/Users/HP/Desktop/article2.py [[3 4 5] [6 7 8]]
Select Columns by Index from a 2D Numpy Array
Syntax for single column use,
array1[ : , column_index]
Lets take an example,
import numpy as np array1 = np.arange(12).reshape(4,3) column = array1[:, 1] print(column)
Output:
RESTART: C:/Users/HP/Desktop/article2.py [ 1 4 7 10]
Syntax to select multiple columns,
ndArray[ : , start_index: end_index]
Lets take an example,
import numpy as np array1 = np.arange(12).reshape(4,3) columns = array1[: , 1:3] print(columns)
Output:
RESTART: C:/Users/HP/Desktop/article2.py [[ 1 2] [ 4 5] [ 7 8] [10 11]]
Select a Sub Matrix or 2d Numpy Array from another 2D Numpy Array
For sub 2d Numpy Array we pass the row & column index range in [] .
Syntax for this is,
ndArray[start_row_index : end_row_index , start_column_index : end_column_index]
lets take an example,
import numpy as np array1 = np.arange(12).reshape(4,3) sub2DArr = array1[1:3, 1:3] print(sub2DArr)
Output:
RESTART: C:/Users/HP/Desktop/article2.py [[4 5] [7 8]]
Selected Row or Column or Sub Array is View only
This is for view only i.e. if there is any modification in returned sub array will be reflected in original Numpy Array .
We are going to take same array which we have created in start of this article.
import numpy as np array1 = np.arange(12).reshape(4,3) row = array1[1] print(row)
Output:
RESTART: C:/Users/HP/Desktop/article2.py [3 4 5]
Now for any modification in the same row,
import numpy as np array1 = np.arange(12).reshape(4,3) row = array1[1] row[:] = 20 print(row)
Output:
RESTART: C:/Users/HP/Desktop/article2.py [20 20 20]
Modification in sub array will be shown in main Numpy Array too. Updated \ 2D Numpy Array array1 are,
[[ 0 1 2] [20 20 20] [ 6 7 8] [ 9 10 11]]
Get a copy of 2D Sub Array from 2D Numpy Array using ndarray.copy()
Create a 2D Numpy array1 with3 rows & columns | Matrix
import numpy as np array1 = np.array(([12, 22, 13], [21, 25, 30], [33, 17, 19])) print(array1)
Output:
RESTART: C:/Users/HP/Desktop/article2.py [[12 22 13] [21 25 30] [33 17 19]]
Select a copy of row at index 1 from 2D array and set all the elements in selected sub array to 20.
array1 = np.array(([12, 22, 13], [21, 25, 30], [33, 17, 19])) row=array1[1].copy() row[:] = 20 print(row)
Output:
RESTART: C:/Users/HP/Desktop/article2.py [20 20 20]
Conclusion:
So in this article we have seen how to work with NumPy arrays.Hope you have understood everything.