As a first step, import numpy library into the program:
import numpy as np
1. INDEXING IN NUMPY
We have studied indexing techniques in Python list, a similar approach is taken for indexing Numpy array.
Indexing means to access the single element in the array, at a given position,
For 1D array, it is similar to indexing Python’s list
For nD array, it is similar to indexing Python’s lists of lists
1.1. On 1D array
# creating an array
arr1d = np.arange(1,11)
print(f"This is the array: {arr1d}")
# fetching first item in array
print(f"\nFirst Item in the array: {arr1d[0]}")
# fetching last item in array
print(f"\nLast Item in the array: {arr1d[-1]}")
# fetching middle item in array
print(f"\nMiddle Item in the array: {arr1d[int((arr1d.size/2)-1)]}")
This is the array: [ 1 2 3 4 5 6 7 8 9 10]
First Item in the array: 1
Last Item in the array: 10
Middle Item in the array: 5
1.2. On nD array
a. 2D array
For 2D array, we need to provide the position in (x,y) scheme, where x is the x-axis position, and y is the position on y-axis
# 2d array
arr2d = np.arange(10).reshape(2,5)
print("We will perform indexing on this 2D array:")
print(arr2d)
#fetching first item in second row
print(f"\nFirst item in second row: {arr2d[1,0]}")
#fetching last item in first row
print(f"\nLast item in first row: {arr2d[0,-1]}")
We will perform indexing on this 2D array:
[[0 1 2 3 4]
[5 6 7 8 9]]
First item in second row: 5
Last item in first row: 4
b. 3D array
For 3D array, we need to provide the position in (a,x,y) scheme, where a is position of matrix, x is the x-axis position, and y is the position on y-axis
# 3d array
arr3d = np.arange(24).reshape(2,3,4)
print("We will perform indexing on this array:")
print(arr3d)
# from first matrix, fetching first item in second row
print(f"\nFrom first matrix, first item in second row: {arr3d[0,1,0]}")
# from second matrix, fetching last item in last row
print(f"\nFrom second matrix,last item in last row: {arr3d[-1,-1,-1]}")
We will perform indexing on this array:
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
From first matrix, first item in second row: 4
From second matrix,last item in last row: 23
2. SLICING IN NUMPY
Slicing means to access subarray from the main array. We use [:] slice notion to perform slicing. Remember that slicing return views rather than copies of the array data. The standard format for slicing is:
1darray[start:stop:step]
The default value for start=0, step=1 and stop=index position before to stop
2.1. On 1D array
We will use the following syntax: 1darray[start:stop:step]
print(f"This is the array: {arr1d}")
# fetching first 3 elements
print(f"\nFirst three elements: {arr1d[:3]}")
# fetching last 3 elements, using negative index
print(f"Last three elements: {arr1d[-3:]}")
# fetching every other elements
print(f"Every other element: {arr1d[::2]}")
# fetching every other elements, starting from '2', with index '1'
print(f"Every other element, starting from 2: {arr1d[1::2]}")
This is the array: [ 1 2 3 4 5 6 7 8 9 10]
First three elements: [1 2 3]
Last three elements: [ 8 9 10]
Every other element: [1 3 5 7 9]
Every other element, starting from 2: [ 2 4 6 8 10]
Reversing the order: By providing step=-1, we reverse the order of elements in the array
# reversing the array
print(f"Reversing the array: {arr1d[::-1]}")
# reversing the array, every other item
print(f"Reversing the array, every other item: {arr1d[::-2]}")
Reversing the array: [10 9 8 7 6 5 4 3 2 1]
Reversing the array, every other item: [10 8 6 4 2]
2.2. On nD array
In this section, we move from 1D arrays to arrays with more than 1 dimension.
a. 2D array
For 2D array, we will use the same syntax for slicing, but each axis slicing point is separated by comma
2darray[start:stop:step, start:stop:step]
arr2d2d = np.arange(25).reshape(5,5)
print("We will perform slicing on this 2D array:")
print(arr2d2d)
# entire first row
print(f"\nFetching first row: {arr2d2d[0,:]}")
# entire first column
print(f"\nFetching first column: {arr2d2d[:,0]}")
# 2 rows, three columns
print(f"\nFetching 2 rows, 3 column: \n{arr2d2d[0:2,0:3]}")
# all rows, every other column
print(f"\nFetching all rows, every other column: \n{arr2d2d[:,::2]}")
We will perform slicing on this 2D array:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]
[20 21 22 23 24]]
Fetching first row: [0 1 2 3 4]
Fetching first column: [ 0 5 10 15 20]
Fetching 2 rows, 3 column:
[[0 1 2]
[5 6 7]]
Fetching all rows, every other column:
[[ 0 2 4]
[ 5 7 9]
[10 12 14]
[15 17 19]
[20 22 24]]
Reversing in 2D array We will reverse:
both rows and columns values,
only rows,
only column
print("We will perform slicing on this 2D array:")
print(arr2d2d)
# reversing the order of elements in 2d array, at both axis
print(f"\nReversing the entire content,rows and columns, of 2D array: \n{arr2d2d[::-1,::-1]}")
# reversing the order of rows only, first becomes last and so-on
print(f"Reversing the order of rows only: \n{arr2d2d[::-1,:]}")
# reversing the order of columns only, first becomes last and so-on
print(f"Reversing the order of columns only: \n{arr2d2d[:,::-1]}")
# creating 3D array
arr3d3d = np.arange(36).reshape(3,3,4)
print("We will perform slicing on this array:")
print(arr3d3d)
# first row, of every dimension
print(f"\nFirst row of every dimension: \n{arr3d3d[:,:1,:]}")
# first column, of every dimension
print(f"\nFirst column of every dimension: \n{arr3d3d[:,:,:1]}")
# every other row and column, in every other dimension
print(f"\nEvery other row and column, in every other dimension: \n{arr3d3d[::2,::2,::2]}")
We will perform slicing on this array:
[[[ 0 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 26 27]
[28 29 30 31]
[32 33 34 35]]]
First row of every dimension:
[[[ 0 1 2 3]]
[[12 13 14 15]]
[[24 25 26 27]]]
First column of every dimension:
[[[ 0]
[ 4]
[ 8]]
[[12]
[16]
[20]]
[[24]
[28]
[32]]]
Every other row and column, in every other dimension:
[[[ 0 2]
[ 8 10]]
[[24 26]
[32 34]]]
Reversing the 3D array
# all rows, columns and dimensions
print(f"Reversing the entire 3D array: \n{arr3d3d[::-1,::-1,::-1]}")
# Reversing rows only
print(f"\nReversing only rows in 3D array: \n{arr3d3d[:,::-1,:]}")