0% found this document useful (0 votes)
6 views49 pages

Numpy Project Part-1

The document provides a comprehensive overview of NumPy arrays, including their creation, types (1D, 2D, 3D), and various functions such as arange, reshape, ones, zeros, linspace, and identity. It also covers array attributes like ndim, shape, size, item size, and dtype, as well as operations like scalar addition and subtraction. Overall, it serves as a tutorial on how to effectively utilize NumPy for numerical computations.

Uploaded by

harshithr977
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views49 pages

Numpy Project Part-1

The document provides a comprehensive overview of NumPy arrays, including their creation, types (1D, 2D, 3D), and various functions such as arange, reshape, ones, zeros, linspace, and identity. It also covers array attributes like ndim, shape, size, item size, and dtype, as well as operations like scalar addition and subtraction. Overall, it serves as a tutorial on how to effectively utilize NumPy for numerical computations.

Uploaded by

harshithr977
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

In [620… import numpy as np

In [621… np.array([2,4,56,100,35,1]) # it was an 1D array

Out[621… array([ 2, 4, 56, 100, 35, 1])

In [622… a =np.array([2,4,56,100,35,1])

In [623… print(a)

[ 2 4 56 100 35 1]

In [624… type(a)

Out[624… numpy.ndarray

In [625… b =np.array([[21,22,23,2],[24,25,26,24]]) #it was the 2D array (OR) 2*2 matrix

In [626… print(b)

[[21 22 23 2]
[24 25 26 24]]

interduced the dtype


In [628… np.array([[10,11,12,13],[13,14,15,16],[16,17,18,19]]) # it was the 3D array(OR)3

Out[628… array([[10, 11, 12, 13],


[13, 14, 15, 16],
[16, 17, 18, 19]])

In [629… np.array([11,22,23],dtype=float) # hear we give the new function it was change t

Out[629… array([11., 22., 23.])

In [630… np.array([11,22,33,44] , dtype=bool ) #hear we change the data type int to bool

Out[630… array([ True, True, True, True])

In [631… np.array([11,22,33,44,55,66] , dtype=complex )

Out[631… array([11.+0.j, 22.+0.j, 33.+0.j, 44.+0.j, 55.+0.j, 66.+0.j])

NUMPY ARRAY VS PYTHON SEQUENCES

arange
In [634… np.arange(1,30) # hear we use arange function it was print the 1-30 (or) many
Out[634… array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29])

In [635… np.arange(1,30,2) #hear we give the range and if the number is divided by2 it wa

Out[635… array([ 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29])

reshape
In [637… np.arange(1,21).reshape(5,4) #hear we give thereshape of the values by using re

Out[637… array([[ 1, 2, 3, 4],


[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
[13, 14, 15, 16],
[17, 18, 19, 20]])

In [638… np.arange(1,31).reshape(6,5) # 6-rows,5-columns

Out[638… array([[ 1, 2, 3, 4, 5],


[ 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30]])

In [639… np.arange(1,101).reshape(10,10)

Out[639… array([[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],


[ 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
[ 21, 22, 23, 24, 25, 26, 27, 28, 29, 30],
[ 31, 32, 33, 34, 35, 36, 37, 38, 39, 40],
[ 41, 42, 43, 44, 45, 46, 47, 48, 49, 50],
[ 51, 52, 53, 54, 55, 56, 57, 58, 59, 60],
[ 61, 62, 63, 64, 65, 66, 67, 68, 69, 70],
[ 71, 72, 73, 74, 75, 76, 77, 78, 79, 80],
[ 81, 82, 83, 84, 85, 86, 87, 88, 89, 90],
[ 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]])

ones & zeros

np.ones and np.zeros


In [642… # hear we use only ones and zeros of the function only numpy doesnot allow the o

In [643… np.ones((5,5)) #we have to menction insidee the tuple

Out[643… array([[1., 1., 1., 1., 1.],


[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])
In [644… np.zeros((6,5))

Out[644… 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., 0., 0., 0.]])

In [645… # hear we use the rendom function it was display the random numbers in to the ma

In [646… np.random.random((6,6))

Out[646… array([[0.05338425, 0.38564023, 0.1031655 , 0.73028241, 0.94053223,


0.15346841],
[0.04227907, 0.9805138 , 0.02907021, 0.11805598, 0.45687816,
0.71712976],
[0.15508968, 0.99453658, 0.80722524, 0.86364956, 0.69035752,
0.58351873],
[0.12983914, 0.59431144, 0.29920295, 0.4525242 , 0.91766997,
0.81795688],
[0.59125696, 0.38400931, 0.77514815, 0.07417498, 0.40368043,
0.2420514 ],
[0.45866433, 0.21174038, 0.25002155, 0.46440131, 0.31779937,
0.56404528]])

LINSPACE
# it is also called as linearly space , linearly separatable,in a given range at equal distance it creates points

In [648… np.linspace(-10,10,10) # hear we the three numbers inthe function [(lower range

Out[648… array([-10. , -7.77777778, -5.55555556, -3.33333333,


-1.11111111, 1.11111111, 3.33333333, 5.55555556,
7.77777778, 10. ])

In [649… np.linspace(-2,10,8)

Out[649… array([-2. , -0.28571429, 1.42857143, 3.14285714, 4.85714286,


6.57142857, 8.28571429, 10. ])

identity
identity matrix is that diagonal items will be ones and everithing is zeros

In [651… # creating identity matrix


np.identity(3) # here we give the 3 is declared 3*3 matrix

Out[651… array([[1., 0., 0.],


[0., 1., 0.],
[0., 0., 1.]])

In [652… np.identity(6) # here we give the 6 value is diclared 6*6 matrix


Out[652… array([[1., 0., 0., 0., 0., 0.],
[0., 1., 0., 0., 0., 0.],
[0., 0., 1., 0., 0., 0.],
[0., 0., 0., 1., 0., 0.],
[0., 0., 0., 0., 1., 0.],
[0., 0., 0., 0., 0., 1.]])

ARRAYN ATTRIBUTES
In [654… a1=np.arange(10) # 1D
a1

Out[654… array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [655… a2 = np.arange(12,dtype=float).reshape(3,4) # here we give the {12- is the rang


a2

Out[655… array([[ 0., 1., 2., 3.],


[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.]])

In [656… a3 = np.arange(8).reshape(2,2,2) # here we give the {8 - is the range of the n


a3

Out[656… array([[[0, 1],


[2, 3]],

[[4, 5],
[6, 7]]])

ndim
to findout given arrays number of dimensions

In [658… a1.ndim # hear we use ndim is the display the number of arrays the the given d

Out[658… 1

In [659… a2.ndim

Out[659… 2

In [660… a3.ndim

Out[660… 3

SHAPE
gives each item consist of no.of rows and no of columns

In [662… a1.shape # 1D array has 10 items

Out[662… (10,)
In [663… a2.shape # 3 rows and 4 columns

Out[663… (3, 4)

In [664… a3.shape # first ,2 says it consists of 2D arrays .2,2 gives no.of rowsand no.of

Out[664… (2, 2, 2)

Size
In [666… a3

Out[666… array([[[0, 1],


[2, 3]],

[[4, 5],
[6, 7]]])

In [667… a3.size # it gives the no.of items in the array

Out[667… 8

In [668… a2

Out[668… array([[ 0., 1., 2., 3.],


[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.]])

In [669… a2.size

Out[669… 12

item size
memory occupied by the item

In [671… a1

Out[671… array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [672… a1.itemsize

Out[672… 4

In [673… a2.itemsize

Out[673… 8

In [674… a3.item

Out[674… <function ndarray.item>

dtype
gives data type of the item

In [676… print(a1.dtype)
print(a2.dtype)
print(a3.dtype)

int32
float64
int32

changing data type


In [678… x = np.array([11,222,3.3])
x

Out[678… array([ 11. , 222. , 3.3])

In [679… x.astype(int)

Out[679… array([ 11, 222, 3])

Array operations
In [681… z1 = np.arange(12).reshape(3,4)
z2 = np.arange(12,24).reshape(3,4)

In [682… z1

Out[682… array([[ 0, 1, 2, 3],


[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])

In [683… z2

Out[683… array([[12, 13, 14, 15],


[16, 17, 18, 19],
[20, 21, 22, 23]])

scalar operation
scalar operation on numpy arrays include performing addition or subration,or multipication on each element of a numpy array

In [685… z1+2 # it was add the two of the array

Out[685… array([[ 2, 3, 4, 5],


[ 6, 7, 8, 9],
[10, 11, 12, 13]])

In [686… z1-2

Out[686… array([[-2, -1, 0, 1],


[ 2, 3, 4, 5],
[ 6, 7, 8, 9]])
In [687… z1*2

Out[687… array([[ 0, 2, 4, 6],


[ 8, 10, 12, 14],
[16, 18, 20, 22]])

In [688… z1**2 # the ** is perform the operation of square

Out[688… array([[ 0, 1, 4, 9],


[ 16, 25, 36, 49],
[ 64, 81, 100, 121]])

In [689… z1%2 # the % is tahen only reminder value

Out[689… array([[0, 1, 0, 1],


[0, 1, 0, 1],
[0, 1, 0, 1]], dtype=int32)

relational operator
In [691… z2

Out[691… array([[12, 13, 14, 15],


[16, 17, 18, 19],
[20, 21, 22, 23]])

In [692… z2>2

Out[692… array([[ True, True, True, True],


[ True, True, True, True],
[ True, True, True, True]])

In [693… z2<20

Out[693… array([[ True, True, True, True],


[ True, True, True, True],
[False, False, False, False]])

Vector operation
In [695… z1

Out[695… array([[ 0, 1, 2, 3],


[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])

In [696… z2

Out[696… array([[12, 13, 14, 15],


[16, 17, 18, 19],
[20, 21, 22, 23]])

In [697… z1+z2
Out[697… array([[12, 14, 16, 18],
[20, 22, 24, 26],
[28, 30, 32, 34]])

In [698… z1*z2

Out[698… array([[ 0, 13, 28, 45],


[ 64, 85, 108, 133],
[160, 189, 220, 253]])

In [699… z1-z2

Out[699… array([[-12, -12, -12, -12],


[-12, -12, -12, -12],
[-12, -12, -12, -12]])

In [700… z1/z2

Out[700… array([[0. , 0.07692308, 0.14285714, 0.2 ],


[0.25 , 0.29411765, 0.33333333, 0.36842105],
[0.4 , 0.42857143, 0.45454545, 0.47826087]])

array functions
In [702… k1 = np.random.random((3,3)) # hear first 3 is column ,second 3 is row
k1 = np.round(k1*100) # hear k1 is multiplied by 100 with range 100
k1

Out[702… array([[28., 33., 56.],


[48., 78., 65.],
[43., 13., 54.]])

In [703… np.max(k1) # hear we use max() gives the maximum value of k1

Out[703… 78.0

In [704… np.min(k1) # hear we use min() gives the minimum value of k1

Out[704… 13.0

In [705… np.sum(k1)

Out[705… 418.0

In [706… np.prod(k1)

Out[706… 380114785290240.0

in numpy
0=columns , 1=rows
In [709… #if we want maximum of every row
np.max(k1, axis = 1)

Out[709… array([56., 78., 54.])

In [710… #if we want maximum of every column


np.max(k1,axis = 0)

Out[710… array([48., 78., 65.])

In [711… #product of every column


np.prod(k1,axis=0)

Out[711… array([ 57792., 33462., 196560.])

statistics releated functions

In [713… #mean
k1

Out[713… array([[28., 33., 56.],


[48., 78., 65.],
[43., 13., 54.]])

In [714… np.mean(k1)

Out[714… 46.44444444444444

In [715… # mean of every column


k1.mean(axis=0)

Out[715… array([39.66666667, 41.33333333, 58.33333333])

In [716… # meadion
np.median(k1)

Out[716… 48.0

In [717… np.median(k1,axis = 1)

Out[717… array([33., 65., 43.])

In [718… np.std(k1)

Out[718… 18.685176008513697

In [719… np.std(k1,axis = 0)

Out[719… array([ 8.49836586, 27.18251072, 4.78423336])

In [720… # variance
np.var(k1)

Out[720… 349.1358024691358

Trigometry functions
In [722… np.sin(k1) # it was find the sin of the given array

Out[722… array([[ 0.27090579, 0.99991186, -0.521551 ],


[-0.76825466, 0.51397846, 0.82682868],
[-0.83177474, 0.42016704, -0.55878905]])

In [723… np.cos(k1)

Out[723… array([[-0.96260587, -0.01327675, 0.85322011],


[-0.64014434, -0.85780309, -0.56245385],
[ 0.5551133 , 0.90744678, -0.82930983]])

In [724… np.tan(k1)

Out[724… array([[ -0.2814296 , -75.3130148 , -0.61127369],


[ 1.20012724, -0.59918 , -1.47003826],
[ -1.49838734, 0.46302113, 0.6738001 ]])

dot product
The numpy module of python provide a function to perform the dot product of two arrays

In [726… s2 = np.arange(12).reshape(3,4)
s3 = np.arange(12,24).reshape(4,3)

In [727… s2

Out[727… array([[ 0, 1, 2, 3],


[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])

In [728… s3

Out[728… array([[12, 13, 14],


[15, 16, 17],
[18, 19, 20],
[21, 22, 23]])

In [729… np.dot(s2,s3) # dot product of s2,s3

Out[729… array([[114, 120, 126],


[378, 400, 422],
[642, 680, 718]])

Log and Exponents

In [731… np.exp(s2)

Out[731… array([[1.00000000e+00, 2.71828183e+00, 7.38905610e+00, 2.00855369e+01],


[5.45981500e+01, 1.48413159e+02, 4.03428793e+02, 1.09663316e+03],
[2.98095799e+03, 8.10308393e+03, 2.20264658e+04, 5.98741417e+04]])

round/floor/ceil

1.round

In [734… arr=np.array([1.5,2.5,3.5,4.5,5.5]) # it was print rounded of the float to the d


roundarr=np.round(arr)
print(roundarr)

[2. 2. 4. 4. 6.]

In [735… arr = np.array([1.234,2.345,3.456,4.567,5.678]) # it was print . after rwo deci


roundarr=np.round(arr,decimals=2)
print(roundarr)

[1.23 2.35 3.46 4.57 5.68]

In [736… np.round(np.random.random((2,3))*100)

Out[736… array([[34., 30., 33.],


[41., 29., 75.]])

2.floor

In [738… arr=np.array([1.2,2.3,3.4,4,5.5])
floorarr=np.floor(arr)
print(floorarr)

[1. 2. 3. 4. 5.]

In [739… np.floor(np.random.random((2,3))*100)

Out[739… array([[60., 2., 28.],


[37., 2., 36.]])

3.ceil

In [741… arr=np.array([1.2,2.3,2.4,5.5,5.6])
ceiledarr=np.ceil(arr)
print(ceiledarr)

[2. 3. 3. 6. 6.]

In [742… np.ceil(np.random.random((2,3))*100)

Out[742… array([[66., 28., 80.],


[85., 57., 75.]])

indexing slicing
In [744… p1=np.arange(10)
p2=np.arange(12).reshape(3,4)
p3=np.arange(8).reshape(2,2,2)

In [745… p1

Out[745… array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [746… p2

Out[746… array([[ 0, 1, 2, 3],


[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])

In [747… p3
Out[747… array([[[0, 1],
[2, 3]],

[[4, 5],
[6, 7]]])

idexing on 1D array

In [749… p1

Out[749… array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [750… p1[-1]

Out[750… 9

In [751… p1[0]

Out[751… 0

indexing on 2D array

In [753… p2

Out[753… array([[ 0, 1, 2, 3],


[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])

In [754… p2[1,2] # hear 1= is represent the first row ,2= is represnt second column

Out[754… 6

In [755… p2[2,3]

Out[755… 11

In [756… p2[1,0]

Out[756… 4

indexing on 3D(Tensors)

In [758… p3

Out[758… array([[[0, 1],


[2, 3]],

[[4, 5],
[6, 7]]])

In [759… p3[1,0,1] #hear first 1 is second matrix ,second 0 is row ,thired 1 is column

Out[759… 5

EXPLANATION :Here 3D is consists of 2 ,2D array , so Firstly we take 1 because our desired is 5 is in second matrix which is
1 .and 1 row so 0 and second column so 1
In [760… p3[0,1,0]

Out[760… 2

EXPLANATION :Here firstly we take 0 because our desired is 2, is in first matrix which is 0 . and 2 row so 1 and first column
so 0

In [761… p3[0,0,0]

Out[761… 0

Here first we take 0 because our desired is 0, is in first matrix which is 0 . and 1 row so 0 and first column so 0

In [762… p3[1,1,0]

Out[762… 6

EXPLANATION : Here first we take because our desired is 6, is in second matrix which is 1 . and second row so 1 and first
column so 0

Slicing
Fetching Multiple items

Slicing on 1D

In [765… p1

Out[765… array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [766… p1[2:5]

Out[766… array([2, 3, 4])

EXPLANATION :Here First we take , whatever we need first item ,2 and up last(4) + 1 which 5 .because last element is not
included

In [767… p1[2:5:2]

Out[767… array([2, 4])

Slicing on 2D
In [769… p2

Out[769… array([[ 0, 1, 2, 3],


[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])

In [770… p2[0, :]

Out[770… array([0, 1, 2, 3])

EXPLANATION :Here 0 represents first row and (:) represnts Total column

In [771… p2[:,2]

Out[771… array([ 2, 6, 10])

EXPLANATION :Here we want all rows so (:) , and we want 3rd column so 2
In [772… p2

Out[772… array([[ 0, 1, 2, 3],


[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])

In [773… p2[1:3]

Out[773… array([[ 4, 5, 6, 7],


[ 8, 9, 10, 11]])

In [774… p2[1:3,1:3]

Out[774… array([[ 5, 6],


[ 9, 10]])
EXPLANATION :Here first [1:3] we slice 2 second row is to third row is not existed which is 2 and Secondly , we take [1:3]
which is same as first:we slice 2 second row is to third row is not included which is 3

In [775… p2

Out[775… array([[ 0, 1, 2, 3],


[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])

In [776… p2[::2,::3]

Out[776… array([[ 0, 3],


[ 8, 11]])
EXPLANATION : Here we take (:) because we want all rows , second(:2) for alternate value, and (:) for all columns and (:3)
jump for two steps

In [777… p2

Out[777… array([[ 0, 1, 2, 3],


[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])

In [778… p2[::2] #for rows

Out[778… array([[ 0, 1, 2, 3],


[ 8, 9, 10, 11]])

In [779… p2[::2,1::2] # columns

Out[779… array([[ 1, 3],


[ 9, 11]])
EXPLANATION : Here we take (:) because we want all rows , second(:2) for alternate value, and (1) for we want from second
column and (:2) jump for two steps and ignore middle one

In [780… p2

Out[780… array([[ 0, 1, 2, 3],


[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])

In [781… p2[1]

Out[781… array([4, 5, 6, 7])

In [782… p2[1,::3]
Out[782… array([4, 7])

EXPLANATION : Here we take (1) because we want second row , second(:) for total column, (:3) jump for two steps and
ignore middle ones

In [783… p2

Out[783… array([[ 0, 1, 2, 3],


[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])

In [784… p2[0:2]

Out[784… array([[0, 1, 2, 3],


[4, 5, 6, 7]])

In [785… p2[0:2,1:]

Out[785… array([[1, 2, 3],


[5, 6, 7]])

In [786… p2

Out[786… array([[ 0, 1, 2, 3],


[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])

In [787… p2[0:2]

Out[787… array([[0, 1, 2, 3],


[4, 5, 6, 7]])

In [788… p2[0:2,1::2]

Out[788… array([[1, 3],


[5, 7]])
EXPLANATION : 0:2 selects the rows from index 0 (inclusive) to index 2 (exclusive), which means it will select the first and
second rows of the array. , is used to separate row and column selections. 1::2 selects the columns starting from index 1 and
selects every second column. So it will select the second and fourth columns of the array.

Slicing in 3D

In [790… p3 = np.arange(27).reshape(3,3,3)
p3

Out[790… array([[[ 0, 1, 2],


[ 3, 4, 5],
[ 6, 7, 8]],

[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],

[[18, 19, 20],


[21, 22, 23],
[24, 25, 26]]])

In [791… p3[1]
Out[791… array([[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]])

In [792… p3[::2]

Out[792… array([[[ 0, 1, 2],


[ 3, 4, 5],
[ 6, 7, 8]],

[[18, 19, 20],


[21, 22, 23],
[24, 25, 26]]])
EXPLANATION : Along the first axis, (::2) selects every second element. This means it will select the subarrays at indices 0
and 2

In [793… p3

Out[793… array([[[ 0, 1, 2],


[ 3, 4, 5],
[ 6, 7, 8]],

[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],

[[18, 19, 20],


[21, 22, 23],
[24, 25, 26]]])

In [794… p3[0]

Out[794… array([[0, 1, 2],


[3, 4, 5],
[6, 7, 8]])

In [795… p3[0,1,:]

Out[795… array([3, 4, 5])

EXPLANATION : 0 represnts first matrix , 1 represents second row , (:) means total

In [796… p3

Out[796… array([[[ 0, 1, 2],


[ 3, 4, 5],
[ 6, 7, 8]],

[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],

[[18, 19, 20],


[21, 22, 23],
[24, 25, 26]]])

In [797… p3[1]
Out[797… array([[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]])

In [798… p3[1]

Out[798… array([[ 9, 10, 11],


[12, 13, 14],
[15, 16, 17]])

In [799… p3[1,:,1]

Out[799… array([10, 13, 16])

EXPLANATION : 1 respresnts middle column , (:) all columns , 1 represnts middle column

In [800… p3

Out[800… array([[[ 0, 1, 2],


[ 3, 4, 5],
[ 6, 7, 8]],

[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],

[[18, 19, 20],


[21, 22, 23],
[24, 25, 26]]])

In [801… p3[1,:,1]

Out[801… array([10, 13, 16])

In [802… p3

Out[802… array([[[ 0, 1, 2],


[ 3, 4, 5],
[ 6, 7, 8]],

[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],

[[18, 19, 20],


[21, 22, 23],
[24, 25, 26]]])

In [803… p3[2]

Out[803… array([[18, 19, 20],


[21, 22, 23],
[24, 25, 26]])

In [804… p3[2,1:] # last tow columns

Out[804… array([[21, 22, 23],


[24, 25, 26]])

In [805… p3[2,1:,1:] # last two columns


Out[805… array([[22, 23],
[25, 26]])
EXPLANATION : Here we go through 3 stages , where 2 for last array , and (1:) from second row to total rows , and (1:) is for
second column to total columns

In [806… p3

Out[806… array([[[ 0, 1, 2],


[ 3, 4, 5],
[ 6, 7, 8]],

[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],

[[18, 19, 20],


[21, 22, 23],
[24, 25, 26]]])

In [807… p3[0::2]

Out[807… array([[[ 0, 1, 2],


[ 3, 4, 5],
[ 6, 7, 8]],

[[18, 19, 20],


[21, 22, 23],
[24, 25, 26]]])

In [808… p3[0::2,0]

Out[808… array([[ 0, 1, 2],


[18, 19, 20]])

In [809… p3[0::2,0,::2]

Out[809… array([[ 0, 2],


[18, 20]])
EXPLANATION : Here we take (0::2) first adn last column , so we did jump using this, and we took (0) for first row , and we
(::2) ignored middle column

Iterating
In [811… p1

Out[811… array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [812… for i in p1:


print(i)
0
1
2
3
4
5
6
7
8
9

In [813… p2

Out[813… array([[ 0, 1, 2, 3],


[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])

In [814… for i in p2:


print(i)

[0 1 2 3]
[4 5 6 7]
[ 8 9 10 11]

In [815… p3

Out[815… array([[[ 0, 1, 2],


[ 3, 4, 5],
[ 6, 7, 8]],

[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],

[[18, 19, 20],


[21, 22, 23],
[24, 25, 26]]])

In [816… for i in p3:


print(i)

[[0 1 2]
[3 4 5]
[6 7 8]]
[[ 9 10 11]
[12 13 14]
[15 16 17]]
[[18 19 20]
[21 22 23]
[24 25 26]]

In [817… for i in np.nditer(p3):


print(i)
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

Reshaping
In [819… p2

Out[819… array([[ 0, 1, 2, 3],


[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])

In [820… np.transpose(p2)

Out[820… array([[ 0, 4, 8],


[ 1, 5, 9],
[ 2, 6, 10],
[ 3, 7, 11]])

In [821… p2.T ## it was theanother way

Out[821… array([[ 0, 4, 8],


[ 1, 5, 9],
[ 2, 6, 10],
[ 3, 7, 11]])

In [822… p3
Out[822… array([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],

[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],

[[18, 19, 20],


[21, 22, 23],
[24, 25, 26]]])

In [823… p3.T

Out[823… array([[[ 0, 9, 18],


[ 3, 12, 21],
[ 6, 15, 24]],

[[ 1, 10, 19],
[ 4, 13, 22],
[ 7, 16, 25]],

[[ 2, 11, 20],
[ 5, 14, 23],
[ 8, 17, 26]]])

Ravel
Converting any dimensions to 1D

In [825… p2

Out[825… array([[ 0, 1, 2, 3],


[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])

In [826… p2.ravel()

Out[826… array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])

In [827… p3

Out[827… array([[[ 0, 1, 2],


[ 3, 4, 5],
[ 6, 7, 8]],

[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],

[[18, 19, 20],


[21, 22, 23],
[24, 25, 26]]])

In [828… p3.ravel()

Out[828… array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,


17, 18, 19, 20, 21, 22, 23, 24, 25, 26])
Stacking
Stacking is the concept of joining arrays in NumPy. Arrays having the same dimensions can be stacked

In [830… w1=np.arange(12).reshape(3,4)
w2=np.arange(12,24).reshape(3,4)

In [831… w1

Out[831… array([[ 0, 1, 2, 3],


[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])

In [832… w2

Out[832… array([[12, 13, 14, 15],


[16, 17, 18, 19],
[20, 21, 22, 23]])

In [833… np.hstack((w1,w2))

Out[833… array([[ 0, 1, 2, 3, 12, 13, 14, 15],


[ 4, 5, 6, 7, 16, 17, 18, 19],
[ 8, 9, 10, 11, 20, 21, 22, 23]])

In [834… w1

Out[834… array([[ 0, 1, 2, 3],


[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])

In [835… w2

Out[835… array([[12, 13, 14, 15],


[16, 17, 18, 19],
[20, 21, 22, 23]])

In [836… np.vstack((w1,w2))

Out[836… array([[ 0, 1, 2, 3],


[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]])

Splitting
its opposite of stacking

In [838… w1

Out[838… array([[ 0, 1, 2, 3],


[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])

In [839… np.hsplit(w1,2)
Out[839… [array([[0, 1],
[4, 5],
[8, 9]]),
array([[ 2, 3],
[ 6, 7],
[10, 11]])]

In [840… np.hsplit(w1,4)

Out[840… [array([[0],
[4],
[8]]),
array([[1],
[5],
[9]]),
array([[ 2],
[ 6],
[10]]),
array([[ 3],
[ 7],
[11]])]

In [841… w2

Out[841… array([[12, 13, 14, 15],


[16, 17, 18, 19],
[20, 21, 22, 23]])

In [842… np.vsplit(w2,3)

Out[842… [array([[12, 13, 14, 15]]),


array([[16, 17, 18, 19]]),
array([[20, 21, 22, 23]])]

Speed of List Vs Numpy


List
In [845… a = [i for i in range(10000000)]
b = [i for i in range(10000000,20000000)]
c = []
import time
start = time.time()
for i in range(len(a)):
c.append(a[i]+b[i])
print(time.time()-start)

4.160593271255493

Numpy
In [847… import numpy as np
a=np.arange(10000000)
b=np.arange(10000000,20000000)
start =time.time()
c=a+b
print(time.time()-start)

0.20647811889648438

In [848… 2.8750417232513428 / 0.14256787300109863

Out[848… 20.166126229779607

Memory Used for List Vs Numpy

List

In [851… P = [i for i in range(1000000)]


import sys
sys.getsizeof(P)

Out[851… 8448728

Numpy

In [853… R = np.arange(1000000)
sys.getsizeof(R)

Out[853… 4000112

In [854… R = np.arange(100000,dtype = np.int16)


sys.getsizeof(R)

Out[854… 200112

Advance Indexing and slicing


In [856… w = np.arange(12).reshape(4,3)
w

Out[856… array([[ 0, 1, 2],


[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])

In [857… w[1,2]

Out[857… 5

In [858… w[1:3]

Out[858… array([[3, 4, 5],


[6, 7, 8]])

In [859… w[1:3,1:3]

Out[859… array([[4, 5],


[7, 8]])
Fancy Indexing
Fancy indexing allows you to select or modify specific elements based on complex conditions or combinations of indices. It
provides a powerful way to manipulate array data in NumPy.

In [861… w

Out[861… array([[ 0, 1, 2],


[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])

In [862… w[[0,2,3]]

Out[862… array([[ 0, 1, 2],


[ 6, 7, 8],
[ 9, 10, 11]])

In [863… z = np.arange(24).reshape(6,4)
z

Out[863… array([[ 0, 1, 2, 3],


[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]])

In [864… z[[0,2,3,5]]

Out[864… array([[ 0, 1, 2, 3],


[ 8, 9, 10, 11],
[12, 13, 14, 15],
[20, 21, 22, 23]])

In [865… z[:,[0,2,3]]

Out[865… array([[ 0, 2, 3],


[ 4, 6, 7],
[ 8, 10, 11],
[12, 14, 15],
[16, 18, 19],
[20, 22, 23]])

Boolean indexing
It allows you to select elements from an array based on a Boolean condition. This allows you to extract only the elements of an
array that meet a certain condition, making it easy to perform operations on specific subsets of data.

In [867… G= np.random.randint(1,100,24).reshape(6,4)
G

Out[867… array([[87, 8, 65, 43],


[ 5, 80, 94, 9],
[17, 6, 26, 80],
[33, 79, 44, 48],
[89, 54, 56, 4],
[98, 58, 9, 21]])

In [868… G>80
Out[868… array([[ True, False, False, False],
[False, False, True, False],
[False, False, False, False],
[False, False, False, False],
[ True, False, False, False],
[ True, False, False, False]])

In [869… G[G>50]

Out[869… array([87, 65, 80, 94, 80, 79, 89, 54, 56, 98, 58])

In [870… G % 2==0

Out[870… array([[False, True, False, False],


[False, True, True, False],
[False, True, True, True],
[False, False, True, True],
[False, True, True, True],
[ True, True, False, False]])

In [871… G[G %2 ==0]

Out[871… array([ 8, 80, 94, 6, 26, 80, 44, 48, 54, 56, 4, 98, 58])

In [872… (G>50)&(G%2==0)

Out[872… array([[False, False, False, False],


[False, True, True, False],
[False, False, False, True],
[False, False, False, False],
[False, True, True, False],
[ True, True, False, False]])

In [873… G[(G>50)&(G%2==0)]

Out[873… array([80, 94, 80, 54, 56, 98, 58])

In [874… G%7==0

Out[874… array([[False, False, False, False],


[False, False, False, False],
[False, False, False, False],
[False, False, False, False],
[False, False, True, False],
[ True, False, False, True]])

In [875… G[~(G%7==0)] #(~) = not

Out[875… array([87, 8, 65, 43, 5, 80, 94, 9, 17, 6, 26, 80, 33, 79, 44, 48, 89,
54, 4, 58, 9])

Broadcasting
Used in VectorizationThe term broadcasting describes how NumPy treats arrays with different shapes during arithmetic
operations. The smaller array is “broadcast” across the larger array so that they have compatible shapes.

In [877… a = np.arange(6).reshape(2,3)
b=np.arange(6,12).reshape(2,3)
print(a)
print(b)
print(a+b)

[[0 1 2]
[3 4 5]]
[[ 6 7 8]
[ 9 10 11]]
[[ 6 8 10]
[12 14 16]]

In [878… a = np.arange(6).reshape(2,3)
b = np.arange(3).reshape(1,3)
print(a)
print(b)
print(a+b)

[[0 1 2]
[3 4 5]]
[[0 1 2]]
[[0 2 4]
[3 5 7]]

Broadcasting Rules

1.Make the two arrays have the same numbers of dimensions


If the numbers of dimensions of the two arrays are different, add new dimensions with size 1 to the head of the array with the
smaller dimension. ex : (3,2) = 2D , (3) =1D ---> Convert into (1,3) (3,3,3) = 3D ,(3) = 1D ---> Convert into (1,1,3)

2.Make each dimension of the two arrays the same size


If the sizes of each dimension of the two arrays do not match, dimensions with size 1 are stretched to the size of the other
array. ex : (3,3)=2D ,(3) =1D ---> CONVERTED (1,3) than strech to (3,3) If there is a dimension whose size is not 1 in either
of the two arrays, it cannot be broadcasted, and an error is raised.

In [882… a=np.arange(12).reshape(4,3)
b=np.arange(3)
print(a)

[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]

In [883… print(b)

[0 1 2]

In [884… print(a+b)

[[ 0 2 4]
[ 3 5 7]
[ 6 8 10]
[ 9 11 13]]
EXPLANATION : Arthematic Operation possible because , Here a = (4,3) is 2D and b =(3) is 1D so did converted (3) to (1,3)
and streched to (4,3)

In [885… a = np.arange(3).reshape(1,3)
b = np.arange(4).reshape(4,1)
print(a)

[[0 1 2]]
In [886… print(b)

[[0]
[1]
[2]
[3]]

In [887… print(a+b)

[[0 1 2]
[1 2 3]
[2 3 4]
[3 4 5]]

In [888… a = np.array([1])
b=np.arange(4).reshape(2,2)

In [889… print(a)

[1]

In [890… print(b)

[[0 1]
[2 3]]

In [891… print(a +b)

[[1 2]
[3 4]]

In [892… a=np.arange(12).reshape(3,4)
b=np.arange(12).reshape(4,3)
print(a)
print(b)
print(a+b)

[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[892], line 5
3 print(a)
4 print(b)
----> 5 print(a+b)

ValueError: operands could not be broadcast together with shapes (3,4) (4,3)

In [893… a=np.arange(16).reshape(4,4)
b=np.arange(4).reshape(2,2)
print(a)
print(b)
print(a+b)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
[[0 1]
[2 3]]
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[893], line 5
3 print(a)
4 print(b)
----> 5 print(a+b)

ValueError: operands could not be broadcast together with shapes (4,4) (2,2)

warking with mathmatical formulas


In [896… k = np.arange(10)

In [898… k

Out[898… array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [900… np.sum(k)

Out[900… 45

In [902… np.sin(k)

Out[902… array([ 0. , 0.84147098, 0.90929743, 0.14112001, -0.7568025 ,


-0.95892427, -0.2794155 , 0.6569866 , 0.98935825, 0.41211849])

sigmoid

In [905… def sigmoid(array):


return 1/(1+np.exp(-(array)))
k = np.arange(10)
sigmoid(k)

Out[905… array([0.5 , 0.73105858, 0.88079708, 0.95257413, 0.98201379,


0.99330715, 0.99752738, 0.99908895, 0.99966465, 0.99987661])

In [907… k = np.arange(100)
sigmoid(k)
Out[907… array([0.5 , 0.73105858, 0.88079708, 0.95257413, 0.98201379,
0.99330715, 0.99752738, 0.99908895, 0.99966465, 0.99987661,
0.9999546 , 0.9999833 , 0.99999386, 0.99999774, 0.99999917,
0.99999969, 0.99999989, 0.99999996, 0.99999998, 0.99999999,
1. , 1. , 1. , 1. , 1. ,
1. , 1. , 1. , 1. , 1. ,
1. , 1. , 1. , 1. , 1. ,
1. , 1. , 1. , 1. , 1. ,
1. , 1. , 1. , 1. , 1. ,
1. , 1. , 1. , 1. , 1. ,
1. , 1. , 1. , 1. , 1. ,
1. , 1. , 1. , 1. , 1. ,
1. , 1. , 1. , 1. , 1. ,
1. , 1. , 1. , 1. , 1. ,
1. , 1. , 1. , 1. , 1. ,
1. , 1. , 1. , 1. , 1. ,
1. , 1. , 1. , 1. , 1. ,
1. , 1. , 1. , 1. , 1. ,
1. , 1. , 1. , 1. , 1. ,
1. , 1. , 1. , 1. , 1. ])

mean squared error

In [910… actual = np.random.randint(1,50,25) # hear 1 is the starting number,,,50 is th


predicted = np.random.randint(1,50,25)

In [912… actual

Out[912… array([36, 14, 2, 32, 31, 45, 44, 25, 15, 9, 49, 31, 16, 18, 21, 34, 14,
30, 46, 41, 17, 38, 17, 14, 45])

In [914… predicted

Out[914… array([23, 17, 44, 42, 37, 25, 5, 48, 41, 26, 33, 21, 25, 49, 21, 49, 10,
1, 29, 40, 9, 18, 30, 14, 27])

In [916… def mse(actual,predicted):


return np.mean((actual-predicted)**2)
mse(actual,predicted)

Out[916… 368.8

In [918… actual-predicted

Out[918… array([ 13, -3, -42, -10, -6, 20, 39, -23, -26, -17, 16, 10, -9,
-31, 0, -15, 4, 29, 17, 1, 8, 20, -13, 0, 18])

In [920… (actual-predicted)**2

Out[920… array([ 169, 9, 1764, 100, 36, 400, 1521, 529, 676, 289, 256,
100, 81, 961, 0, 225, 16, 841, 289, 1, 64, 400,
169, 0, 324])

In [922… np.mean((actual-predicted)**2)

Out[922… 368.8

Working with missing values


In [925… s = np.array([1,2,3,4,np.nan,6])
s

Out[925… array([ 1., 2., 3., 4., nan, 6.])

In [927… np.isnan(s)

Out[927… array([False, False, False, False, True, False])

In [929… s[np.isnan(s)] #Nan values

Out[929… array([nan])

In [931… s[~np.isnan(s)] # not nan values

Out[931… array([1., 2., 3., 4., 6.])

plotting Graphs
In [934… x = np.linspace(-10,10,100) # hear -10 to 10 is the range of the given values ,
x

Out[934… array([-10. , -9.7979798 , -9.5959596 , -9.39393939,


-9.19191919, -8.98989899, -8.78787879, -8.58585859,
-8.38383838, -8.18181818, -7.97979798, -7.77777778,
-7.57575758, -7.37373737, -7.17171717, -6.96969697,
-6.76767677, -6.56565657, -6.36363636, -6.16161616,
-5.95959596, -5.75757576, -5.55555556, -5.35353535,
-5.15151515, -4.94949495, -4.74747475, -4.54545455,
-4.34343434, -4.14141414, -3.93939394, -3.73737374,
-3.53535354, -3.33333333, -3.13131313, -2.92929293,
-2.72727273, -2.52525253, -2.32323232, -2.12121212,
-1.91919192, -1.71717172, -1.51515152, -1.31313131,
-1.11111111, -0.90909091, -0.70707071, -0.50505051,
-0.3030303 , -0.1010101 , 0.1010101 , 0.3030303 ,
0.50505051, 0.70707071, 0.90909091, 1.11111111,
1.31313131, 1.51515152, 1.71717172, 1.91919192,
2.12121212, 2.32323232, 2.52525253, 2.72727273,
2.92929293, 3.13131313, 3.33333333, 3.53535354,
3.73737374, 3.93939394, 4.14141414, 4.34343434,
4.54545455, 4.74747475, 4.94949495, 5.15151515,
5.35353535, 5.55555556, 5.75757576, 5.95959596,
6.16161616, 6.36363636, 6.56565657, 6.76767677,
6.96969697, 7.17171717, 7.37373737, 7.57575758,
7.77777778, 7.97979798, 8.18181818, 8.38383838,
8.58585859, 8.78787879, 8.98989899, 9.19191919,
9.39393939, 9.5959596 , 9.7979798 , 10. ])

In [936… y = x

In [938… y
Out[938… array([-10. , -9.7979798 , -9.5959596 , -9.39393939,
-9.19191919, -8.98989899, -8.78787879, -8.58585859,
-8.38383838, -8.18181818, -7.97979798, -7.77777778,
-7.57575758, -7.37373737, -7.17171717, -6.96969697,
-6.76767677, -6.56565657, -6.36363636, -6.16161616,
-5.95959596, -5.75757576, -5.55555556, -5.35353535,
-5.15151515, -4.94949495, -4.74747475, -4.54545455,
-4.34343434, -4.14141414, -3.93939394, -3.73737374,
-3.53535354, -3.33333333, -3.13131313, -2.92929293,
-2.72727273, -2.52525253, -2.32323232, -2.12121212,
-1.91919192, -1.71717172, -1.51515152, -1.31313131,
-1.11111111, -0.90909091, -0.70707071, -0.50505051,
-0.3030303 , -0.1010101 , 0.1010101 , 0.3030303 ,
0.50505051, 0.70707071, 0.90909091, 1.11111111,
1.31313131, 1.51515152, 1.71717172, 1.91919192,
2.12121212, 2.32323232, 2.52525253, 2.72727273,
2.92929293, 3.13131313, 3.33333333, 3.53535354,
3.73737374, 3.93939394, 4.14141414, 4.34343434,
4.54545455, 4.74747475, 4.94949495, 5.15151515,
5.35353535, 5.55555556, 5.75757576, 5.95959596,
6.16161616, 6.36363636, 6.56565657, 6.76767677,
6.96969697, 7.17171717, 7.37373737, 7.57575758,
7.77777778, 7.97979798, 8.18181818, 8.38383838,
8.58585859, 8.78787879, 8.98989899, 9.19191919,
9.39393939, 9.5959596 , 9.7979798 , 10. ])

In [940… import matplotlib.pyplot as plt


plt.plot(x,y)

Out[940… [<matplotlib.lines.Line2D at 0x1620164c4a0>]

In [941… x = np.linspace(-10,10,100)
y = x**2
plt.plot(x,y)
Out[941… [<matplotlib.lines.Line2D at 0x16201f30080>]

In [942… x = np.linspace(-10,20,100) # hear -10 is the bigns number,,,10 is the last nu


y = np.sin(x)
plt.plot(x,y)

Out[942… [<matplotlib.lines.Line2D at 0x16201f8e660>]

In [943… x = np.linspace(-10,10,100)
y = x*np.log(x)
plt.plot(x,y)

C:\Users\LEELA MANOHAR\AppData\Local\Temp\ipykernel_6172\1961417358.py:2: Runtime


Warning: invalid value encountered in log
y = x*np.log(x)
Out[943… [<matplotlib.lines.Line2D at 0x16201f53e60>]

In [944… x = np.linspace(-10,10,100)
y = 1/(1+np.exp(-x))
plt.plot(x,y)

Out[944… [<matplotlib.lines.Line2D at 0x162017ac8f0>]


Numpy
In [946… import numpy as np
import matplotlib.pyplot as plt

In [948… x = np.linspace(0,10,100)
y = np.linspace(0,10,100)

In [950… f = x**2+y**2

In [952… plt.figure(figsize=(4,2))
plt.plot(f)
plt.show()

In [954… x=np.arange(3)
y=np.arange(3)

In [955… x
Out[955… array([0, 1, 2])

In [957… y

Out[957… array([0, 1, 2])

In [961… xv , yv =np.meshgrid(x,y) # hear the misgard function is used to create a set

In [965… xv

Out[965… array([[0, 1, 2],


[0, 1, 2],
[0, 1, 2]])

In [968… yv

Out[968… array([[0, 0, 0],


[1, 1, 1],
[2, 2, 2]])

In [971… P = np.linspace(-4,4,9)
V = np.linspace(-5,5,11)
print(P)
print(V)

[-4. -3. -2. -1. 0. 1. 2. 3. 4.]


[-5. -4. -3. -2. -1. 0. 1. 2. 3. 4. 5.]

In [973… P_1,V_1 = np.meshgrid(P,V)

In [975… print(P_1)

[[-4. -3. -2. -1. 0. 1. 2. 3. 4.]


[-4. -3. -2. -1. 0. 1. 2. 3. 4.]
[-4. -3. -2. -1. 0. 1. 2. 3. 4.]
[-4. -3. -2. -1. 0. 1. 2. 3. 4.]
[-4. -3. -2. -1. 0. 1. 2. 3. 4.]
[-4. -3. -2. -1. 0. 1. 2. 3. 4.]
[-4. -3. -2. -1. 0. 1. 2. 3. 4.]
[-4. -3. -2. -1. 0. 1. 2. 3. 4.]
[-4. -3. -2. -1. 0. 1. 2. 3. 4.]
[-4. -3. -2. -1. 0. 1. 2. 3. 4.]
[-4. -3. -2. -1. 0. 1. 2. 3. 4.]]

In [977… print(V_1)

[[-5. -5. -5. -5. -5. -5. -5. -5. -5.]


[-4. -4. -4. -4. -4. -4. -4. -4. -4.]
[-3. -3. -3. -3. -3. -3. -3. -3. -3.]
[-2. -2. -2. -2. -2. -2. -2. -2. -2.]
[-1. -1. -1. -1. -1. -1. -1. -1. -1.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[ 2. 2. 2. 2. 2. 2. 2. 2. 2.]
[ 3. 3. 3. 3. 3. 3. 3. 3. 3.]
[ 4. 4. 4. 4. 4. 4. 4. 4. 4.]
[ 5. 5. 5. 5. 5. 5. 5. 5. 5.]]
Numpy Meshgrid creates coordinates for a grid
system
In [980… xv**2 + yv**2

Out[980… array([[0, 1, 4],


[1, 2, 5],
[4, 5, 8]])

In [982… x = np.linspace(-2,2,100)
y = np.linspace(-1,1,100)
xv,yv=np.meshgrid(x,y)
f = np.exp(-xv**2,-yv**2)

In [984… plt.pcolormesh(xv,yv,f,shading='auto')
plt.colorbar()
plt.grid()
plt.show()

In [985… import numpy as np


import matplotlib.pyplot as plt
def f(x,y):
return np.where((x**2+y**2<1),1.0,0.0)
x = np.linspace(-5,5,500)
y = np.linspace(-5,5,500)
xv,yv=np.meshgrid(x,y)
rectangular_mask = f(xv,yv)
plt.pcolormesh(xv,yv,rectangular_mask,shading='auto')
plt.colorbar()
plt.grid()
plt.show()
In [987… x = np.linspace(-4,4,9)

In [988… y= np.linspace(-5,5,11)

In [992… x_1,y_1=np.meshgrid(x,y)

In [994… random_data = np.random.random((11,9))


plt.contourf(x_1,y_1,random_data,cmap='jet')
plt.colorbar()
plt.show()
In [995… x_1,y_1 = np.meshgrid(x,y,sparse = True)

In [998… x_1

Out[998… array([[-4., -3., -2., -1., 0., 1., 2., 3., 4.]])

In [100… y_1

Out[100… array([[-5.],
[-4.],
[-3.],
[-2.],
[-1.],
[ 0.],
[ 1.],
[ 2.],
[ 3.],
[ 4.],
[ 5.]])

np.sort
In [100… a = np.random.randint(1,100,15)
a

Out[100… array([22, 12, 56, 94, 89, 77, 65, 50, 93, 99, 28, 60, 88, 32, 84])

In [100… b = np.random.randint(1,100,25).reshape(5,5)

In [100… b
Out[100… array([[89, 66, 86, 37, 64],
[77, 59, 63, 39, 51],
[69, 23, 99, 79, 56],
[38, 51, 70, 10, 33],
[68, 42, 99, 76, 88]])

In [100… np.sort(a) # hear we use the sort function to assend the values assinding order

Out[100… array([12, 22, 28, 32, 50, 56, 60, 65, 77, 84, 88, 89, 93, 94, 99])

In [101… np.sort(a)[::-1]

Out[101… array([99, 94, 93, 89, 88, 84, 77, 65, 60, 56, 50, 32, 28, 22, 12])

In [101… np.sort(b)

Out[101… array([[37, 64, 66, 86, 89],


[39, 51, 59, 63, 77],
[23, 56, 69, 79, 99],
[10, 33, 38, 51, 70],
[42, 68, 76, 88, 99]])

In [101… np.sort(b,axis = 0)

Out[101… array([[38, 23, 63, 10, 33],


[68, 42, 70, 37, 51],
[69, 51, 86, 39, 56],
[77, 59, 99, 76, 64],
[89, 66, 99, 79, 88]])

np.append
In [101… a

Out[101… array([22, 12, 56, 94, 89, 77, 65, 50, 93, 99, 28, 60, 88, 32, 84])

In [102… np.append(a,200) # hearb we use the append function to add the another number

Out[102… array([ 22, 12, 56, 94, 89, 77, 65, 50, 93, 99, 28, 60, 88,
32, 84, 200])

In [102… b

Out[102… array([[89, 66, 86, 37, 64],


[77, 59, 63, 39, 51],
[69, 23, 99, 79, 56],
[38, 51, 70, 10, 33],
[68, 42, 99, 76, 88]])

In [102… np.append(b,np.ones((b.shape[0],1)))

Out[102… array([89., 66., 86., 37., 64., 77., 59., 63., 39., 51., 69., 23., 99.,
79., 56., 38., 51., 70., 10., 33., 68., 42., 99., 76., 88., 1.,
1., 1., 1., 1.])

In [102… np.append(b,np.ones((b.shape[0],1)),axis=1)
Out[102… array([[89., 66., 86., 37., 64., 1.],
[77., 59., 63., 39., 51., 1.],
[69., 23., 99., 79., 56., 1.],
[38., 51., 70., 10., 33., 1.],
[68., 42., 99., 76., 88., 1.]])

In [102… np.append(b,np.random.random((b.shape[0],1)),axis = 1)

Out[102… array([[89. , 66. , 86. , 37. , 64. ,


0.26754088],
[77. , 59. , 63. , 39. , 51. ,
0.66090451],
[69. , 23. , 99. , 79. , 56. ,
0.5088528 ],
[38. , 51. , 70. , 10. , 33. ,
0.32865371],
[68. , 42. , 99. , 76. , 88. ,
0.90024838]])

np.concatenate
In [103… c = np.arange(6).reshape(2,3)
d = np.arange(6,12).reshape(2,3)

In [103… c

Out[103… array([[0, 1, 2],


[3, 4, 5]])

In [103… d

Out[103… array([[ 6, 7, 8],


[ 9, 10, 11]])

In [103… np.concatenate((c,d)) # it was combine the two matrixs into the one matrix

Out[103… array([[ 0, 1, 2],


[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])

np.unique
In [104… e = np.array([1,2,3,4,5,6,1,2,3,4,5,6])
e

Out[104… array([1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6])

In [104… np.unique(e) # it was remove the duplicate value in the array

Out[104… array([1, 2, 3, 4, 5, 6])

np.expend_dims
In [104… a

Out[104… array([22, 12, 56, 94, 89, 77, 65, 50, 93, 99, 28, 60, 88, 32, 84])

In [104… a.shape #hear we give the how many values are inside the array set

Out[104… (15,)

In [104… np.expand_dims(a,axis = 0) # it was convert the array into the matrix

Out[104… array([[22, 12, 56, 94, 89, 77, 65, 50, 93, 99, 28, 60, 88, 32, 84]])

In [105… np.expand_dims(a,axis = 0).shape

Out[105… (1, 15)

In [105… np.expand_dims(a,axis=1) # hear we use "axis = 1" it was display the output row

Out[105… array([[22],
[12],
[56],
[94],
[89],
[77],
[65],
[50],
[93],
[99],
[28],
[60],
[88],
[32],
[84]])

In [105… np.expand_dims(a,axis = 1).shape

Out[105… (15, 1)

np.where
In [105… a

Out[105… array([22, 12, 56, 94, 89, 77, 65, 50, 93, 99, 28, 60, 88, 32, 84])

In [106… np.where(a>50)

Out[106… (array([ 2, 3, 4, 5, 6, 8, 9, 11, 12, 14], dtype=int64),)

In [106… np.where(a>5,0,a)

Out[106… array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

In [106… np.where(a%2==0)
Out[106… (array([ 0, 1, 2, 3, 7, 10, 11, 12, 13, 14], dtype=int64),)

In [106… np.where(a%2 == 0,0,a)

Out[106… array([ 0, 0, 0, 0, 89, 77, 65, 0, 93, 99, 0, 0, 0, 0, 0])

np.argmax
arg = argument

In [106… a

Out[106… array([22, 12, 56, 94, 89, 77, 65, 50, 93, 99, 28, 60, 88, 32, 84])

In [107… np.argmax(a)

Out[107… 9

In [107… b

Out[107… array([[89, 66, 86, 37, 64],


[77, 59, 63, 39, 51],
[69, 23, 99, 79, 56],
[38, 51, 70, 10, 33],
[68, 42, 99, 76, 88]])

In [107… np.argmax(b,axis = 1)

Out[107… array([0, 0, 2, 2, 2], dtype=int64)

In [107… np.argmax(b,axis = 0)

Out[107… array([0, 0, 2, 2, 4], dtype=int64)

In [107… a

Out[107… array([22, 12, 56, 94, 89, 77, 65, 50, 93, 99, 28, 60, 88, 32, 84])

In [108… np.argmin(a)

Out[108… 1

On Statics:

np.cumsum
numpy.cumsum() function is used when we want to compute the cumulative sum of array elements over a given axis.

In [108… a

Out[108… array([22, 12, 56, 94, 89, 77, 65, 50, 93, 99, 28, 60, 88, 32, 84])

In [108… np.cumsum(a)
Out[108… array([ 22, 34, 90, 184, 273, 350, 415, 465, 558, 657, 685, 745, 833,
865, 949])

In [108… b

Out[108… array([[89, 66, 86, 37, 64],


[77, 59, 63, 39, 51],
[69, 23, 99, 79, 56],
[38, 51, 70, 10, 33],
[68, 42, 99, 76, 88]])

In [109… np.cumsum(b)

Out[109… array([ 89, 155, 241, 278, 342, 419, 478, 541, 580, 631, 700,
723, 822, 901, 957, 995, 1046, 1116, 1126, 1159, 1227, 1269,
1368, 1444, 1532])

In [109… np.cumsum(b,axis=1)

Out[109… array([[ 89, 155, 241, 278, 342],


[ 77, 136, 199, 238, 289],
[ 69, 92, 191, 270, 326],
[ 38, 89, 159, 169, 202],
[ 68, 110, 209, 285, 373]])

In [109… np.cumsum(b,axis = 0)

Out[109… array([[ 89, 66, 86, 37, 64],


[166, 125, 149, 76, 115],
[235, 148, 248, 155, 171],
[273, 199, 318, 165, 204],
[341, 241, 417, 241, 292]])

In [109… a

Out[109… array([22, 12, 56, 94, 89, 77, 65, 50, 93, 99, 28, 60, 88, 32, 84])

In [109… np.cumprod(a)

Out[109… array([ 22, 264, 14784, 1389696, 123682944,


933652096, 557844096, 2122401024, -185200384, -1154968832,
2020611072, 977580032, 127696896, -208666624, -348127232])

np.precentile
numpy.percentile()function used to compute the nth percentile of the given data (array elements) along the specified axis.

In [110… a

Out[110… array([22, 12, 56, 94, 89, 77, 65, 50, 93, 99, 28, 60, 88, 32, 84])

In [110… np.percentile(a,100)

Out[110… 99.0

In [110… np.percentile(a,0)

Out[110… 12.0
In [110… np.percentile(a,50)

Out[110… 65.0

In [111… np.median(a)

Out[111… 65.0

np.histogram
Numpy has a built-in numpy.histogram() function which represents the frequency of data distribution in the graphical form.

In [111… a

Out[111… array([22, 12, 56, 94, 89, 77, 65, 50, 93, 99, 28, 60, 88, 32, 84])

In [111… np.histogram(a,bins= [10,20,30,40,50,60,70,8090,100])

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[1115], line 1
----> 1 np.histogram(a,bins= [10,20,30,40,50,60,70,8090,100])

File ~\anaconda3\Lib\site-packages\numpy\lib\histograms.py:780, in histogram(a, b


ins, range, density, weights)
680 r"""
681 Compute the histogram of a dataset.
682
(...)
776
777 """
778 a, weights = _ravel_and_check_weights(a, weights)
--> 780 bin_edges, uniform_bins = _get_bin_edges(a, bins, range, weights)
782 # Histogram is an integer or a float array depending on the weights.
783 if weights is None:

File ~\anaconda3\Lib\site-packages\numpy\lib\histograms.py:431, in _get_bin_edges


(a, bins, range, weights)
429 bin_edges = np.asarray(bins)
430 if np.any(bin_edges[:-1] > bin_edges[1:]):
--> 431 raise ValueError(
432 '`bins` must increase monotonically, when an array')
434 else:
435 raise ValueError('`bins` must be 1d, when an array')

ValueError: `bins` must increase monotonically, when an array

In [111… np.histogram(a ,bins=[0,50,100])

Out[111… (array([ 4, 11], dtype=int64), array([ 0, 50, 100]))

np.corrcoef
Return pearson product-moment correlation coefficients

In [112… salary = np.array([20000,40000,250000,350000,60000])


experience = np.array([1,3,2,4,2])
In [112… salary

Out[112… array([ 20000, 40000, 250000, 350000, 60000])

In [112… experience

Out[112… array([1, 3, 2, 4, 2])

In [112… np.corrcoef(salary,experience)

Out[112… array([[1. , 0.64257262],


[0.64257262, 1. ]])

Utility functions
np.isin
With the help of numpy.isin() method, we can see that one array having values are checked in a different numpy array having
different elements with different sizes.

In [113… a

Out[113… array([22, 12, 56, 94, 89, 77, 65, 50, 93, 99, 28, 60, 88, 32, 84])

In [113… items = [10,20,30,40,50,60,70,80,90,100]


np.isin(a,items)

Out[113… array([False, False, False, False, False, False, False, True, False,
False, False, True, False, False, False])

In [113… a[np.isin(a,items)]

Out[113… array([50, 60])

np.flip
The numpy.flip() function reverses the order of array elements along the specified axis, preserving the shape of the array.

In [113… a

Out[113… array([22, 12, 56, 94, 89, 77, 65, 50, 93, 99, 28, 60, 88, 32, 84])

In [114… np.flip(a)

Out[114… array([84, 32, 88, 60, 28, 99, 93, 50, 65, 77, 89, 94, 56, 12, 22])

In [114… b

Out[114… array([[89, 66, 86, 37, 64],


[77, 59, 63, 39, 51],
[69, 23, 99, 79, 56],
[38, 51, 70, 10, 33],
[68, 42, 99, 76, 88]])

In [114… np.flip(b)
Out[114… array([[88, 76, 99, 42, 68],
[33, 10, 70, 51, 38],
[56, 79, 99, 23, 69],
[51, 39, 63, 59, 77],
[64, 37, 86, 66, 89]])

In [114… np.flip(b,axis = 1)

Out[114… array([[64, 37, 86, 66, 89],


[51, 39, 63, 59, 77],
[56, 79, 99, 23, 69],
[33, 10, 70, 51, 38],
[88, 76, 99, 42, 68]])

In [114… np.flip(b,axis = 0)

Out[114… array([[68, 42, 99, 76, 88],


[38, 51, 70, 10, 33],
[69, 23, 99, 79, 56],
[77, 59, 63, 39, 51],
[89, 66, 86, 37, 64]])

np.put
The numpy.put() function replaces specific elements of an array with given values of p_array. Array indexed works on
flattened array.

In [115… a

Out[115… array([22, 12, 56, 94, 89, 77, 65, 50, 93, 99, 28, 60, 88, 32, 84])

In [115… np.put(a,[0,1],[110,530])

In [115… a

Out[115… array([110, 530, 56, 94, 89, 77, 65, 50, 93, 99, 28, 60, 88,
32, 84])

np.delete
The numpy.delete() function returns a new array with the deletion of sub-arrays along with the mentioned axis.

In [115… a

Out[115… array([110, 530, 56, 94, 89, 77, 65, 50, 93, 99, 28, 60, 88,
32, 84])

In [116… np.delete(a,0)

Out[116… array([530, 56, 94, 89, 77, 65, 50, 93, 99, 28, 60, 88, 32,
84])

In [116… np.delete(a,[0,2,4])

Out[116… array([530, 94, 77, 65, 50, 93, 99, 28, 60, 88, 32, 84])

Set functions
np.union 1dnp.intersect1dnp.setdiff1dnp.setxor1dnp.in1d
In [116… m = np.array([1,2,3,4,5])
n = np.array([3,4,5,6,7])

In [117… np.union1d(m,n)

Out[117… array([1, 2, 3, 4, 5, 6, 7])

In [117… np.intersect1d(m,n)

Out[117… array([3, 4, 5])

In [117… np.setdiff1d(m,n)

Out[117… array([1, 2])

In [118… np.setdiff1d(n,m)

Out[118… array([6, 7])

In [118… np.setxor1d(m,n)

Out[118… array([1, 2, 6, 7])

In [118… np.in1d(m,1)

Out[118… array([ True, False, False, False, False])

In [118… m[np.in1d(m,1)]

Out[118… array([1])

In [118… np.in1d(m,10)

Out[118… array([False, False, False, False, False])

np.clip
numpy.clip() function is used to Clip (limit) the values in an array.

In [119… a

Out[119… array([110, 530, 56, 94, 89, 77, 65, 50, 93, 99, 28, 60, 88,
32, 84])

In [119… np.clip(a,a_min=15,a_max = 50)

Out[119… array([50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 28, 50, 50, 32, 50])

it clips the minimum data to 15 and replaces everything below data to 15 and maximum to 50

np.swapaxes
numpy.swapaxes() function interchange two axes of an array.

In [120… arr = np.array([[1,2,3],[4,5,6]])


swapped_arr = np.swapaxes(arr,0,1)
In [120… arr

Out[120… array([[1, 2, 3],


[4, 5, 6]])

In [120… swapped_arr

Out[120… array([[1, 4],


[2, 5],
[3, 6]])

In [120… print("Original array:")


print(arr)

Original array:
[[1 2 3]
[4 5 6]]

In [120… print("Swapped_array:")
print(swapped_arr)

Swapped_array:
[[1 4]
[2 5]
[3 6]]

In [ ]:

In [ ]:

In [ ]:

In [ ]:

You might also like