0% found this document useful (0 votes)
3 views

Notes7_Class_10_Basics_of_Numpy_Programs_Colaboratory

This document provides sample code and explanations for various numpy functions, including array creation, attributes, and manipulation techniques. It includes examples of using functions like empty(), zeros(), ones(), full(), arange(), linspace(), and copy(), as well as array slicing and concatenation. A link to a Colab file containing the code is also provided for reference.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Notes7_Class_10_Basics_of_Numpy_Programs_Colaboratory

This document provides sample code and explanations for various numpy functions, including array creation, attributes, and manipulation techniques. It includes examples of using functions like empty(), zeros(), ones(), full(), arange(), linspace(), and copy(), as well as array slicing and concatenation. A link to a Colab file containing the code is also provided for reference.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Dear students,

This file contains sample code as a reference for numpy functions.

Menu driven programs are also included.

Link for this colab file is as follows:


https://colab.research.google.com/drive/15XS0A3ID13tsiMOLA5VZ8bB4T0jV0Ozd?usp=sharing

#To create an array from a list


import numpy as np # import statement, gave alias name np
list1 = [12,13,14,15,16,17]
list2 = [12,12.7,'abcd',56]
print(list1, type(list1))
ar = np.array(list1) # array() - used to convert a sequence to an array
ar2 = np.array([12.5,56.7,88.0,99,100])
ar3=np.array(list2)
print(ar, type(ar), ar.dtype)
print(ar2, type(ar2), ar2.dtype)
print(ar3, type(ar3), ar3.dtype)

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

#Attributes of numpy arrays


import numpy as np
a1 = np.array([13,15,16,17,18,19,101,292]) #creating an array
print(a1, type(a1))
print(a1.ndim) #dimension of the array
print(a1.shape) #returns the no. of elements in each dimension.
#It returns the output as a tuple
print(a1.dtype) #returns the data type of the array
print(a1.itemsize)
print(a1.size)
print(a1.size * a1.itemsize)

a2 = np.array([[13,15,16,17],[18,19,101,292]]) #creating 2 2D array


print(a2, type(a2))
print(a2.ndim) #dimension of the array
print(a2.shape) #returns the no. of elements in each dimension.
#It returns the output as a tuple
print(a2.dtype) #returns the data type of the array
print(a2.itemsize) #returns the no of bytes occupied by an element of array
print(a2.size) #Total number of elelemnts in an array
print(a2.size * a1.itemsize) #total number of bytes occupied by the array
SAMPLE OUTPUT:
[ 13 15 16 17 18 19 101 292] <class 'numpy.ndarray'>
1
(8,)
int64
8
8
64
[[ 13 15 16 17]
[ 18 19 101 292]] <class 'numpy.ndarray'>
2
(2, 4)
int64
8
8
64

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('\n arange() function\n')


#arange() is used to generate an array(1D) from a given range
a9 = np.arange(1,10,2) #1 3 5 7 9
print(a9)
a10 = np.arange(15) # 0 to 14
print(a10)
a11 = np.arange(50,70,5) #50 55 60 65
print(a11)

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

[2.47e-322 2.72e-322 2.96e-322 3.21e-322]


[[94271694010912 94271718113280 8 94271718113280]
[ 8 94271718113280 9 0]
[94271718113792 94271718113792 94271718113792 0]]

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]

[[12.5 12.5 12.5 12.5 12.5]


[12.5 12.5 12.5 12.5 12.5]]
float64

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()

[2. 2.02040816 2.04081633 2.06122449 2.08163265 2.10204082


2.12244898 2.14285714 2.16326531 2.18367347 2.20408163 2.2244898
2.24489796 2.26530612 2.28571429 2.30612245 2.32653061 2.34693878
2.36734694 2.3877551 2.40816327 2.42857143 2.44897959 2.46938776
2.48979592 2.51020408 2.53061224 2.55102041 2.57142857 2.59183673
2.6122449 2.63265306 2.65306122 2.67346939 2.69387755 2.71428571
2.73469388 2.75510204 2.7755102 2.79591837 2.81632653 2.83673469
2.85714286 2.87755102 2.89795918 2.91836735 2.93877551 2.95918367
2.97959184 3. ]
[2. 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3. ]
[ 1. 1.47368421 1.94736842 2.42105263 2.89473684 3.36842105
3.84210526 4.31578947 4.78947368 5.26315789 5.73684211 6.21052632
6.68421053 7.15789474 7.63157895 8.10526316 8.57894737 9.05263158
9.52631579 10. ]

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.

In case of 2D arrays, this function concatenates 2 arrays either by rows or columns.

#Concatenating three 1D Arrays


import numpy as np
a = np.array([1,2,3])
b= np.array([5,6])
d = np.array([1,2,3,4])
c = np.concatenate([a,b,d]) #joining 3 arrays
print(c)

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.

