0% found this document useful (0 votes)
23 views4 pages

Numpy-4 - Jupyter Notebook

The document contains Python code snippets demonstrating various NumPy functionalities, including array creation, statistical operations, and conditional indexing. Key operations include finding minimum and maximum values, calculating sums, and performing horizontal and vertical stacking of arrays. Additionally, it showcases how to use conditional statements to locate specific elements within arrays.

Uploaded by

VAIBHAV Gupta
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)
23 views4 pages

Numpy-4 - Jupyter Notebook

The document contains Python code snippets demonstrating various NumPy functionalities, including array creation, statistical operations, and conditional indexing. Key operations include finding minimum and maximum values, calculating sums, and performing horizontal and vertical stacking of arrays. Additionally, it showcases how to use conditional statements to locate specific elements within arrays.

Uploaded by

VAIBHAV Gupta
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/ 4

In [1]:  import numpy as np

a=np.arange(1,6) # make a list 0f values from 1 to 5(6-1).


print(a)
print('-----------------------------------------------------------------')
b=np.min(a) #print minimum value of list a.
print(b)
print('-----------------------------------------------------------------')
b=np.max(a) #print maximum value of list a.
print(b)
print('-----------------------------------------------------------------')
b=np.argmin(a) #print lowest index value of list a.
print(b)
print('-----------------------------------------------------------------')
b=np.argmax(a) #print highest index value of list a.
print(b)
print('-----------------------------------------------------------------')
b=np.sqrt(a) #print square root value of each elements of list a.
print(b)
print('-----------------------------------------------------------------')
b=np.sin(a) #print sin value of list a.
print(b)
print('-----------------------------------------------------------------')
b=np.cos(a) #print cos value of list a.
print(b)
print('-----------------------------------------------------------------')
b=np.linspace(1,2,4) #print random 4 values between 1 and 2 including 1 and 2.
print(b)

[1 2 3 4 5]
-----------------------------------------------------------------
1
-----------------------------------------------------------------
5
-----------------------------------------------------------------
0
-----------------------------------------------------------------
4
-----------------------------------------------------------------
[1. 1.41421356 1.73205081 2. 2.23606798]
-----------------------------------------------------------------
[ 0.84147098 0.90929743 0.14112001 -0.7568025 -0.95892427]
-----------------------------------------------------------------
[ 0.54030231 -0.41614684 -0.9899925 -0.65364362 0.28366219]
-----------------------------------------------------------------
[1. 1.33333333 1.66666667 2. ]
In [2]:  a=np.random.seed(1)
a=np.random.randint(1,21,9).reshape(3,3)
print(a)
print('-----------------------------------------------------------------')
print(np.sum(a)) #print sum of all the elements of 2 D array. Method 1
print('-----------------------------------------------------------------')
print(a.sum()) #print sum of all the elements of 2 D array. Method 2
print('-----------------------------------------------------------------')
print(np.min(a)) #print minimum value of 2D array a.
print('-----------------------------------------------------------------')
print(np.max(a)) #print maximum value of 2D array a.
print('-----------------------------------------------------------------')
print(np.min(a,axis = 1)) # print minimum value of each row.axix=1 means row
print('-----------------------------------------------------------------')
print(np.min(a,axis = 0)) # print minimum value of each column.axix=0 means column
print('-----------------------------------------------------------------')
print(np.max(a,axis = 1)) # print maximum value of each row.axix=1 means row
print('-----------------------------------------------------------------')
print(np.max(a,axis = 0)) # print maximum value of each column.axix=0 means column
print('-----------------------------------------------------------------')
print(np.sum(a,axis =1)) #print sum of each row
print('-----------------------------------------------------------------')
print(np.sum(a,axis =0)) #print sum of each column
print('-----------------------------------------------------------------')
print(np.cumsum(a)) #cumulative sum
print('-----------------------------------------------------------------')
print(np.cumsum(a,axis=1)) #cumulative sum of rows
print('-----------------------------------------------------------------')
print(np.cumsum(a,axis=0)) #cumulative sum of columns
print('-----------------------------------------------------------------')

