NUMPY
What is Numpy ?
- Numpy is library.
- library: collection of modules is called library.
- module: collection of functions is called module.
- Numpy is used to deal with N-Dimensional arrays.
- dealing means create and manipulate the N-Dimensional arrays . Numpy , which stands for numerical python ,is a library consisting of multidimensional array objects and a collection of routines for processing those arrays. Using Numpy,mathematical and logical operations on arrays can be performed.This tutorial explains the basics of Numpy such as its architecture and evironment.It also discuss the various array functions,types of indexing,etc.
Why we Use Numpy?
In python we have lists that serve the purpose of array, but they are slow to process. Numpy aims to provide an array object that is up to 50x faster than traditional python lists.
Arrays:
It is a sequential collection of data type,it should hold same type of data.There are n number of dimensions. If n=0(scalar),n=1(vector),n=2(matrix),………..,n=32
- Arrays is a collection data type.
- It stores same type of data only.
- n=0 scaler
- n=1 vector
- n=2 matrix
- n>2 tensor of rank n
- Create n dimentional array:
- Import numpy:
- Import numpy in our applications by adding the “import” keyword,Now Numpy is imported and ready to use.
- code:import numpy
- now create an array:
- code:
- 0-dimention
a=numpy.array(10)
a
2. 1-dmention
a=numpy.array([1,2,3,4,5])
a
3. 2-dimention
a=numpy.array([1,2,3,4,5],[6,7,8,9,10])
a
Installation:
- pip install numpy.
- import numpy as np.
np.array () :
Default function to create n-dimensional array.
parameter can be list or tuple.
array() function always return n-dimensional array object, where all data consider as object and data types considered as class.
np.array(data,ndmin=rank of dimension,dtype=”data type of elements”)
w=np.array([[1,2,3,],[1,4,5],[34,5,6]],ndmin=9,dtype=float)ndim: used to know the dimension of the array.
w.ndim#o/p:2shape: used to know the shape of the array.
w.shape#o/p:(3,3)dtype: used to know the data type of the elements present in the array.
w.dtype#o/p:ndarrayImportant Numpy Functions in Python:
- np.array (): This function is used to create an array in NumPy.
ex: Import numpy as np
import numpy as np
2. np.zeros(shape,dtype=float)
it will return a N-dimensional array where all the elements are zeros only.
np.zeros((3,5))#o/p
array([[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]])default dtype will be float in zeros.
3.Ones(): It create n-dimentional array, all the elements sholud be 1 (one).
np.ones((8,2))
it will return a N-dimensional array where all the elements are ones only.
np.ones((8,2))#o/p
array([[1., 1.],
[1., 1.],
[1., 1.],
[1., 1.],
[1., 1.],
[1., 1.],
[1., 1.],
[1., 1.]])4.Full(): The numpy. full() function is used to return a new array of a given shape and data type filled with fill_value .
it will return a N-dimensional array where all the elements are the element which we have given.
np.full((2,3),4)#o/p
array([[4, 4, 4],
[4, 4, 4]])5. np.eye(value)
it always returns square identity matrix only based on the value we have given which is equals to no.of colums and rows.
default dtype will be float.
np.eye(4,dtype=int)#o/parray([[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]])
6. np.arange(start range,ending range,step size,dtype)
it will always returns 1-d array.
in this ending range in excluded.
np.arange(0,20,5)#o/p
array([ 0, 5, 10, 15])7. np.diag(data)
it will always returns square matrix of array where diagonal elements will be the list of elements we have given and all other are zeros.
np.diag([1,2,3,4,5])#o/parray([[1, 0, 0, 0, 0],
[0, 2, 0, 0, 0],
[0, 0, 3, 0, 0],
[0, 0, 0, 4, 0],
[0, 0, 0, 0, 5]])
8. np.linspace(starting value,ending value,spaces we want to divide)
it will always returns 1-D array of values by dividing them into equal spaces.
default spaces is equal to 50.
np.linspace(1,10,10)#o/p:
array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])9.RANDOM MODULE:
random module is collection of function.
to use functions available in random module we need to import random first.
import random
9.1.random.random():it will always returns a random float value between 0 and 1.
np.random.random()0.75596151324732579.2.random.rand(shape):it will return n-d array of elements between 0 and 1.
np.random.rand(4,2)array([[0.33075413, 0.39864716],
[0.32140947, 0.49762384],
[0.53635311, 0.32898758],
[0.26579894, 0.93014751]]9.3.random.randint(starting value,ending value,size=[2,3]):it will return n-d array by excluding ending value.
np.random.randint(1,4,size=[3,4])array([[2, 3, 2, 3],
[3, 3, 1, 2],
[2, 2, 3, 2]])9.4 random.choice(list of elements,size=,p=[sum must equal to 1]):it will return random elements from the list according to size by depending on the probability.
np.random.choice([1,2,3,4,5],size=[10],p=[0.6,0.1,0.1,0.1,0.1]) array([5, 1, 1, 3, 4, 3, 3, 1, 2, 3])DIFFERENCE BETWEEN LIST AND ARRAY:
LIST :
- collection data type.
- sequential data.
- it can hold same and different types of data.
- manipulation done on lists is slow when compared to arrays.
- in lists we can’t do vectorization operation.
- we can change the size of the list.
- it takes more space compared to arrays.
ARRAYS:
- > collection data type.
- sequential data.
- it can hold same type of data only.
- manipulation done on arrays is faster when compared to lists.
- in arrays we can perform vectorization operation.
- size is immutable.
- it takes less space compared to lists.
MANIPULATION ON ARRAYS:
- Accessing elements:
- 1.1 in 1-D array
x=np.array[1,2,3,4,5])
x[0]o/p:1
x[0:4]
o/p:[1,2,3,4]
- 2. In 2-D arrays
- integer based accessing.
- slicing based accessing.
- boolean based accessing.
- 2.1.Integer based accessing:
properties:
can access any orbitary points and sequential data also.
sub array is always 1-D lower than parent array.
sub array is copy of the original array.
z=np.array([[1,2,3,4],[5,6,7,8],[10,11,12,13],[20,30,40,23],[222,45,67,89]])array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 10, 11, 12, 13],
[ 20, 30, 40, 23],
[222, 45, 67, 89]])z[[1],[1]] array([6])
- 2.2.slicing based accessing:
properties:
Get Psuryachinni’s stories in your inbox
Join Medium for free to get updates from this writer.
it will return sequence of elements.
sub array is always view of the original array.
always sub array having same rank as original array.
x=np.array([[1,2,3,4],[5,6,7,8],[10,11,12,13],[20,30,40,23],[222,45,67,89]])xarray([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 10, 11, 12, 13],
[ 20, 30, 40, 23],
[222, 45, 67, 89]])
x[1:3,2:] array([[ 7, 8],
[12, 13]])
- 2.3.Boolean based accesing:
properties:
this accessing is totally depends on condition.
it returns boolean array and it acts as mask to the original array.
it displays true indexed elements only.
we need to follow two steps:
- create mask(using comparision and logical operators)
- apply the mask on specific array.
x=np.array([[1,2,3],[4,5,6],[6,7,8]])
y=x%2==0
x[y]
array([2, 4, 6, 6, 8])
y
array([[False, True, False],
[ True, False, True],
[ True, False, True]])MATHEMATICAL OPERATIONS ON ARRAYS:
mathematical operations applied on each elements because of vectorization .
- comparision operators
- logical operators
properties of mathematical operations on arrays:
1.sub array is lower rank than parent array.
2.always 1-d output
3.sub array is copy of original array.
DIFFERENT TOOLS TO PERFORM MANIPULATION ON ARRAYS:
- reshape():used to change the shape of an array.
x=np.arange(1,10).reshape(3,3)o/p:
xarray([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
2.dot():used to perform dot product between two arrays.where columns of first array and rows of second array are same only.
x=np.arange(1,10).reshape(3,3)
y=np.arange(1,7).reshape(3,2)np.dot(x,y)array([[ 22, 28],
[ 49, 64],
[ 76, 100]])
3.logical_or():used to perform logical or between two or more arrays.
it will return an boolean array.
x=np.arange(1,10)
y=np.zeros((9,),dtype=int)np.logical_or(x,y)o/p:array([ True, True, True, True, True, True, True, True, True])
4.logical_and():used to perform logical andbetween two or more arrays.
it will return an boolean array.
x=np.arange(1,10)
y=np.zeros((9,),dtype=int)np.logical_and(x,y)o/p:array([False, False, False, False, False, False, False, False, False])
5.all():it will return true if all the elements are true.
np.all(np.array([1,2,3,0]))o/p:
False6.any():it will return true if at least one elements istrue.
np.any(np.array([1,2,3,0]))o/p:
True7.array_equal():checks between two arrays both are equal or not,it equal return true else false.
a=np.arange(1,13).reshape(4,3)
b=np.arange(1,13).reshape(3,4)np.array_equal(a,b)
o/p:
False8.ravel():it will turn a n-d array into 1-d array,but the ouput is view of the original array.
t=np.array([[1,2,3,4],[4,5,6,7]])
np.ravel(t)o/p:array([ 1,2,3,4,4,5,6,7])
9.flatten():it will turn a n-d array into 1-d array,but the ouput is copy of the original array.
t=np.array([[1,2,3,4],[4,5,6,7]])
a=t.flatten()
print(a)
o/p:array([ 1,2,3,4,4,5,6,7])7. copy(): Whenever we want to create a copy of an array it effects copied array and original array will not be effected.
8. view(): Whenever we want to create a viewof an array it effects both view array and original array .
REDUCING FUNCTIONS:
- np.sum(array,axis=0 or 1): return sum of the elements in the arrays if axis is not given.if axis is defined it will do sum along the axis.
a=array([[ 3, 4],[10, 6]])
np.sum(w)
o/p:23np.sum(w,axis=0)
o/p:[13,10]2.np.prod(array,axis=0 or 1): return product of the elements in the arrays if axis is not given.if axis is defined it will do product along the axis.
a=array([[ 3, 4],[10, 6]])
np.prod(w)
o/p:720np.sum(w,axis=0)
o/p:[30,24]3.np.nansum():used to do sum of elements when a nan element is present.it will convert all the nan to 0.
x=np.array([1,2,3,4,np.nan])
np.nansum(x)
o/p:10.04.np.nanprod():used to do product of elements when a nan element is present.it will convert all the nan to 1.
x=np.array([1,2,3,4,np.nan])
np.nansum(x)
o/p:24.05.np.cumsum(array,axis=0 or 1):it will return the cumulative sum of the array along the axis.it will always return 1-d array only.
x=np.array([1,2,3,4])
np.cumsum(x)
o/p:[1,3,6,10]6.np.cumprod(array,axis=0 or 1):it will return the cumulative product of the array along the axis.it will always return 1-d array only.
x=np.array([1,2,3,4])
np.cumsum(x)
o/p:[1,2,6,24]7.np.nancumsum(array,axis=0 or 1):it will return the cumulative sum of the array along the axis by converting nan to 0.it will always return 1-d array only.
x=array([[ 1., 2., 3.],[ 4., 5., 6.],[nan, 8., 9.]])
np.nancumsum(x)o/p:array([ 1., 3., 6., 10., 15., 21., 21., 29., 38.])8.np.nancumprod(array,axis=0 or 1):it will return the cumulative product of the array along the axis by converting nan to 1.it will always return 1-d array only.
x=array([[ 1., 2., 3.],[ 4., 5., 6.],[nan, 8., 9.]])
np.nancumprod(x)o/p:array([1.000e+00, 2.000e+00, 6.000e+00, 2.400e+01, 1.200e+02, 7.200e+02,
7.200e+02, 5.760e+03, 5.184e+04])9.np.round():by using this we may loose our precised data.it will round to the close values.
10.np.floor():round off to the previous whole number.
11.np.ceil():round off to the next whore number.
12.np.repeat():whenever we want to repeat elements n times in an array we use this function.
13.np.tile():when we want to repeat an entire array we usethis function.
14.np.unique():used to know unnique elements from an array.in unique function we can also use return_counts and return_index.
a=np.unique(a,return_counts=True,return_index=True)o/p:(array([ 1, 2, 3, 4, 6, 7, 8, 9, 10, 12, 13, 14]),
array([ 0, 1, 5, 6, 11, 2, 19, 3, 10, 7, 13, 15], dtype=int64),
array([3, 2, 1, 1, 3, 3, 1, 1, 1, 1, 2, 1], dtype=int64))
15. .T:used to transpose the matrix means toturn rows to columns.
16. np.max(array,axis=0 or 1):used to get the max value along axis.
17. np.min(array,axis=0 or 1):used to get the minimum value along axis.
18. np.nanmax(array,axis=0 or 1):used to get the max value along axis when nan is present.
19. np.nanmin(array,axis=0 or 1):used to get the min value along axis when nan is present.
20. np.argmax(array):used to get the index of max value.
21. np.argmin(array):used to get the index of min value.
22.Combining rows and columns:
concatenation: To combine Multiple array into single array then concatination technique is used.
2. hstack(): hstack() function is used to stack the sequence of input arrays horizontally (i.e. column wise) to make a single array.
3. vstack(): vstack() function is used to stack the sequence of input arrays vertically to make a single array.
ADVANCED FUNCTIONS:
- np.where(condition):used to get the index of elements where the condition satisfies.
- np.take():it will give values with the whare function results.
- concatenation(list of arrays):it will concatenate arrays of same shape .
- np.sort(array):used to sort the array.
- np.argsort():returns the array of sorted elements indexes.
- lexsort():used to sort an array based on multiple rows and columns.
- aply_along_axis(function name,axis,array):used to apply a function along axis on the given array.
x=np.array([[9,12,2,43],[4,2,6,7],[6,3,9,0]])
i=np.lexsort((x[:,0],))o/p:array([1, 2, 0], dtype=int64)8.np.vectorize():it will user defined functions and apply them element wise.
9.np.save(location\name,array ):used to save an array .
10.np.savez(location\name,array ):used to save multiple arrays.
11.np.load():used to access the arrays from pc.
12.np.genfromtxt(path,delimiter=”,”,skip_header=1):used to generate array from the text.
NUMPY BROAD CASTING:
used to do the sum of two arrays by broad casting.