If axis=0, then the column dimension should be matched.


If axis=1, then row dimension should be matched.

#Concatenating two 2D Arrays


import numpy as np
a = np.array([[1,2],
[3,4],
[4,6],
[7,8],
[9,10]]) #5 rows; 2 columns

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

d = np.full((5,1),0) #5 rows, 1 column, default=0

#Concatenating arrays in different ways


Arr1 = np.concatenate([a,c],axis=1) #no of rows are same in a & c arrays
print(Arr1)
print()

Arr2 = np.concatenate((b,c),axis=0) #no of columns are same in b & c arrays


print(Arr2)
print()

Arr3 = np.concatenate((a,d),axis=1)
print(Arr3)
print()

Arr4 = np.concatenate([c,d],axis=1)
print(Arr4)
print()

SAMPLE OUTPUT:

[[ 1 2 100 200 300 400 500]


[ 3 4 10 20 30 40 50]
[ 4 6 33 44 55 66 77]
[ 7 8 123 121 122 124 125]
[ 9 10 1000 2000 3000 4000 5000]]

[[ 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]]

[[ 100 200 300 400 500 0]


[ 10 20 30 40 50 0]
[ 33 44 55 66 77 0]
[ 123 121 122 124 125 0]
[1000 2000 3000 4000 5000 0]]

#Example-2 of Joining and stacking

a = np.array([[7,5],[6,8],[10,20]]) #3 rows; 2 columns


d = np.array([[17,15],[16,18],[100,200]]) #3 rows; 2 columns
e = np.array([[1,2,3],[111,222,333],[888,999,777]]) #3 rows; 3 columns
print(np.concatenate([a,d]))
print()
print(np.concatenate([a,d],axis=1))
print()
b = np.concatenate([a,e],axis=1) #no of rows match
print(b)

c = np.concatenate([a,d],axis=0) #no of columns match


print()
print(c)
print()

#Stacking functions - hstack(), vstack()


#hstack() - horizontal stack
#vstack() - vertical stack
print('\nStacking\n')
a1 = np.hstack([a,e]) # gives same output as concatenate() with axis=1
print(a1)
print()
a2 = np.vstack([a,d]) # gives the same output as concatenate() with axis=0
print(a2)

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]]

Basic Arithmetic Operations on arrays

#Math operation on 1D Arrays


#Perform scalar operations
import numpy as np
a = np.array([10,20,30,40,50])
b = np.array([1,2,3,4,5])
c = a+10 #10 is a scalar value
print(c)
c = a-10
print(c)
c = a*10
print(c)
c = a/10
print(c)
c = a%10
print(c)
c = a//10
print(c)
c = a**10
print(c)
print()
#Perform operations on 2 arrays
c = a+b
print(c)
c = a-b
print(c)
c = a*b
print(c)
c = a/b
print(c)
c = a%b
print(c)
c = a//b
print(c)
c = a**b
print(c)

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]

#Math operations on 2D arrays


import numpy as np
a = np.array([[11,12,13,14,15],
[16,17,18,19,20]])

b = np.array([[1,2,3,4,5],
[6,7,8,9,10]])

c = a*20 #scalar operation


print(c)
print()
c = a+b
print(c)
c = a-b
print(c)
c = a*b
print(c)
c = a/b
print(c)
c = a%b
print(c)
c = a//b
print(c)
c = a**b
print(c)
print()
#Functions for basic math operations
print('Math functions')
d = np.add(b,5) #adding 5 to all the elements of array b
print(d)
print()
d = np.add(a,b)
print(d)
print()
d = np.subtract(a,b)
print(d)
print()
d = np.multiply(a,b)
print(d)
print()
d = np.divide(a,b)
print(d)

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]]

[[11. 6. 4.33333333 3.5 3. ]


[ 2.66666667 2.42857143 2.25 2.11111111 2. ]]

Statistical operations on Arrays


import numpy as np
#Basic statistical operations on arrays
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('Original array')
print(c)
print('Max')
#max()- return the maximum element/value in an array
print(c.max())
print(c.max(axis=1)) #gives us max value in each row
print(c.max(axis=0)) #gives us max value in each column
print()

#min()- return the minimum element/value in an array


print('Min')
print(c.min())
print(c.min(axis=1)) #min value in each row
print(c.min(axis=0)) #min value in each column
print()

#sum() - return the sum of elements


print('Sum')
print(np.sum(c))
print(c.sum(axis=1)) #sum of each row
print(c.sum(axis=0)) #sum of each column
print()

#mean() - arithmetic mean


print('Mean')
print(np.mean(c)) #OR print(c.mean())
print(c.mean(axis=1)) #row wise mean
print(c.mean(axis=0)) #column wise mean
print()