[[ 6 12 13]
[ 9 10 12]
[ 6 16 1]]
-----------------------------------------------------------------
85
-----------------------------------------------------------------
85
-----------------------------------------------------------------
1
-----------------------------------------------------------------
16
-----------------------------------------------------------------
[6 9 1]
-----------------------------------------------------------------
[ 6 10 1]
-----------------------------------------------------------------
[13 12 16]
-----------------------------------------------------------------
[ 9 16 13]
-----------------------------------------------------------------
[31 31 23]
-----------------------------------------------------------------
[21 38 26]
-----------------------------------------------------------------
[ 6 18 31 40 50 62 68 84 85]
-----------------------------------------------------------------
[[ 6 18 31]
[ 9 19 31]
[ 6 22 23]]
-----------------------------------------------------------------
[[ 6 12 13]
[15 22 25]
[21 38 26]]
-----------------------------------------------------------------
In [3]:  a=np.array([1,2,3,4])
b=np.array([5,6,7,8])
print(a)
print('-----------------------------------------------------------------')
print(b)
print('-----------------------------------------------------------------')
c=np.hstack((a,b)) #horizontaly arrange all values of list a and list b
print(c)
print('-----------------------------------------------------------------')
d=np.vstack((a,b)) #vertically arrange all values of list a and list b
print(d)
print('-----------------------------------------------------------------')
np.random.seed(122)
a=np.random.randint(1,21,9).reshape(3,3)
b=np.random.randint(31,51,9).reshape(3,3)
print(a)
print('-----------------------------------------------------------------')
print(b)
print('-----------------------------------------------------------------')
c=np.hstack((a,b)) #horizontaly arrange all values of Array a and Array b
print(c)
print('-----------------------------------------------------------------')
d=np.vstack((a,b)) #vertically arrange all values of Array a and Array b
print(d)

[1 2 3 4]
-----------------------------------------------------------------
[5 6 7 8]
-----------------------------------------------------------------
[1 2 3 4 5 6 7 8]
-----------------------------------------------------------------
[[1 2 3 4]
[5 6 7 8]]
-----------------------------------------------------------------
[[16 11 13]
[17 13 16]
[ 3 16 11]]
-----------------------------------------------------------------
[[40 37 45]
[34 35 50]
[50 44 31]]
-----------------------------------------------------------------
[[16 11 13 40 37 45]
[17 13 16 34 35 50]
[ 3 16 11 50 44 31]]
-----------------------------------------------------------------
[[16 11 13]
[17 13 16]
[ 3 16 11]
[40 37 45]
[34 35 50]
[50 44 31]]
In [4]:  a=np.arange(1,11)
print(a)
print('----------------CONDITIONAL PRINTING------------')
b=np.where(a==7) #print index value where element 7 is stored.
print(b)
print('-----------------------------------------------------------------')
a=np.array([1,2,3,7,8,4,7,6,5,7])
print(a)
print('-----------------------------------------------------------------')
b=np.where(a==7) #print index value where each element 7 is stored.
print(b)
print('-----------------------------------------------------------------')
print() #condition based searching
a=np.arange(1,11)
print(a)
print('-----------------------------------------------------------------')
b=np.where(a%2==0) #print index value of every even element
print(b)
print('-----------------------------------------------------------------')
a=a=np.array([1,2,3,7,8,13,26,65,77]) #sorted list is given here.
print(a)
print('-----------------------------------------------------------------')
index=np.searchsorted(a,6)
#print index value ,if we want to insert element 6 in a given sorted list.
print(index)

[ 1 2 3 4 5 6 7 8 9 10]
----------------CONDITIONAL PRINTING------------
(array([6], dtype=int64),)
-----------------------------------------------------------------
[1 2 3 7 8 4 7 6 5 7]
-----------------------------------------------------------------
(array([3, 6, 9], dtype=int64),)
-----------------------------------------------------------------

[ 1 2 3 4 5 6 7 8 9 10]
-----------------------------------------------------------------
(array([1, 3, 5, 7, 9], dtype=int64),)
-----------------------------------------------------------------
[ 1 2 3 7 8 13 26 65 77]
-----------------------------------------------------------------
3

In [ ]:  ​

You might also like