Numpy Project Part-1
Numpy Project Part-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 [626… print(b)
[[21 22 23 2]
[24 25 26 24]]
In [630… np.array([11,22,33,44] , dtype=bool ) #hear we change the data type int to bool
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
In [639… np.arange(1,101).reshape(10,10)
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))
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
In [649… np.linspace(-2,10,8)
identity
identity matrix is that diagonal items will be ones and everithing is zeros
ARRAYN ATTRIBUTES
In [654… a1=np.arange(10) # 1D
a1
[[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
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
[[4, 5],
[6, 7]]])
Out[667… 8
In [668… a2
In [669… a2.size
Out[669… 12
item size
memory occupied by the item
In [671… a1
In [672… a1.itemsize
Out[672… 4
In [673… a2.itemsize
Out[673… 8
In [674… a3.item
dtype
gives data type of the item
In [676… print(a1.dtype)
print(a2.dtype)
print(a3.dtype)
int32
float64
int32
In [679… x.astype(int)
Array operations
In [681… z1 = np.arange(12).reshape(3,4)
z2 = np.arange(12,24).reshape(3,4)
In [682… z1
In [683… z2
scalar operation
scalar operation on numpy arrays include performing addition or subration,or multipication on each element of a numpy array
In [686… z1-2
relational operator
In [691… z2
In [692… z2>2
In [693… z2<20
Vector operation
In [695… z1
In [696… z2
In [697… z1+z2
Out[697… array([[12, 14, 16, 18],
[20, 22, 24, 26],
[28, 30, 32, 34]])
In [698… z1*z2
In [699… z1-z2
In [700… z1/z2
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[703… 78.0
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)
In [713… #mean
k1
In [714… np.mean(k1)
Out[714… 46.44444444444444
In [716… # meadion
np.median(k1)
Out[716… 48.0
In [717… np.median(k1,axis = 1)
In [718… np.std(k1)
Out[718… 18.685176008513697
In [719… np.std(k1,axis = 0)
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
In [723… np.cos(k1)
In [724… np.tan(k1)
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
In [728… s3
In [731… np.exp(s2)
round/floor/ceil
1.round
[2. 2. 4. 4. 6.]
In [736… np.round(np.random.random((2,3))*100)
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)
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)
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
In [746… p2
In [747… p3
Out[747… array([[[0, 1],
[2, 3]],
[[4, 5],
[6, 7]]])
idexing on 1D array
In [749… p1
In [750… p1[-1]
Out[750… 9
In [751… p1[0]
Out[751… 0
indexing on 2D array
In [753… p2
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
[[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
In [766… p1[2:5]
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]
Slicing on 2D
In [769… p2
In [770… p2[0, :]
EXPLANATION :Here 0 represents first row and (:) represnts Total column
In [771… p2[:,2]
EXPLANATION :Here we want all rows so (:) , and we want 3rd column so 2
In [772… p2
In [773… p2[1:3]
In [774… p2[1:3,1:3]
In [775… p2
In [776… p2[::2,::3]
In [777… p2
In [780… p2
In [781… p2[1]
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
In [784… p2[0:2]
In [785… p2[0:2,1:]
In [786… p2
In [787… p2[0:2]
In [788… p2[0:2,1::2]
Slicing in 3D
In [790… p3 = np.arange(27).reshape(3,3,3)
p3
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
In [791… p3[1]
Out[791… array([[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]])
In [792… p3[::2]
In [793… p3
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
In [794… p3[0]
In [795… p3[0,1,:]
EXPLANATION : 0 represnts first matrix , 1 represents second row , (:) means total
In [796… p3
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
In [797… p3[1]
Out[797… array([[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]])
In [798… p3[1]
In [799… p3[1,:,1]
EXPLANATION : 1 respresnts middle column , (:) all columns , 1 represnts middle column
In [800… p3
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
In [801… p3[1,:,1]
In [802… p3
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
In [803… p3[2]
In [806… p3
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
In [807… p3[0::2]
In [808… p3[0::2,0]
In [809… p3[0::2,0,::2]
Iterating
In [811… p1
In [813… p2
[0 1 2 3]
[4 5 6 7]
[ 8 9 10 11]
In [815… p3
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
[[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
In [820… np.transpose(p2)
In [822… p3
Out[822… array([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
In [823… p3.T
[[ 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
In [826… p2.ravel()
In [827… p3
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
In [828… p3.ravel()
In [830… w1=np.arange(12).reshape(3,4)
w2=np.arange(12,24).reshape(3,4)
In [831… w1
In [832… w2
In [833… np.hstack((w1,w2))
In [834… w1
In [835… w2
In [836… np.vstack((w1,w2))
Splitting
its opposite of stacking
In [838… w1
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
In [842… np.vsplit(w2,3)
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
Out[848… 20.166126229779607
List
Out[851… 8448728
Numpy
In [853… R = np.arange(1000000)
sys.getsizeof(R)
Out[853… 4000112
Out[854… 200112
In [857… w[1,2]
Out[857… 5
In [858… w[1:3]
In [859… w[1:3,1:3]
In [861… w
In [862… w[[0,2,3]]
In [863… z = np.arange(24).reshape(6,4)
z
In [864… z[[0,2,3,5]]
In [865… z[:,[0,2,3]]
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
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[871… array([ 8, 80, 94, 6, 26, 80, 44, 48, 54, 56, 4, 98, 58])
In [872… (G>50)&(G%2==0)
In [873… G[(G>50)&(G%2==0)]
In [874… G%7==0
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
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]]
[[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)
In [898… k
In [900… np.sum(k)
Out[900… 45
In [902… np.sin(k)
sigmoid
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. ])
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])
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
In [927… np.isnan(s)
Out[929… array([nan])
plotting Graphs
In [934… x = np.linspace(-10,10,100) # hear -10 to 10 is the range of the given values ,
x
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 [941… x = np.linspace(-10,10,100)
y = x**2
plt.plot(x,y)
Out[941… [<matplotlib.lines.Line2D at 0x16201f30080>]
In [943… x = np.linspace(-10,10,100)
y = x*np.log(x)
plt.plot(x,y)
In [944… x = np.linspace(-10,10,100)
y = 1/(1+np.exp(-x))
plt.plot(x,y)
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
In [965… xv
In [968… yv
In [971… P = np.linspace(-4,4,9)
V = np.linspace(-5,5,11)
print(P)
print(V)
In [975… print(P_1)
In [977… print(V_1)
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 [988… y= np.linspace(-5,5,11)
In [992… x_1,y_1=np.meshgrid(x,y)
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)
In [101… np.sort(b,axis = 0)
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
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)
np.concatenate
In [103… c = np.arange(6).reshape(2,3)
d = np.arange(6,12).reshape(2,3)
In [103… c
In [103… d
In [103… np.concatenate((c,d)) # it was combine the two matrixs into the one matrix
np.unique
In [104… e = np.array([1,2,3,4,5,6,1,2,3,4,5,6])
e
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,)
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=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]])
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)
In [106… np.where(a>5,0,a)
In [106… np.where(a%2==0)
Out[106… (array([ 0, 1, 2, 3, 7, 10, 11, 12, 13, 14], dtype=int64),)
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
In [107… np.argmax(b,axis = 1)
In [107… np.argmax(b,axis = 0)
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
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)
In [109… np.cumsum(b,axis = 0)
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)
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])
---------------------------------------------------------------------------
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])
np.corrcoef
Return pearson product-moment correlation coefficients
In [112… experience
In [112… np.corrcoef(salary,experience)
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])
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)]
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
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)
In [114… np.flip(b,axis = 0)
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)
In [117… np.intersect1d(m,n)
In [117… np.setdiff1d(m,n)
In [118… np.setdiff1d(n,m)
In [118… np.setxor1d(m,n)
In [118… np.in1d(m,1)
In [118… m[np.in1d(m,1)]
Out[118… array([1])
In [118… np.in1d(m,10)
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])
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… swapped_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 [ ]: