You are on page 1of 18

 NUMPY :- Numpy is a very

useful,powerful library of python for


fast mathematical computation on
array.To use this, we need to import it
as import numpy.

 Numpy array:- It is a collection of


elements of same type,numpy arrays
are also called ndarrays.
They are of two types:
1) 1D (one dimensional ) array
2) 2D (two dimensional ) array
 AXES :- It is the dimension of arrays.
Axes are always numbered 0 onward on
ndarrays.
axis=0
1 2 3 4 5 ( one row only)

axis=1
1 2 3 4 5
axis=0

6 7 8 9 10

( 2row, 5 columns)

 Rank :-The number of axes in an ndarray is its


rank.
Axis=0, means along the rows
Axis=1, means along the columns
 Shape :- The number of elements along each axis of it It is
in the form of tuple.
 Data type (dtype) :- It tells about the type of data stored
in the ndarray. By default the data type of ndarray is float.
 Itemsize :- It is the size of each element of an ndarray in
bytes.
•Difference between Numpy array and python lists.
Numpy array Python lists
1.After creating ndarray, its size 1.After creating a list, its size can be
cannot be changed. changed.
2. Array elements can be of same type 2.List elements can be of different type.
only.
3.It occupy less space. 3.It occupy more space.
4.It supports elements wise operations. 4.It does not support element wise
operations.
 Numpy data dypes :
Data type Size
Bool_ 1byte
Int8 1byte
Int16 2bytes
Int32 4bytes
int64 8bytes
Float16 2bytes
Float32 4bytes
Complex64 8bytes
String_ 1bytes per character

 Creating Numpy 1D array


1.Using array() function:-
Syntax: Numpy.array(<array object>,[<dtype>])
Ex:- import numpy as np
l=[1,3,6,5,7]
a=np.array(l)
print(a)
[1 3 6 5 7]
2.Using fromiter()function:-
Syntax :Numpy.fromiter(<array object>,<dtype>)
Ex:-import numpy as np
d={1:’A’,2:’B’,3:’c’}
a1=np.fromiter(d,dtype=np.int32)
print(a1)
[1 2 3]

3.Using arange ()function:-


Syntax: Numpy.arange([start]stop[step],[dtype])
Ex:- a2=np.arange(7)
print(a2)
[0 1 2 3 4 5 6]
4.Using linspace()function:-
Syntax: Numpy.linspace(<start>,<stop>,<no. of values>)
Ex: a4=np.linspace(2,4,5)
print(a4)
[2. 2.5.3 ]
 Creating Numpy 2D array:-
1.Using array()function:-
syntax:Numpy.array(<array object>,[<dtype>])
Ex:- L=[[1,3,5],[2,4,6]]
a=np.array(L)
print(a)
[[1 3 5]
[2 4 6]]

2.Using arange()function:-
syntax:Numpy.arange(<array object>,[<dtype>])
Ex:-ar=np.arange(10) ar1=ar.reshape(5,2)
ar ar1
array([0,1,2,3,4,5,6,7,8,9]) array([[0,1],
[2,3],
[4,5],
[6,7],
[8,9]])
 Creating empty using empty functions:-

syntax:numpy.empty(shape,[<dtype>])
Ex:-ar2=np.empty([3,2],dtype=np.int8)
ar2
array([[0,0],
[0,0],
[0,0]],dtype=int8)

 Creating array using zeros () function:-

syntax:Numpy.zeros(shape,[<dtype>])
Ex:-ar3=np.zeros([3,2],dtype=np.int8)
ar3
array ([[0,0],
[0,0],
[0,0]],dtype=int8)
 Creating array using ones () function:-

syntax:Numpy.ones(shape,[<dtype>]
Ex:-ar4=np.ones([3,2],dtype=np.int8)
ar4
array ([[1,1],
[1,1],
[1,1]],dtype=int8)

 np.eye(n):-It will create a n*n matrix keeping one in diagonal and


another in non-diagonal position.
Ex:-aa=np.eye(2,dtype=np.int8)
aa
array ([[1,0],
[0,1]],dtype=int8)

 np.full(<dimension>,<value>) - It will fill the created array with the


value Ex:-aa1=np.full((2,3),3)
aa1
array ([[3,3,3],
[3,3,3]])
 To get contiguous subsets of numpy arrays-
split(), hsplit() and vsplit() functions are used.

Ex:- 1) hsplit – ar=np.arange(9)


a=ar.reshape(3,3)
a
array([[0,1,2],
[3,4,5],
[6,7,8]])

