NumPy
--> numpy means numeric python
--> This module is used to performing mathematical and numerical
operations
Note:- numpy module is not a part of standard python , so we have to install
separetly
--> If you want to install any library, we have to open "pypi.org" website
then copy the command
Then open command prompt execute that command
Microsoft Windows [Version 10.0.17763.1217]
(c) 2018 Microsoft Corporation. All rights reserved.
C:\Users\Venkat>pip list
Package Version
---------- -------
pip 19.2.3
setuptools 41.2.0
xlwt 1.3.0
WARNING: You are using pip version 19.2.3, however version 20.1.1 is
available.
You should consider upgrading via the 'python -m pip install --upgrade pip'
command.
C:\Users\Venkat>python -m pip install --upgrade pip
Collecting pip
Downloading
https://files.pythonhosted.org/packages/43/84/23ed6a1796480a6f1a2d38f
2802901d078266bda38388954d01d3f2e821d/pip-20.1.1-py2.py3-none-
any.whl (1.5MB)
|¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦| 1.5MB 595kB/s
Installing collected packages: pip
Found existing installation: pip 19.2.3
Uninstalling pip-19.2.3:
Python with Venkat @ Sathya Technologies Page 1
Successfully uninstalled pip-19.2.3
Successfully installed pip-20.1.1
C:\Users\Venkat>pip install numpy
Collecting numpy
Downloading numpy-1.18.4-cp38-cp38-win_amd64.whl (12.8 MB)
|¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦| 12.8 MB 595 kB/s
Installing collected packages: numpy
Successfully installed numpy-1.18.4
C:\Users\Venkat>pip list
Package Version
---------- -------
numpy 1.18.4
pip 20.1.1
setuptools 41.2.0
xlwt 1.3.0
C:\Users\Venkat>pip install matplotlib
Collecting matplotlib
Downloading matplotlib-3.2.1-cp38-cp38-win_amd64.whl (9.2 MB)
|¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦| 9.2 MB 3.2 MB/s
Collecting cycler>=0.10
Downloading cycler-0.10.0-py2.py3-none-any.whl (6.5 kB)
Collecting pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1
Downloading pyparsing-2.4.7-py2.py3-none-any.whl (67 kB)
|¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦| 67 kB 1.1 MB/s
Collecting kiwisolver>=1.0.1
Downloading kiwisolver-1.2.0-cp38-none-win_amd64.whl (58 kB)
|¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦| 58 kB 713 kB/s
Requirement already satisfied: numpy>=1.11 in e:\python\pythonsw\lib\site-packages
(from matplotlib) (1.18.4)
Collecting python-dateutil>=2.1
Downloading python_dateutil-2.8.1-py2.py3-none-any.whl (227 kB)
|¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦| 227 kB 6.8 MB/s
Collecting six
Downloading six-1.14.0-py2.py3-none-any.whl (10 kB)
Installing collected packages: six, cycler, pyparsing, kiwisolver, python-dateutil, matplotlib
Successfully installed cycler-0.10.0 kiwisolver-1.2.0 matplotlib-3.2.1 pyparsing-2.4.7
python-dateutil-2.8.1 six-1.14.0
C:\Users\Venkat>
Python with Venkat @ Sathya Technologies Page 2
Using numpy module we can create the arrays in different ways
1) array( ):- This function is used to create an array
in arry, we have different types of attributes , they are
i) size : - it will return the number of elements in the array
ii)shape :-This attribute returns a tuple consisting of array
dimensions
iii) dtype :- it will specifies the type of data stroed in array
Ex1:
from numpy import *
a1 = array([5,3,8,2,9])
print(type(a1))
print(a1.size)
print(a1.shape)
Ex2:
from numpy import *
a1 = array([[3,7,4] ,[5,9,1]])
print(a1)
print("Number of Elements are : ",a1.size)
print("Shape of array is : ", a1.shape)
2) arange( ): - This function returns an array object containing evenly spaced
values with in a given range
arange(5) :- [01234]
arange(1,6) :- [1 2 3 4 5]
arange(1,8,2) :- [ 1 3 5 7]
3) reshape() :- This function is used to reshape an array
from numpy import *
a1 = arange(1,13)
print(a1)
#a2 = a1.reshape(2,3) # Error
a2 = a1.reshape(3,4)
print(a2)
Python with Venkat @ Sathya Technologies Page 3
4) ones( ) : This function returns a new array of given shape and type with
ones
from numpy import *
a1 = ones(6) # By defaule float
print(a1)
a2 = ones(4,int)
print(a2)
a3 = ones((3,4),dtype =int)
print(a3)
5) zeros( ) : this function returns a new array of given shape and type with
zeros
from numpy import *
a1 = zeros(6) # By defaule float
print(a1)
a2 = zeros(4,int)
print(a2)
a3 = zeros((3,4),dtype =int)
print(a3)
6) transpose( ) : This function is used to transpose an array
from numpy import *
a1 = array([[2,4,6],[3,5,7]])
print("Your Matrix is : ")
print(a1)
a2 = transpose(a1)
print(" Your Transpose Matrix is :")
print(a2)
7) add( ) : This function is used to perform addition of two array
from numpy import *
a1 =[ [2,4,5],[3,7,6]]
a2 =[ [1,2,3], [6,4,1]]
a3 = add(a1,a2)
print(a1)
print(a2)
Python with Venkat @ Sathya Technologies Page 4
print(a3)
8) subtract( ) :- This function is sued to perform subtraction of two arrays
from numpy import *
a1 =[ [2,4,5],[3,7,6]]
a2 =[ [1,2,3], [6,4,1]]
a3 = subtract(a1,a2)
print(a1)
print(a2)
print(a3)
9)multiply( ) :- This function is used to perfrom multiplication of two arrays
from numpy import *
a1 =[ [2,4,5],[3,7,6]]
a2 =[ [1,2,3], [6,4,1]]
a3 = multiply(a1,a2)
print(a1)
print(a2)
print(a3)
10) divide( ) : This function is used to divide an element from first array by an
element from second element
from numpy import *
a1 =[ [2,4,5],[3,7,6]]
a2 =[ [1,2,3], [6,4,1]]
a3 = divide(a1,a2)
print(a1)
print(a2)
print(a3)
11) randint( ): - This function will generate a random int value which can be
stored into an array of the specified size
from numpy import *
a1 = random.randint(20,70,(3,2))
print(a1)
Python with Venkat @ Sathya Technologies Page 5