Part -1: NumPy Basic Array Operations

The NumPy array is a data structure for storing and accessing multidimensional arrays (also known as tensors) that allows for a wide range of scientific computations. Just like python list we can access element from NumPy array. If you dont know what is NumPy then visit our NumPy: The absolute basics for beginners post.

In this post we will learn what is basic property of numpy arrays and some mathematical operations of array.

Single Dimensional Array
>>> import numpy as np
>>> a = np.array([5,8,9])
>>> a[0]
5   
>>> a[2]
9   

This is single or one dimensional array using NumPy here you can seen we can access that array element like python list.

To index from the end of the array, you can use negative indices:

>>> a = np.array([5,8,9])     
>>> a[-1] 
9
Two Dimensional Array

You can store two dimensional array in NumPy

>>> a = np.array([[1,2,3],[2,4,6]])
>>> a
array([[1, 2, 3], 
       [2, 4, 6]])
>>> a[0] 
array([1, 2, 3])

To index from the end of the array, you can use negative indices:

>>> a = np.array([[1,2,3],[2,4,6]])
>>> a[-1] 
array([2, 4, 6])
ndim

There is a property called ndim in NumPy to use print number of dimensions (axes) of the array:

>>> a= np.array([5,6,7]) 
>>> a.ndim
1
>>> a = np.array([[1,2,3],[2,4,6]])
>>> a.ndim
2
dtype

Using dtype property we can find the datatype of the elements of a NumPy array.

>>> a = np.array([[1,2,3],[2,4,6]])
>>> a
array([[1, 2, 3], 
       [2, 4, 6]])
>>> a.dtype
dtype('int32')

Converting integer to float in Numpy array

>>> a = np.array([[1,2,3],[2,4,6]], dtype=np.float64) 
>>> a
array([[1., 2., 3.],
       [2., 4., 6.]])
itemsize

itemsize property returns the size (in bytes) of each element of a NumPy array.

>>> a = np.array([[1,2,3],[2,4,6]])
>>> a
array([[1, 2, 3], 
       [2, 4, 6]])
>>> a.itemsize
4
>>> a = np.array([[1,2,3],[2,4,6]], dtype=np.float64) 
>>> a
array([[1., 2., 3.],
       [2., 4., 6.]])
>>> a.itemsize
8
size

size is the total number of elements in the NumPy array

>>> a = np.array([[1,2,3],[2,4,6]])
>>> a
array([[1, 2, 3],
       [2, 4, 6]])
>>> a.size
6
shape

shape is a tuple of integers representing the size of the NumPy array in each dimension.

e.g. for this 2-dimensional array [[1,2,3],[2,4,6]], value of shape will be (2,3) because this ndarray has two dimensions – rows and columns – and the number of rows is 2 and the number of columns is 3

array dimensions
>>> a = np.array([[1,2,3],[2,4,6]])
>>> a
array([[1, 2, 3],
       [2, 4, 6]])
>>> a.shape
(2, 3)
reshape()

Another useful type of operation is reshaping of arrays. The most flexible way of doing this is with the reshape method.

For example, if you want to put the numbers 1 through 9 in a 3×3 grid, you can do the following:

>>> a =  np.arange(1, 10).reshape((3, 3))
>>> a
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
>>> a = np.array([[1,2,3],[2,4,6]])
>>> a
array([[1, 2, 3],
       [2, 4, 6]])
>>> a.shape
(2, 3)
>>> a.reshape((3,2))
array([[1, 2],
       [3, 2],
       [4, 6]])
zeros()

This function is used to get a new array of given shape and type, filled with zeros(0). 

Syntax: numpy.zeros(shape, dtype=None)
>>> np.zeros( (4,3) )
array([[0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.]])
ones()

This function is used to get a new array of given shape and type, filled with ones(1).

Syntax: numpy.ones(shape, dtype=None, order=’C’)
>>> np.ones([4, 3],dtype = np.int32,order = 'f')         
array([[1, 1, 1],
       [1, 1, 1],
       [1, 1, 1],
       [1, 1, 1]])
arange()

This is a NumPy function that returns values that are equally spaced inside a given interval.

Syntax: numpy.arange(start, stop, step, dtype=None)
>>> np.arange(6)
array([0, 1, 2, 3, 4, 5])
>>> np.arange(2,9)
array([2, 3, 4, 5, 6, 7, 8])
>>> np.arange(5,15,2)
array([ 5,  7,  9, 11, 13])
linspace()

This function returns numbers that are uniformly spaced between two limits.

Syntax: numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)
>>> np.linspace(1, 10, 3)   
array([ 1. ,  5.5, 10. ])
>>> np.linspace(1, 10, 5) 
array([ 1.  ,  3.25,  5.5 ,  7.75, 10.  ])
ravel()

This function used to flatten array.

>>> a = np.arange(6)
>>> a
array([0, 1, 2, 3, 4, 5])
>>> a.reshape((6,1))
array([[0],
       [1],
       [2],
       [3],
       [4],
       [5]])
>>> a.ravel()
array([0, 1, 2, 3, 4, 5])
min() and max()

this function used to print minimum or maximum element value from array.

>>> a = np.array([[1,2,3],[2,4,6]])
>>> a
array([[1, 2, 3],
       [2, 4, 6]])
>>> a.min()
1
>>> a.max()
6
sum()

this function used to print sum all element of array.

>>> a = np.array([[1,2,3],[2,4,6]])
>>> a
array([[1, 2, 3],
       [2, 4, 6]])
>>> a.sum()
18

There is a option axis in sum(). axis = 0 will be column of array and axis = 1 will be row of array.

>>> a = np.array([[1,2,3],[2,4,6]])
>>> a
array([[1, 2, 3],
       [2, 4, 6]])
>>> a.sum(axis=0)
array([3, 6, 9])
>>> a.sum(axis=1) 
array([ 6, 12])
sqrt()

This function used to print square root of each element of array.

>>> a = np.array([[1,2,3],[2,4,6]])
>>> a
array([[1, 2, 3], 
       [2, 4, 6]])
>>> np.sqrt(a) 
array([[1.        , 1.41421356, 1.73205081], 
       [1.41421356, 2.        , 2.44948974]])

Mathematical Operations

you can perform a mathematical operations with your numpy array. Here is some of the examples.

>>> a =np.array([[2,4,6],[8,10,12]]) 
>>> a
array([[ 2,  4,  6],
       [ 8, 10, 12]])
>>> b = np.array([[6,4,9],[2,8,3]])
>>> b
array([[6, 4, 9],
       [2, 8, 3]])
Addition :
>>> a+b
array([[ 8,  8, 15],
       [10, 18, 15]])
Multiplication
>>> a*b
array([[12, 16, 54],
       [16, 80, 36]])
Subtraction
>>> a-b
array([[-4,  0, -3],
       [ 6,  2,  9]])
Division
>>> a/b
array([[0.33333333, 1.        , 0.66666667],
       [4.        , 1.25      , 4.        ]])
Matrix Product
>>> c =b.reshape((3,2)) 
>>> a.dot(c)
array([[ 96,  34],
       [234,  88]])

That is it for today, hope it helps.  If you have any suggestion for this article please make a comment in comment section below.

If you like this article, you can buy me a coffee. Thanks!

Part -1: NumPy Basic Array Operations

One thought on “Part -1: NumPy Basic Array Operations

  1. Having read this I thought it was very informative. I appreciate you taking the time and effort to put this article together. I once again find myself spending way to much time both reading and commenting. But so what, it was still worth it!

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top