np.hsplit(a,3)
[array([[0],
[3],
[6]]), array ([[1];
[4],
[7]]), array ([[2];
[5],
[8]])]
2) vsplit- np.vsplit(a,3)
[array ([[0,1,2]]),array([[3,4,5]]),array ([[6,7,8]])]

3) Split ()- m=[11,12,13,14,15,16]


np.split(m[2,5])
array([11,12]),array([13,14,15]),array([16])]
It splits the array as follows :-
2 5
[0:2] [2:5] [5: ] index value
[11,12] [13,14,15] [16]
Split ()-For 2D array.
array([[0,1,2],
[3,4,5],
[6,7,8]])
Column wise split can be done as:-
np.split(a,[2,5],axis=1)
[array([[0,1],
[3,4],
[6,7]]),array([[2],
[5],
[8],array([],shape=(3,0),dtype=int32)]
 To get non-contiguous ubsets of numpy arrays-
extract() & compress() functions are used.
Ex:-1) Extract():-
syntax:Numpy.extract(<condition>;<array>)
cond=np.mod(a,2)==0
cond
array([[true,false,true],
[false,true,false],
[true,false,true]])
res=np.extract(cond,a)
res
array([0,2,4,6,8])
2) Compress():-
syntax:Numpy.compress(<condition>,<array>[axis])
m=[11,12,13,14,15,16]
cond=np.mod(m,2)==0
cond
array([false,true,false,true,false,true])
np.compress(cond,m,axis=0)
array[12,14,16]
 Arithmetic operations on array :
m=[11,12,13,14,15,16]
np.add(m,2)
array([21,22,23,24,25,26])
np.mod(m,2)
Array([1,0,1,0,1,0],dtype=int32)
m*2
[11,12,13,1,4,15,16,11,12,13,14,15,16]
 Applications of numpy array:

1.Covariance:-The mean value of the product of the deviations of


two from their respective mean.
The formula of covariance is:
Ex:a=[1,2,3]
b=[10,20,30]
res=np.cov(a,b)
res
array([[1., 10.],
[10., 100]])
2.Correlation:It is used to measure the strength of the relationship
between two variables.
Ex:-a=[1,2,3]
b=[10,20,30]
res1=np.corrcoef(a,b)
res1
array([[1.,1.],
[1.,1.]])

3.Linear regression:It is a method used to find a relationship between


a dependent variable and a set independent variables.
syntax:Numpy.polyfit(array1,array2 deg) deg=degree
Ex:-a=[1,2,3]
b=[4,5,6]
res2=np.polyfit(a,b,1)
res2
array([1.,3.])
 Joining numpy array:To join or concatenate numpy
arrays are:-
1.hstack() and vstack()
Ex:-L1=[1,3,5]
L2=[2,4,6]
res.np.hstack((L1,L2))
res
array ([1,3,5,2,4,6])
res1=np.vstack((L1,L2))
res1
array([[1,3,5],
[2,4,6]])
2.Concatenate()
Ex:-a=np.array([[1,2],[10,20])
b=np.array([[11,22],[21,31]])
res=np.concatenate((a,b),axis=0)
array([[1,2],
[10,20],
[11,22],
[21,31]])
 Accessing elements of Numpy arrays:-

For 1D array:-<array>[<index>]
Ex:-ar=np.array([10,20,30,40,50,60,70])
ar
array([10,20,30,40,50,60,70])
ar[4]
50

For 2D array:-<array>[<rowindex>[<colindex>]
Ex:-array([[1,2,3],
[10,20,30],
[100,200,300]])
ar1[1,1]
20
ar1[2,0]
100
 Array slices:-

A)1D array slices:-


1.A[m:n]-ir extract slice from m to n-1.
Ex:-ar=np.array9[10,20,30,40,50])
ar=[1:4]
array([20,30,40])
2.A[ :n]-ir extract slice from 0 to n-1.
3.A[m: ]-it extract slice from m to end.
Ex:-ar[ :3]
array([10,20,30])
ar[2: ]
array([30,40,50])
4.A[m:-2]-it extract slice from m to end -2.
Ex:-ar[2:-2]
array([30])
5.A[m:n:o]-it extract slice from m to n-1 picking every
other element.
Ex:-ar[1:4:2]
array([1,3])
B) 2D array slices:-
A[<start>:<stop>:<step>,<start>:<stop>:step]
Ex:-ar1
array([[1,2,3],,
[10,20,30],
[100,200,300]])
ar1[1:3,2:4]
array([[30],
[300]])

You might also like