Notes7_Class_10_Basics_of_Numpy_Programs_Colaboratory
Notes7_Class_10_Basics_of_Numpy_Programs_Colaboratory
SAMPLE OUTPUT:
[12, 13, 14, 15, 16, 17] <class 'list'>
[12 13 14 15 16 17] <class 'numpy.ndarray'> int64
[ 12.5 56.7 88. 99. 100. ] <class 'numpy.ndarray'> float64
['12' '12.7' 'abcd' '56'] <class 'numpy.ndarray'> <U32
import numpy as np
#Various functions used to create arrays
#empty() - used to an empty or uninitialized array of specified dtype and shape.
# Default dtype is float.
print('Empty() function\n')
a1 = np.empty(4) #1D array with 4 elements
print(a1)
a2 = np.empty([3,4],dtype=int) #2D array with 3 rows & 4 columns, dtype is int
print(a2)
print('\nZeros() function\n')
#zeros() - used to create array of specified shape with defalut values as zero.
#Default dtype-float
#a3 = np.zeros(5)
a3 = np.zeros((5,)) #specified the shape as tuple
print(a3)
#a4 = np.zeros((2,3),dtype=int)
a4 = np.zeros([2,3],dtype=int)
print(a4)
print('\nOnes() function\n')
#ones()-used to create array of specified shape with defalut values as one.
#Default dtype-float
#a5 = np.ones(7)
a5 = np.ones((7,))
print(a5)
#a6 = np.ones([4,5],dtype=int)
a6 = np.ones((4,5),dtype=int)
print(a6)
print('\nFull() function\n')
#full() is used to create an array of specified shape with a default value
a7 = np.full(5,15) #1Darray of 5 elements with constant value 15
#a7 = np.full((5,),15)
print(a7)
print()
a8= np.full((2,5),12.5) #2D array of 2 rows; 5 columns with default float value
#a8= np.full([2,5],12.5)
print(a8)
print(a8.dtype)
x = np.full([6,7],20)
x = np.full((6,7),20)
print()
print('\nlinspace()\n')
#linspace() is used to create arrays(1D) based on given range;
#default type is float
#linspace(start index, stop index, no. of values to be generated)
#stop index is included;
#the generated values are equally spaced/difference btw them
a12 = np.linspace(2,3, )
print(a12)
a13 = np.linspace(2,3,11)
print(a13)
a14 = np.linspace(1,10,20)
print(a14)
print('\ncopy()\n')
#copy - copy the contents of one array to another
a15 = np.array([[10,20,30,40],[100,200,300,400]]) #2X4 array-2 rows,4columns
b = a15 #creating a copy, memory location of both arrays is same
c = a15.copy() #creating a copy using copy(), allocates new memory location
print("a15=",a15, id(a15))
print()
print("b=",b, id(b)) #id() - identity function, displays the memory location
print()
print("c",c, id(c))
a15[0][0]=1000 #modifying the value of element at 0 row,0 column
print("a15=",a15, id(a15))
print()
print("b=",b, id(b)) #id() - identity function, displays the memory location
print()
print("c",c, id(c)) #Changes made in original array will not reflect on its copy
print('\nreshape()\n')
#reshape() - Used to create a 2D arrays from a 1D array.
#No of elements remain same
a16 = np.arange(1,11) #Create an array of 10 elements, values= 1 to 10
print(a16,a16.shape)
print()
a17 = np.reshape(a16,(5,2)) # array to be reshaped and dimensions
print(a17, a17.shape)
x = np.arange(5,85,5)
print(x)
print(x.size)
y = np.reshape(x,(4,4)) #4 rows; 4 cols
y = np.reshape(x,(8,2)) #8 rows; 2 cols
y = np.reshape(x,(2,8))
y = np.reshape(x,(16,1))
y = np.reshape(x,(1,16))
SAMPLE OUTPUT:
Empty() function
Zeros() function
[0. 0. 0. 0. 0.]
[[0 0 0]
[0 0 0]]
Ones() function
[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]]
Full() function
[15 15 15 15 15]
arange() function
[1 3 5 7 9]
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
[50 55 60 65]
linspace()
copy()
a15= [[ 10 20 30 40]
[100 200 300 400]] 139767892719216
Array Slicing: Slicing and indexing of Numpy arrays is same as that of Strings, Lists and Tuples.
import numpy as np
#slicing 1 D Arrays
a1 = np.array([12,11,15,18,45,67,99,88,123,345,567])
print(a1[:]) # the whole array will printed
print(a1[4:]) #from 4th index till end
print(a1[:7]) #from 0th index to 6th index
print(a1[2:10:2]) #2nd,4th,6th,8th index
print(a1[-2:-9:-2])
print(a1[::]) #whole array
SAMPLE OUTPUT:
[ 12 11 15 18 45 67 99 88 123 345 567]
[ 45 67 99 88 123 345 567]
[12 11 15 18 45 67 99]
[ 15 45 99 123]
[345 88 67 18]
[ 12 11 15 18 45 67 99 88 123 345 567]
#Slicing 2D Array
import numpy as np
a1 = np.array([[12,13,14,15,16],[32,33,34,35,36],[42,43,44,45,46],[62,63,64,65,66
print(a1)
print()
print(a1[:,3])
print()
print(a1[2,:])
print()
print(a1[::-1,::-1]) #printing the array in reverse
print()
print(a1[:2,1:3])
print()
print(a1[::,::-1]) #elements of each row in reverse
print()
print(a1[1]) #all elements of row 1
print()
SAMPLE OUTPUT:
[[12 13 14 15 16]
[32 33 34 35 36]
[42 43 44 45 46]
[62 63 64 65 66]]
[15 35 45 65]
[42 43 44 45 46]
[[66 65 64 63 62]
[46 45 44 43 42]
[36 35 34 33 32]
[16 15 14 13 12]]
[[13 14]
[33 34]]
[[16 15 14 13 12]
[36 35 34 33 32]
[46 45 44 43 42]
[66 65 64 63 62]]
[62 63 64 65 66]
Joins/ Concatenation in Arrays concatenate() - This function is used to join 2 or more arrays of same shape.
SAMPLE OUTPUT:
[1 2 3 5 6 1 2 3 4]
For Joining 2D arrays, axis is specified. axis - refers to the axis along which arrays have to be joined.
Default value is 0.
b = np.array([[11,12,13,14,15],
[16,17,18,19,20]]) #2 rows; 5 columns
c = np.array([[100,200,300,400,500],
[10,20,30,40,50],
[33,44,55,66,77],
[123,121,122,124,125],
[1000,2000,3000,4000,5000]]) #5 rows; 5 columns
Arr3 = np.concatenate((a,d),axis=1)
print(Arr3)
print()
Arr4 = np.concatenate([c,d],axis=1)
print(Arr4)
print()
SAMPLE OUTPUT:
[[ 11 12 13 14 15]
[ 16 17 18 19 20]
[ 100 200 300 400 500]
[ 10 20 30 40 50]
[ 33 44 55 66 77]
[ 123 121 122 124 125]
[1000 2000 3000 4000 5000]]
[[ 1 2 0]
[ 3 4 0]
[ 4 6 0]
[ 7 8 0]
[ 9 10 0]]
SAMPLE OUTPUT:
[[ 7 5]
[ 6 8]
[ 10 20]
[ 17 15]
[ 16 18]
[100 200]]
[[ 7 5 17 15]
[ 6 8 16 18]
[ 10 20 100 200]]
[[ 7 5 1 2 3]
[ 6 8 111 222 333]
[ 10 20 888 999 777]]
[[ 7 5]
[ 6 8]
[ 10 20]
[ 17 15]
[ 16 18]
[100 200]]
Stacking
[[ 7 5 1 2 3]
[ 6 8 111 222 333]
[ 10 20 888 999 777]]
[[ 7 5]
[ 6 8]
[ 10 20]
[ 17 15]
[ 16 18]
[100 200]]
SAMPLE OUTPUT:
[20 30 40 50 60]
[ 0 10 20 30 40]
[100 200 300 400 500]
[1. 2. 3. 4. 5.]
[0 0 0 0 0]
[1 2 3 4 5]
[ 10000000000 10240000000000 590490000000000 10485760000000000
97656250000000000]
[11 22 33 44 55]
[ 9 18 27 36 45]
[ 10 40 90 160 250]
[10. 10. 10. 10. 10.]
[0 0 0 0 0]
[10 10 10 10 10]
[ 10 400 27000 2560000 312500000]
b = np.array([[1,2,3,4,5],
[6,7,8,9,10]])
SAMPLE OUTPUT:
[[220 240 260 280 300]
[320 340 360 380 400]]
[[12 14 16 18 20]
[22 24 26 28 30]]
[[10 10 10 10 10]
[10 10 10 10 10]]
[[ 11 24 39 56 75]
[ 96 119 144 171 200]]
[[11. 6. 4.33333333 3.5 3. ]
[ 2.66666667 2.42857143 2.25 2.11111111 2. ]]
[[0 0 1 2 0]
[4 3 2 1 0]]
[[11 6 4 3 3]
[ 2 2 2 2 2]]
[[ 11 144 2197 38416
759375]
[ 16777216 410338673 11019960576 322687697779
10240000000000]]
Math functions
[[ 6 7 8 9 10]
[11 12 13 14 15]]
[[12 14 16 18 20]
[22 24 26 28 30]]
[[10 10 10 10 10]
[10 10 10 10 10]]
[[ 11 24 39 56 75]
[ 96 119 144 171 200]]
#median() - median
print('Median')
print(np.median(c))
print(np.median(c,axis=1)) #row wise median
print(np.median(c,axis=0)) #column wise median
SAMPLE OUTPUT:
Original array
[[ 100 200 300 400 500]
[ 10 20 30 40 50]
[ 33 44 55 66 77]
[ 123 121 122 124 125]
[1000 2000 3000 4000 5000]]
Max
5000
[ 500 50 77 125 5000]
[1000 2000 3000 4000 5000]
Min
10
[ 100 10 33 121 1000]
[10 20 30 40 50]
Sum
17540
[ 1500 150 275 615 15000]
[1266 2385 3507 4630 5752]
Mean
701.6
[ 300. 30. 55. 123. 3000.]
[ 253.2 477. 701.4 926. 1150.4]
Median
122.0
[ 300. 30. 55. 123. 3000.]
[100. 121. 122. 124. 125.]
import numpy as np
a = np.array([1,2,3,4]) #1D array
b = np.array([[7,5],[6,8],[10,20]]) #3 rows; 2 columns
print('Example of math operations')
print(np.power(a,3)) #a raised to the power 3
print()
print(np.power(b,2)) #b raised to the power 2
print()
print(np.sqrt(b)) #square root
x = np.sqrt(b)
print()
print(np.floor(x))
SAMPLE OUTPUT:
[[ 49 25]
[ 36 64]
[100 400]]
[[2.64575131 2.23606798]
[2.44948974 2.82842712]
[3.16227766 4.47213595]]
[[2. 2.]
[2. 2.]
[3. 4.]]
import numpy as np
print('Sorting 1D array')
a = np.array([1,100,2,567,898,1023]) #1D array
a.sort() #sorted array in ascending order
print(a)
print()
a = np.array([1,100,2,567,898,1023])
a[::-1].sort() #Sorted in descending order
print(a)
print()
print('Sorting 2D array')
#By default 2D array is sorted row-wise means axis=1
c = np.array([[100,200,300,400,500],
[10,20,30,40,50],
[33,44,55,66,77],
[123,121,122,124,125],
[1000,2000,3000,4000,5000]]) #5 rows; 5 columns
print('Row-wise sorted array')
c.sort() #or d = c.sort(axis=1)
print(c)
print()
SAMPLE OUTPUT:
Sorting 1D array
[ 1 2 100 567 898 1023]
Sorting 2D array
Row-wise sorted array
[[ 100 200 300 400 500]
[ 10 20 30 40 50]
[ 33 44 55 66 77]
[ 121 122 123 124 125]
[1000 2000 3000 4000 5000]]
import numpy as np
#1D array
x = np.arange(1,11)
print(np.split(x,2)) #split array x in 2 arrays
SAMPLE OUTPUT:
if opt==1:
print('Arithmetic mean = ', np.mean(A))
elif opt==2:
print('Minimum element=', A.max())
else:
print('Invalid input')
SAMPLE OUTPUT:
Enter 1 to calculate arithmetic mean,
2 to display the maximum element:1
Arithmetic mean = 14.666666666666666
SAMPLE OUTPUT:
Enter 1 to subtract Y from x, 2 to multiple both arrays: 8
Invalid input
Write a menu driven program to create two 3X3 arrays and perform operationsbased on user input.
Option-1: Add
Option-2:Subtract
Option-3: Multiply
Option-4: Divide
import numpy as np
X = np.array([[117,127,137],[157,167,177],[197,107,117]])
Y = np.array([[1,2,3],[5,6,7],[9,10,11]])
print('Array X:\n', X)
print()
print('Array Y:\n', Y)
print()
opt = int(input('''Enter option:
Option-1: Add
Option-2:Subtract
Option-3: Multiply
if opt==1:
print('X+Y = ')
print(X+Y)
elif opt==2:
print('X-Y = ')
print(X-Y)
elif opt==3:
print('X*Y = ')
print(X*Y)
elif opt==4:
print('X/Y = ')
print(X/Y)
else:
print('Invalid input')
SAMPLE OUTPUT
Array X:
[[117 127 137]
[157 167 177]
[197 107 117]]
Array Y:
[[ 1 2 3]
[ 5 6 7]
[ 9 10 11]]
Enter option:
Option-1: Add
Option-2:Subtract
Option-3: Multiply
Option-4: Divide= 1
X+Y =
[[118 129 140]
[162 173 184]
[206 117 128]]
Write a menu driven program to create a 4X3 array and perform operations based on user input.
import numpy as np
X = np.array([[5,10,15],[190,165,123],[88,3,30],[11,17,183]])
print('Array X:\n', X)
print()
elif opt==2:
print('Sum of all elements = ', X.sum())
elif opt==3:
print('Maximum Element = ', X.max())
elif opt==4:
print('Minimum element = ', X.min())
else:
print('Invalid input')
SAMPLE OUTPUT
Array X:
[[ 5 10 15]
[190 165 123]
[ 88 3 30]
[ 11 17 183]]
Enter option:
Option-1: Arithmetic mean of all elements
Write a menu driven program to create a 3X3 array and perform operations based on user input.
import numpy as np
X = np.array([[100,400,900],[25,49,16],[4,64,125]])
print('Array X:\n', X)
print()
if opt==1:
print('Square of all the elements = ')
print(X*X)
elif opt==2:
print('Remainder when elements are divided by 5 = ')
print(X%5)
elif opt==3:
print('Square root of all the elements = ')
print(np.sqrt(X))
elif opt==4:
print('Cube of all the elements = ')
print(np.power(X,3))
else:
print('Invalid input')
SAMPLE OUTPUT
Array X:
[[100 400 900]
[ 25 49 16]
[ 4 64 125]]
Enter option:
Option-1: Square of all the elements
Write a menu driven program to create two 3X3 arrays and perform operations based on user input.
X = np.array([[117,127,137],[157,167,177],[197,107,117]])
Y = np.array([[1,2,3],[5,6,7],[9,10,11]])
print('Array X:\n', X)
print()
print('Array Y:\n', Y)
print()
opt = int(input('''Enter option:
Option-1: Join X and Y horizontally
if opt==1:
print('X and Y joined horizontally ')
print(np.hstack([X,Y]))
elif opt==2:
print('X and Y joined vertically ')
print(np.vstack([X,Y]))
elif opt==3:
print(' Elements of array X sorted row-wise')
print(X.sort(axis=1))
elif opt==4:
print('Elements of array Y sorted column-wise ')
print(Y.sort(axis=0))
else:
print('Invalid input')
SAMPLE OUTPUT
Array X:
[[117 127 137]
[157 167 177]
[197 107 117]]
Array Y:
[[ 1 2 3]
[ 5 6 7]
[ 9 10 11]]
Enter option:
Option-1: Join X and Y horizontally
Write a menu driven program to create a 3X3 array using different based on user input and display the resultant
array.
import numpy as np
X=[]
print('Creating array using different methods:')
print()
opt = int(input('''Enter option:
Option-1: Create array with 100 as default value
if opt==1:
X = np.full([3,3],100)
elif opt==2:
X = np.ones([3,3], dtype=int)
elif opt==3:
X = np.zeros([3,3], dtype=int)
elif opt==4:
a = np.arange(1,10)
X = np.reshape(a,(3,3))
else:
print('Invalid input')
Enter option:
Option-1: Create array with 100 as default value