Profile Pic

Iqbal Ahmed

I develop Android Apps along with Backend
and My only LOVE is Kotlin

4 minute read

As we have used Numpy in our previous post, In this post we gonna learn more about the basics of Numpy which gonna help us in OpenCV.

Numpy is the homogeneous n-dimensional array. Unlike Python list. Numpy array keep all the elements in the same data type. In Numpy, dimensions we called axis. Axis just tells us how many dimensions in the array.

For eg: [1, 2, 1] this is 1-D array, means 1 axis with 3 elements,

[
    [1 , 0 , 1],
    [0 , 1 , 0]
]

this is 2-D array, means 2 axis, first axis length is 2(rows) and the second axis lenght is 3(columns)

Important methods/attributes which we use in daily in numpy

>> import numpy as np
>> a = np.array(
[
    [1 , 0 , 1],
    [0 , 1 , 0]
    [1 , 1 , 1]
])
>>> a.ndim #this will give the number of axis(dimensions) of the array
2
>>> a.shape #shape of the array or total number of rows and cols
(3, 3)
>>> a.dtype #data type of element
dtype('int64')

Let’s learn some more about Numpy creation, types, indexing, and slicing

To create an array

>>> np.array([1, 2, 3])  #This is a integer array
array([1, 2, 3])
>>> np.array([1.2, 3.5, 5.1]) #This is float 
array([1.2, 3.5, 5.1])
>>> np.array([[1, 2 , 3],[4, 5, 5]]) #2-D array

Fill the array with zeros

To create an array full of zeros we just pass rows and columns

>>> np.zeros((3,4))
array([[0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.]])

Fill the array with ones

To create an array full of ones same like zeros we just pass rows and columns

>>> np.ones((3,4)) 
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]])

Create identity array

>>> np.identity(5) # 5 x 5 array with 1 in diagonal (Identiy Matrix)
array([[1., 0., 0., 0., 0.],
       [0., 1., 0., 0., 0.],
       [0., 0., 1., 0., 0.],
       [0., 0., 0., 1., 0.],
       [0., 0., 0., 0., 1.]])

Create an array with fixed size and range

>>> np.linspace(0, 3, 6) # 6 numbers from 0 to 3
array([0. , 0.6, 1.2, 1.8, 2.4, 3. ])

we can do maths operations also between two arrays, like addition, multiplication, dots product.

For eg:

>>> a = np.array([1,2,3,4,5])
>>> b = np.array([6,7,8,9,10])
>>> a + b 
array([ 7,  9, 11, 13, 15])
>>> a + 30
array([31, 32, 33, 34, 35])
>>> a * b
array([ 6, 14, 24, 36, 50])
>>> a * 10
array([10, 20, 30, 40, 50])
>>> a @ b #dot product
130

Indexing and slicing

1D array slicing and indexing

>>> a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> a
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> a[2] #print index at 2, 2
3
>>> a[2:5] #print from index 2 to index 5
array([3, 4, 5])
>>> a[:5:2] = 101 #replace every 2nd element to 101,from 0 to 5 with step 2
>>> a
array([101, 2, 101, 4, 101, 6, 7, 8, 9])
>>> a[::-1] # reverse the array
array([9, 8, 7, 6, 101, 4, 101, 2, 101])

2D array slicing and indexing

>>> a = np.array([
    [ 0,  1,  2,  3],
    [10, 11, 12, 13],
    [20, 21, 22, 23],
    [30, 31, 32, 33],
    [40, 41, 42, 43]
    ])
>>> a[2, 3] # print row 2nd and 3rd column, 23
23
>>> a[0:5, 1] #from 0 to 5th row, 1st column
array([ 1, 11, 21, 31, 41])
>>> a[:,1] # all the rows of 1st column
array([ 1, 11, 21, 31, 41])
>>> a[1:3, :] # form 1st row to 2nd row all the columns
array([[10, 11, 12, 13],
       [20, 21, 22, 23]])
>>> a[-1] # last row
array([40, 41, 42, 43])
>>> a[:-2, :] # form 0th to 2nd last row(total rows - 2) and all the columns
array([[ 0,  1,  2,  3],
       [10, 11, 12, 13],
       [20, 21, 22, 23]])

all the source code you can find in the repo and to run this source code you can install jupyter notebook or run online in the browser.

Say something

Comments

Nothing yet.

Recent posts

Categories

About

I develop Android Apps along with Backend
and My only LOVE is Kotlin