#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.]

Math operations on arrays using Math functions

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))

#Similarly explore other math functions

SAMPLE OUTPUT:

Example of math operations


[ 1 8 27 64]

[[ 49 25]
[ 36 64]
[100 400]]

[[2.64575131 2.23606798]
[2.44948974 2.82842712]
[3.16227766 4.47213595]]
[[2. 2.]
[2. 2.]
[3. 4.]]

Sorting Array elements - sort()

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()

print('Column-wise sorted array')


c.sort(axis=0) #column wise sorting
print(c)
print()

SAMPLE OUTPUT:

Sorting 1D array
[ 1 2 100 567 898 1023]

[1023 898 567 100 2 1]

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]]

Column-wise sorted array


[[ 10 20 30 40 50]
[ 33 44 55 66 77]
[ 100 122 123 124 125]
[ 121 200 300 400 500]
[1000 2000 3000 4000 5000]]

[[ 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]]

split() - used to split an array into sub-arrays

import numpy as np

#1D array
x = np.arange(1,11)
print(np.split(x,2)) #split array x in 2 arrays

SAMPLE OUTPUT:

[array([1, 2, 3, 4, 5]), array([ 6, 7, 8, 9, 10])]

#Menu Driven program


import numpy as np
A = np.array([[10,20,30],[11,22,33],[1,2,3]])
opt = int(input('''Enter 1 to calculate arithmetic mean,
2 to display the maximum element:'''))

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

#Menu Driven program


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]])
opt = int(input('Enter 1 to subtract Y from x, 2 to multiple both arrays: '))
if opt==1:
print('X-Y = ')
print(X-Y)
elif opt==2:
print('X*Y =')
print(X*Y)
else:
print('Invalid input')

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

Option-4: Divide= '''))


print()

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.

Option-1: Arithmetic mean of all elements

Option-2:Sum of all elements

Option-3: Maximum Element

Option-4: Minimum element

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()

opt = int(input('''Enter option:


Option-1: Arithmetic mean of all elements

Option-2:Sum of all elements

Option-3: Maximum Element

Option-4: Minimum element= '''))


print()
if opt==1:
print('Arithmetic mean of all elements = ', X.mean())

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

Option-2:Sum of all elements

Option-3: Maximum Element

Option-4: Minimum element= 2

Sum of all elements = 840

Write a menu driven program to create a 3X3 array and perform operations based on user input.

Option-1: Square of all the elements

Option-2: Remainder when elements are divided by 5

Option-3: Square root of all the elements

Option-4: Cube of all the elements

import numpy as np

X = np.array([[100,400,900],[25,49,16],[4,64,125]])

print('Array X:\n', X)
print()

opt = int(input('''Enter option:


Option-1: Square of all the elements

Option-2: Remainder when elements are divided by 5


Option-3: Square root of all the elements

Option-4: Cube of all the elements= '''))


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

Option-2: Remainder when elements are divided by 5

Option-3: Square root of all the elements

Option-4: Cube of all the elements= 4

Cube of all the elements =


[[ 1000000 64000000 729000000]
[ 15625 117649 4096]
[ 64 262144 1953125]]

Write a menu driven program to create two 3X3 arrays and perform operations based on user input.

Option-1: Join X and Y horizontally

Option-2: Join X and Y vertically

Option-3: Sort the elements of array X row-wise

Option-4: Sort the elements of array Y column-wise


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: Join X and Y horizontally

Option-2: Join X and Y vertically

Option-3: Sort the elements of array X row-wise

Option-4: Sort the elements of array Y column-wise= '''))


print()

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

Option-2: Join X and Y vertically

Option-3: Sort the elements of array X row-wise


Option-4: Sort the elements of array Y column-wise= 1

X and Y joined horizontally


[[117 127 137 1 2 3]
[157 167 177 5 6 7]
[197 107 117 9 10 11]]

Write a menu driven program to create a 3X3 array using different based on user input and display the resultant
array.

Option-1: Create array with 100 as default value

Option-2: Create array of ones (int)

Option-3: Create array of zeros (int)

Option-4: Create array with range of values 1 to 9

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

Option-2: Create array of ones (int)

Option-3: Create array of zeros (int)

Option-4: Create array with range of values 1 to 9= '''))


print()

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')

print('The resultant array=')


print(X)
SAMPLE OUTPUT

Creating array using different methods:

Enter option:
Option-1: Create array with 100 as default value

Option-2: Create array of ones (int)

Option-3: Create array of zeros (int)

Option-4: Create array with range of values 1 to 9= 3

The resultant array=


[[0 0 0]
[0 0 0]
[0 0 0]]

check 5s completed at 1:51 PM

You might also like