InfinityCodeX

Unlock the power of Python, Data Science, Machine Learning, and Deep Learning with our comprehensive guides! Whether you're a beginner eager to dive into AI or an experienced professional looking to sharpen your skills, our blog offers easy-to-follow tutorials, insightful tips, and expert advice to fuel your AI career. Dive in today and start mastering the cutting-edge technologies shaping the future!

Numpy With Boolean Array


In this session we are going to cover some important concepts of Numpy which are :

(i)   Indexing
(ii)  Slicing
(iii) Stacking

Before diving right into this lets see an example & understand how the index value is assigned to the list / array.






As you all can see in the above example that the initial value of the list/array starts with "0" & end with "11" so with the help of that we can access any value assigned to a particular index number. So we had understood the concept of indexing from value 0 till N now you must be thinking what does the negative value means. So as you all can see in the above example that to access the last value of the list/array we use the integer "-1" which will make easy to access the value while sorting or finding any certain value & this goes on to find the 2nd last value we will use "-2" then for 3rd last "-3" & so on.

Indexing :

Lets take some practical example :

code 1 :

import numpy as np
a=['H','E','L','L','O',' ','P','Y','T','H','O','N']
a[0] 

output :

'H'

code 2 :

import numpy as np
a=['H','E','L','L','O',' ','P','Y','T','H','O','N']

a[-1] 

output :

'N'


Now i think you will experiment with this example & explore more. Look guys you have to practice & create your own examples & see what the answers you get & please share your experience by commenting below.

So we basically saw the indexing above now its time for slicing.

Slicing :


Basically Slicing is used for getting the exact value that we want from an N-Dimension array. Before starting slicing there is basic rule that you have to understand.



code:

import numpy as np
n=[10,20,30,40]
n[0:3]

output :

[10,20,30]


code :

import numpy as np
n=[10,20,30,40]
n[-4:-1]

output :

[10,20,30]

we can use negative indexing too.

code :

import numpy as np
n=np.array([1,2,3,4])
print(n[0:2])
print(n[-1])

output :

[1  2]
-4

we can do same thing with numpy array.

As we had seen all the basic concepts of list / array. Now use multi-dimension array & apply our knowledge on it.

code :

import numpy as np
n=np.array([[10,20,30],[40,50,60],[70,80,90]])


output :

array([[10, 20, 30],
       [40, 50, 60],
       [70, 80, 90]])
So here we had taken any numpy array with shape (3,3) which means that it has 3 rows & 3 columns.


Now lets play an coding game where we have to print certain given elements from that (3,3) numpy array.

Q. Find the element's :

(i) 50

import numpy as np
n=np.array([[10,20,30],[40,50,60],[70,80,90]])
n[1,1]

(ii) 90

import numpy as np
n=np.array([[10,20,30],[40,50,60],[70,80,90]])
n[2,2]

(iii) 40

import numpy as np
n=np.array([[10,20,30],[40,50,60],[70,80,90]])
n[1,0]

(iv) 30,60

import numpy as np
n=np.array([[10,20,30],[40,50,60],[70,80,90]])
n[0:2,2]


(v) 40,50

import numpy as np
n=np.array([[10,20,30],[40,50,60],[70,80,90]])
n[-2,0:2]

(vi) column 1 & column 2 only

import numpy as np
n=np.array([[10,20,30],[40,50,60],[70,80,90]])

n[:,1:3]              # [:,x] <= means go through all the rows , [x,:] <= means go through all the columns 
output:
array([[20, 30],
       [50, 60],
       [80, 90]])
Now here are some problems for you to solve :

Q.) Create an (4,3) numpy array with value's from 10 till 120 with each list contains 4 elements & find the following :
(i) 120              (ii) 10,60
(iii)100,40        (iv) 10,90

* Iteration Through An Array :
Lets take same numpy array & work on it.
code :
import numpy as np
n=np.array([[10,20,30],[40,50,60],[70,80,90]])
for row in n:
          print(row)
output :
       [10, 20, 30],        [40, 50, 60],        [70, 80, 90] 
 (i) .flat :

import numpy as np 
n=np.array([[10,20,30],[40,50,60],[70,80,90]])
for x in n.flat:
          print(x)
asdas
output :
10
20
30
40
50
60
70
80
90
asdas
(ii) Stacking 2 arrays :
asdas
(a) vstack
asdas
import numpy as np
a=np.arange(6).reshape(3,2)
b=np.arange(6,12).reshape(3,2) 
print("a:\n",a)                                    # \n for new line 
print("b:\n",b)
print("a stack b:\n",np.vstack((a,b)))
asdas
output :
 a: 
[[0 1] 
[2 3] 
[4 5]] 
b: 
[[ 6 7] 
[ 8 9] 
[10 11]] 
a stack b: 
[[ 0 1] 
[ 2 3] 
[ 4 5] 
[ 6 7] 
[ 8 9] 
[10 11]] 
This v.stack means vertical stacking which means that we kept the box 1 on box 2.
(b) hstack
asdas
import numpy as np
a=np.arange(6).reshape(3,2)
b=np.arange(6,12).reshape(3,2) 
print("a:\n",a)                                    # \n for new line 
print("b:\n",b)
print("a stack b:\n",np.hstack((a,b))) 
asdas
output :
a:
 [[0 1]
 [2 3]
 [4 5]]
b:
 [[ 6  7]
 [ 8  9]
 [10 11]]
a stack b:
 [[ 0  1  6  7]
 [ 2  3  8  9]
 [ 4  5 10 11]]
hstack means stacking horizontally 1 box on another. 1st row of both the row will come together, followed by 2nd row of both rows & 3rd row of both numpy array.
 
(iii) split
 
 There are 2 types of split horizontal split (hsplit) & vertical split (vsplit).
 
(a) hsplit
 


import numpy as np
a=np.arange(10).reshape(2,5)
print(a)
x=np.h.split(a,5)
print("For 0 :\n",x[0])
print("For 1 :\n",x[1])
print("For 2 :\n",x[2]) 


output :
[[0 1 2 3 4]
 [5 6 7 8 9]]
For 0 :
 [[0]
 [5]]
For 1 :
 [[1]
 [6]]
For 2 :
 [[2]
 [7]]
 
(b) vsplit :
 
a=np.arange(30).reshape(2,15)
print(a)
x=np.vsplit(a,2)
print("For 0 :\n",x[0])
print("For 1 :\n",x[1]) 
 
output :
[[ 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 27 28 29]]
For 0 :
 [[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]]
For 1 :
 [[15 16 17 18 19 20 21 22 23 24 25 26 27 28 29]] 
 
We can use various operators in this eg:
 
1.) a=np.arange(20).reshape(2,10) 
     b= a>7
     b
output:
array([[False, False, False, False, False, False, False, False,  True,
         True],
       [ True,  True,  True,  True,  True,  True,  True,  True,  True,
         True]]) 
 
2.) a[b]=5
     a

output :
array([[0, 1, 2, 3, 4, 5, 6, 7, 5, 5],
       [5, 5, 5, 5, 5, 5, 5, 5, 5, 5]])
 
it put the value 5 where ever the value True.
 
 
Look guys All the features & functionalities which we saw here is mostly present in numpy array. That's the main reason why we use numpy array instead of the list. I hope you will utilize this features & practice it to improve your knowledge.

No comments:

No Spamming and No Offensive Language

Powered by Blogger.