Numpy:

Rion Louji
6 min readJun 10, 2021

Numpy is a python library which works in array. It also has many function in the area of linear algebra, Fourier transform , matrices and so on… Numpy literally means Numerical Python. It was created by Travis Oliphant in the year 2005.

In python we have lists, but which is a slow process. Numpy is a library written in python to speed up the process. It can be utilised to perform various operations such as statistical, algebraic trignometric and so on…

List of contents:

  1. How to install numpy?
  2. How to import numpy?
  3. Converting a list into an array
  4. Creating a 3D Array
  5. Finding dimensions, shape and different Data Types
  6. To find Different datatypes
  7. Creating random arrays
  8. Limits in random multi dimensional array
  9. Function Operations in arrays
  10. Math functions with numpy

1.Installing numpy:

There are no prerequisite for installing python. You need only python to install numpy.

If you use pip use the below code to install numpy.

pip install numpy

If you use conda use the below code to install numpy

conda create -n my-env
conda activate my-env
conda config --env --add channels conda forge
conda install bumpy

2.Importing numpy:

After installing numpy, to use it and its functions you need to import it. This can easily be done by using import statement. You can use the below code to import numpy.

import numpy as np

3.Creating a list and converting it into an np array:

List and array both are data structures. List is a linear data structure where it contains heterogeneous elements. i.e) It is a collection of elements that contains multiple data types such as numeric, characters,... Whereas an array is a data structure that contains homogeneous elements. i.e) Elements Of same Data Type. The major difference between an array and a list is that list cannot handle arithmetic operations directly whereas arrays can handle arithmetic operations directly.

The below code converts a list into an array

ls=[1,2,3]
ls=np.array([1,2,3])
ls
Output:
array([1, 2, 3])

a) Saving variable in np:

ls1=[[1,2,3],[4,5,6],[7,8,9]]np.array(ls1)

4.Creating a 3D array:

3D Array is a multidimensional array that is a collection of 2D Arrays. In order to declare a 3D array we have specify the block size, row size and column size which is as given below.

ls3d = np.array([[[1,2,3],[4,5,6],[7,8,9]],[[11,12,13],[14,15,16],[17,18,19]],[[21,22,23],[24,25,26],[27,28,29]]])np.array(ls3d)Output:array([[[ 1,  2,  3],         [ 4,  5,  6],         [ 7,  8,  9]],
[[11, 12, 13], [14, 15, 16], [17, 18, 19]],
[[21, 22, 23], [24, 25, 26], [27, 28, 29]]])

5. a) Dimensions:

Dimension of an array represents the size of the array.To find Dimensions, we use the keyword “array_name.dim” .

i) This code gives the dimension of a 1D array

#1D Arrayls.ndimOutput:
1

ii) This code gives the dimension of 3D Array

#3D Arrayls3d.ndimOutput:
3

6. Shape:

Shape of an array is the the number of elements in each dimension.To get the shape of the array, we use “array_name.shape”.

i) Getting the shape of 1D array

ls.ndimOutput:
(3,)

ii)Getting the shape of 3D array:

ls3d.shapeOutput: 
(3, 3, 3)

7.To find different datatypes:

Basically we have five different data types such as strings, integer, float, complex and boolean. In numpy we have some more additional datatypes such as unsigned integer, complex float, datetime, timedelta, object, unicode string. Inorder to find the data type of an array we use “dtype”.

word=np.array(["Rion","Louji","Rion_Louji"])
print(word.dtype)
Output:<U10boo = np.array([True,False,True])
boo.dtype
Output:
dtype('bool')

8. Creating Random Arrays:

i) Creating all elements as zeroes in the array:

The argument determines the size of the array.

#1d array of all 0's
np.zeros(2)
Output:
array([0., 0.])
#2D array of all 0's
np.zeros((2,3))
Output:
array([[0., 0., 0.], [0., 0., 0.]])
#3D array of all zeros
np.zeros(((3,3,3)))
Output:
array([[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]])

ii) Creating all elements as ones in the array:

#1d array of all 1's
np.ones(2)
Output:
array([1., 1., 1., 1.])
#3d array of all 1's
np.ones(((1,4,3)))
Output:
array([[[1., 1., 1.],[1., 1., 1.],[1., 1., 1.],[1., 1., 1.]]])

iii) Creating arrays with a particular number:

np.full(((3,3,3)),8)Output:
array([[[8, 8, 8], [8, 8, 8], [8, 8, 8]],
[[8, 8, 8], [8, 8, 8], [8, 8, 8]],
[[8, 8, 8], [8, 8, 8], [8, 8, 8]]])

iv) Creating array with random numbers:

#This will create float valuesnp.random.rand(4,2)Output:
array([[0.1562124 , 0.0252378 ], [0.6406471 , 0.89942884],
[0.36301276, 0.42914325], [0.04054535, 0.21043029]])

v) Creating an array with random integer:

np.random.randint(3,4)Output:
4

8.Limits in random multi dimensional array:

np.random.randint(5, size=(3,4))Output:
array([[2, 3, 4, 2], [2, 3, 2, 2], [1, 0, 3, 4]])

In the above code, the first argument limits the integers(i.e. creates a random array within value 5 ) and the second argument determines the size of the array.

np.random.randint(4,8,size=(3,3,3))Output:array([[[6, 7, 5],         [4, 7, 6],         [7, 6, 5]],        
[[4, 7, 5], [6, 6, 7], [5, 5, 7]],
[[5, 5, 4], [7, 5, 4], [5, 5, 5]]])

This code creates a random 3D array for which the values lie between 4 and 7. On the same way, we can do it for 2D arrays.

9. Function operations in arrays:

#defining the function
def func(num1,num2):
return num1*num2
# Calling the function
np.random.randint(func(2,6),size=(4,4,6))
Output:
array([[[ 9, 11, 1, 4, 0, 7], [ 6, 10, 2, 5, 6, 7],
[ 3, 0, 9, 2, 5, 9], [ 3, 7, 5, 0, 1, 8]],
[[ 6, 7, 2, 10, 4, 1], [10, 11, 5, 10, 10, 8],
[ 9, 8, 9, 5, 11, 7], [ 2, 4, 11, 10, 6, 6]],
[[ 2, 2, 7, 5, 0, 3], [ 5, 6, 4, 8, 11, 5],
[ 2, 4, 4, 5, 3, 10], [11, 2, 4, 3, 6, 9]],
[[ 9, 3, 5, 11, 11, 3], [ 1, 6, 5, 2, 0, 2],
[11, 5, 9, 1, 4, 0], [ 5, 5, 11, 4, 11, 0]]])

10.Math functions with numpy:

i)Arithmetic Operations:

a)Addition:

The arithmetic operations performed in arrays simply perform those mentioned operations to all the elements in array which is described above.

# initialising array
arr1=np.array([1,2,3,4])
#Basic arithmetic operations
arr1+3
Output:
array([4, 5, 6, 7])

b)Multiplication:

Multiplying two arrays with different size is as similar to the multiplication of two different sized matrices. For example, when a matrix of order 2X4 and 4X3 are multiplied, the resultant matrix will be order of 2 X3.

#storing two arrays in a variable
a=np.random.randint(5,size=(2,4))
b=np.random.randint(7,size=(4,3))
# Multiplying two arrays a and b
np.matmul(a,b)
Output:
array([[31, 39, 16], [19, 29, 11]])

ii) Statistical Operations:

i) Minimum and Maximum value of an array:

#Declaration of array
stats=np.array([[1,2,3,4],[5,6,7,8]])
#Min and Max of the array
c=np.min(stats)
d=np.max(stats)
print(c)
print(d)
Output:
1
8

ii)Mean, median, mode:

stats=np.array([[1,2,3,4],[5,6,7,8]])
np.mean(stats)
Output:
4.5

iii)Sum:

The above keyword sums up all the elements in an array.

stats=np.array([[1,2,3,4],[5,6,7,8]])
np.sum(stats)
Output:
36

11.Rearranging the rows and columns of an array:

Here ,the rows and columns are interchanged. (i.e. the rows transform into a column and the column transform into a row).

#Checking the array
rionarr=np.array([[1,2,3,4],[5,6,7,8]])
print(rionarr)
Output:
[[1 2 3 4]
[5 6 7 8]]
#Getting the shape of the array
np.shape(rionarr)
Output:
(2, 4)
rionarr1=rionarr.reshape((4,2))
print(rionarr1)
Output:
[[1 2]
[3 4]
[5 6]
[7 8]]
#Reshaping the array
np.shape(rionarr1)
Output:(4, 2)

Feel free to contact me for any clarifications or queries.

Email: sjrion01@gmail.com

Happy coding……..

 by the author.

--

--